Hi,
I'm relatively new to arduino and I've run into a problem.
I have 8 LEDs wired up via a 74HC595 and I am trying to have them light up randomly one by one when numbers 0-7 are put out. However, if the number 8 is produced I want all LEDs to switch off. So far, switching them on works just fine but they do not switch off if 8 is put out. I guess it has something to do with the if statement and what it accepts but I have no idea how to fix it.
int latchPin = 11;
int clockPin = 9;
int dataPin = 12;
long led;
byte leds = 0;
void updateShiftRegister()
{
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, leds);
digitalWrite(latchPin, HIGH);
}
void setup()
{
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
updateShiftRegister();
Serial.begin(9600);
while (! Serial);
Serial.println("Enter LED Number 0 to 7 or '8' to clear");
randomSeed (analogRead(0));
}
void loop()
{ led = random(0,9);
if (led == '8')
{
leds = 0;
updateShiftRegister();
Serial.println("All LEDs turned off");
}
else
{ bitSet(leds, led);
updateShiftRegister();
Serial.print("Turned on LED ");
Serial.println(led);
}
delay (2000);
}