I am trying to get one led to turn on and off. So I have A led strip that has 32 leds. I am trying to make led 5 to turn off and on, and the color of red. The code is below.
I am miss something in my program. If you can help me out that would be great…
// * L2ggrrbb
// * 00 - LED off
// * 01 - LED on (max bright)
// * 10 - LED fade up (start at min bright)
// * 11 - LED fade down (start at max bright)
// Set which pins you will use to connect to the LED strip, you need 4 digital pins.
#define SIPIN 2
#define DIPIN 3
#define CLKPIN 4
#define LATCHPIN 5
#define LEDS 32 //Set this to the number of LEDs in your strip
#define CLOCK_DELAY (LEDS * 250)/1000 //This is the required delay between sending a bit and the clock line being fired.
//You can tweak this value lower until you find LEDs on the end of your chain misbehaving
//In fact, if you are running less than a few hundred LEDs you can get away with zero here.
//250nSec per LED is the specified value for the chip.
// These are the instructions that can be loaded for each LED in the strip.
// You must OR a Command or Commandx2 with any combination of the color operations.
// eg Command | BlueOn | RedDown will make an LED fad from Purpple to Blue.
// eg Commandx2 | GreenUp will make an LED fad UP to Green at double speed.
#define Command B10000000
#define Commandx2 B11000000 //Use this one to make dimming twice as fast.
#define BlueOff B00000000
#define BlueOn B00010000
#define BlueUp B00100000
#define BlueDown B00110000
#define RedOff B00000000
#define RedOn B00000100
#define RedUp B00001000
#define RedDown B00001100
#define GreenOff B00000000
#define GreenOn B00000001
#define GreenUp B00000010
#define GreenDown B00000011
#define White B00010101
unsigned char Lights[LEDS];
void setup()
{
//Set up our pins to connect to the LED strip.
pinMode(SIPIN, OUTPUT);
pinMode(DIPIN, OUTPUT);
pinMode(CLKPIN, OUTPUT);
pinMode(LATCHPIN, OUTPUT);
digitalWrite(SIPIN, LOW);
digitalWrite(DIPIN, LOW);
digitalWrite(CLKPIN, LOW);
digitalWrite(LATCHPIN, LOW);
int looper;
for ( looper = 0; looper < LEDS; ++looper ) {
Lights[looper] = BlueOff | RedOff | GreenOff;
}
}
void loop()
{
int looper;
for ( looper = 0; looper < LEDS; ++looper ) {
SendByte( Lights[looper] );
latch();
}
delay(50);
Lights[5] = RedOn;
for ( looper = 0; looper < LEDS; ++looper ) {
SendByte( Lights[looper] );
latch();
}
delay(50);
Lights[5] = RedOff | BlueOff | GreenOff;
}
void sPulse()
{
//Pulse the S line on the LEDs. This will make any LEDs that are fading do their thing.
//If the double speed bit IS set then two fades will occur. Once each time the pin is inverted.
digitalWrite(SIPIN, !digitalRead(SIPIN));
delayMicroseconds(100);
digitalWrite(SIPIN, !digitalRead(SIPIN));
delayMicroseconds(100);
}
void SendByte(unsigned char it)
{
//Send out one byte, don’t forget to LATCH it by calling latch()
//Note that for LARGE number of LEDs you may need to slow things down a little here.
digitalWrite(CLKPIN, LOW);
char x;
for(x=0;x < 8; x++)
{
if(B10000000 & it)
digitalWrite(DIPIN, HIGH);
else
digitalWrite(DIPIN, LOW);
it = it<<1;
// Wait here needs to be 250 x Number of LEDs nano seconds to allow data to propagate before being clocked in
delayMicroseconds(CLOCK_DELAY);
digitalWrite(CLKPIN, HIGH);
digitalWrite(CLKPIN, LOW);
}
}
void latch()
{
digitalWrite(LATCHPIN, HIGH);
delayMicroseconds(1); // spec sheet specifies minimum latch pulse of 1us
digitalWrite(LATCHPIN, LOW);
}
void runfader(int y, int d)
{
//Pulse the fader y times with delay d between each pulse
int x;
for(x=0;x<y;x++)
{
digitalWrite(SIPIN, LOW);
delay(d);
digitalWrite(SIPIN, HIGH);
delay(d);
}
}