I have four LEDs hooked up to pins 7-13. I'm trying to get one to light up randomly, stay on for three seconds, then turn off and repeat the process. For some reason, the code here won't work. I am not sure why. What happens when I upload it to my Arduino goes as follows:
The fourth LED lights up, stays for three seconds, then goes off and the second LED comes on, holds for three seconds then goes off and the third goes on, repeats process, then first goes on and the whole process is repeated. I am trying to get the order of the LEDs lighting up to be completely random.
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(11, OUTPUT);
pinMode(9, OUTPUT);
pinMode(7, OUTPUT);
pinMode(12, OUTPUT);
pinMode(10, OUTPUT);
pinMode(8, OUTPUT);
digitalWrite(12, LOW);
digitalWrite(10, LOW);
digitalWrite(8, LOW);
}
void loop() {
start:
int const led1 = 13;
int const led2 = 11;
int const led3 = 9;
int const led4 = 7;
int const ran = random(1,5);
if(ran == 1) {
digitalWrite(led1, HIGH);
delay(3000);
digitalWrite(led1, LOW);
goto start;
}
if(ran == 2) {
digitalWrite(led2, HIGH);
delay(3000);
digitalWrite(led2, LOW);
goto start;
}
if(ran == 3) {
digitalWrite(led3, HIGH);
delay(3000);
digitalWrite(led3, LOW);
goto start;
}
if(ran == 4) {
digitalWrite(led4, HIGH);
delay(3000);
digitalWrite(led4, LOW);
goto start;
}
}