Skip to content

Instantly share code, notes, and snippets.

@devries
Created September 21, 2014 20:19
Show Gist options
  • Save devries/1f394f64b39dc3ba0dd4 to your computer and use it in GitHub Desktop.
Save devries/1f394f64b39dc3ba0dd4 to your computer and use it in GitHub Desktop.
Cool Neon Cylon Eye (originally from Mike Winter, possibly modified)
#include <SPI.h>
//LightStrand_RobotMike
//by Mike Winter
//version 3
//Demonstrates chaseing lights
//Intented use: testing LED interface
//Code State: experimental, but it worked for me with 25 lights
//Concept: user adjust Pots to make a color. Color travels down strand of lights
//SPI Interface to strand based on Keith's excellent SPI code, thanks Keith
//Instructions:
// 1) Connect hardware, use Cool Neon specs.
// 2) In software, set LEDS_ON_STRAND to number of lights on your strand.
// 3) run, turn pot knobs and watch color progress down strand
//Parts list:
//1 - light strand from Cool Neon
//1 - Arduino board
//3 - 10k pots (or 5k) for adjusting red, green, blue levels
//Connections:
//Arduino > LightStrand connections
//Pin 11 SPI Data > LightStrand data.
//Pin 13 SPI Clock > LightStrand clock.
//Connection to Pots (3 pots: Red,Green,Blue levels)
//Analog 0 > Pot 0
//Analog 1 > Pot 1
//Analog 2 > Pot 2
//Change these
const int LEDS_ON_STRAND = 100;
const int DELAY_SENDING_COLORS = 10;
int gDelayCount = 0;
int iter = 0;
//Store color values in 1 dimensional matrix
byte matrix_f [LEDS_ON_STRAND];
byte matrix_r [LEDS_ON_STRAND];
byte matrix_g [LEDS_ON_STRAND];
byte matrix_b [LEDS_ON_STRAND];
void setup() {
//Serial monitor for debugging
Serial.begin(9600);
//Pot analog in Pins
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
//LightStrand SPI interface
SPI.begin();
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV8);//second fastest on Arduino
//init array holding LED colors
matrix_SetAllElements(0,0,0); //clear matrix to 0
}
void loop() {
byte r,g,b,f;
int theOn;
int onbit;
matrix_SetAllElements(0,0,0);
theOn = iter%(2*LEDS_ON_STRAND);
if(theOn<LEDS_ON_STRAND) {
onbit=theOn;
}
else {
onbit = 2*LEDS_ON_STRAND-1-theOn;
}
matrix_SetElement(onbit,255,0,0);
lightStrand_Start(); //start
matrix_Display();
lightStrand_Start(); // end too!
if(theOn==0) {
delay(1);
}
else {
delay(5);
}
iter++;
}
//Read Pots to get color for LEDs
//Input: analog values for Pots
//Returns: values for color for an LED
void ui_inputLedColor(byte *r,byte *g,byte *b){
//Read red, green. blue pots
word raw0=0; //raw. Range 0 to 1023
word raw1=1023;
word raw2=0;
//scale range
byte scaled0 =lowByte(raw0>>2); //scaled to valid R,G,B values. Range 0 to 255
byte scaled1 =lowByte(raw1>>2);
byte scaled2 =lowByte(raw2>>2);
*r=scaled0;
*g=scaled1;
*b=scaled2;
// *f=lightStrand_MakeFlag(*r,*g,*b);
}
//Light Strand command: resets strand
//send the start frame packet
void lightStrand_Start(){
SPI.transfer(0x00);
SPI.transfer(0x00);
SPI.transfer(0x00);
SPI.transfer(0x00);
}
//Light Strand command: Send a R,G,B packet to a single light on the strand
void lightStrand_SendColorToLED(byte flag, byte red, byte green, byte blue){
SPI.transfer(flag);
SPI.transfer(blue);
SPI.transfer(green);
SPI.transfer(red);
}
//Light Strand utility: make required flag for color packet
byte lightStrand_MakeFlag(byte red, byte green, byte blue){
byte flag = 0;
flag = (red&0xC0)>>6;
flag |= ((green&0xC0)>>4);
flag |= ((blue&0xC0)>>2);
return ~flag;
}
//Set 1 element in matrix
void matrix_SetElement(int index, byte r, byte g, byte b){
matrix_f[index] = lightStrand_MakeFlag(r,g, b);
matrix_r[index] = r;
matrix_g[index] = g;
matrix_b[index] = b;
}
//Set entire matrix
void matrix_SetAllElements(byte r, byte g, byte b){
byte f = lightStrand_MakeFlag(r,g, b);
for (int i= 0; i < LEDS_ON_STRAND ; i++){
matrix_f[i] = f;
matrix_r[i] = r;
matrix_g[i] = g;
matrix_b[i] = b;
}
}
// Slide pixels toward end of matrix, with wrap
void matrix_ShiftElement(int index, int size)
{
//save last
byte last_f = matrix_f[size-1];
byte last_r = matrix_r[size-1];
byte last_g = matrix_g[size-1];
byte last_b= matrix_b[size-1];
for (int i=(size-2); i >= 0 ; i--){
matrix_f[i+1] = matrix_f[i];
matrix_r[i+1] = matrix_r[i];
matrix_g[i+1] = matrix_g[i];
matrix_b[i+1] = matrix_b[i];
}
//wrap
matrix_f[0] = last_f;
matrix_r[0] = last_r;
matrix_g[0] = last_g;
matrix_b[0] = last_b;
}
//Send colors in matrix to Strand (
void matrix_Display(){
for(int i = 0; i < LEDS_ON_STRAND; i++){
lightStrand_SendColorToLED( matrix_f[i],matrix_r[i],matrix_g[i],matrix_b[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment