I have a project where a random of 1 of 5 LEDs would light up and when a button corresponding to the LED that lit up is pressed, it would turn off the LED and repeat.
//LED pin variables
const int bLED = 2;
const int gLED = 4;
const int rLED = 6;
const int yLED = 8;
const int wLED = 10;
const int gsLED = 12;
const int goLED = 13;
//Button pin variables
const int bButton = 3;
const int gButton = 5;
const int rButton = 7;
const int yButton = 9;
const int wButton = 11;
//Button reading states
int bButtonState;
int gButtonState;
int rButtonState;
int yButtonState;
int wButtonState;
int cooldown = 5000;
int LEDinterval = 5000;
long prebLED = 0;
long pregLED = 0;
long prerLED = 0;
long preyLED = 0;
long prewLED = 0;
void setup() {
// put your setup code here, to run once:
//LED pin modes
pinMode(bLED, OUTPUT);
pinMode(gLED, OUTPUT);
pinMode(rLED, OUTPUT);
pinMode(yLED, OUTPUT);
pinMode(wLED, OUTPUT);
//Button pin modes
pinMode(bButton, INPUT);
pinMode(gButton, INPUT);
pinMode(rButton, INPUT);
pinMode(yButton, INPUT);
pinMode(wButton, INPUT);
Serial.begin(9600);
//Game starts
digitalWrite(gsLED, HIGH);
delay(2000);
digitalWrite(gsLED, LOW);
}
void loop() {
bButtonState = digitalRead(bLED);
gButtonState = digitalRead(gLED);
rButtonState = digitalRead(rLED);
yButtonState = digitalRead(yLED);
wButtonState = digitalRead(wLED);
unsigned long currentMillis = millis();
randomSeed(analogRead(A0));
int l2l = random(0,5);
if (l2l == 0) {
digitalWrite(bLED, HIGH);
if (currentMillis - prebLED >= LEDinterval) { //within a span of 5s, if...
prebLED = currentMillis;
if (bButtonState == 1) {
digitalWrite(bLED, LOW);
Serial.println("pressed");
delay(cooldown - 200);
return;
}
else if (bButtonState == 0) {
digitalWrite(bLED, LOW);
Serial.println("not pressed");
delay(cooldown - 200);
return; //where program is supposed to end
}
}
}
else if (l2l == 1) {
digitalWrite(gLED, HIGH);
if (currentMillis - pregLED >= LEDinterval) { //within a span of 5s, if...
prebLED = currentMillis;
if (gButtonState == 1) {
digitalWrite(gLED, LOW);
Serial.println("pressed");
delay(cooldown - 200);
return;
}
else if (gButtonState == 0) {
digitalWrite(gLED, LOW);
Serial.println("not pressed");
delay(cooldown - 200);
return; //where program is supposed to end
}
}
}
else if (l2l == 2) {
digitalWrite(rLED, HIGH);
if (currentMillis - prerLED >= LEDinterval) { //within a span of 5s, if...
prerLED = currentMillis;
if (rButtonState == 1) {
digitalWrite(rLED, LOW);
Serial.println("pressed");
delay(cooldown - 200);
return;
}
else if (rButtonState == 0) {
digitalWrite(rLED, LOW);
Serial.println("not pressed");
delay(cooldown - 200);
return; //where program is supposed to end
}
}
}
else if (l2l == 3) {
digitalWrite(yLED, HIGH);
if (currentMillis - preyLED >= LEDinterval) { //within a span of 5s, if...
preyLED = currentMillis;
if (yButtonState == 1) {
digitalWrite(yLED, LOW);
Serial.println("pressed");
delay(cooldown - 200);
return;
}
else if (yButtonState == 0) {
digitalWrite(yLED, LOW);
Serial.println("not pressed");
delay(cooldown - 200);
return; //where program is supposed to end
}
}
}
else if (l2l == 4) {
digitalWrite(wLED, HIGH);
if (currentMillis - prewLED >= LEDinterval) { //within a span of 5s, if...
prewLED = currentMillis;
if (wButtonState == 1) {
digitalWrite(wLED, LOW);
Serial.println("pressed");
delay(cooldown - 200);
return;
}
else if (wButtonState == 0) {
digitalWrite(wLED, LOW);
Serial.println("not pressed");
delay(cooldown - 200);
return; //where program is supposed to end
}
}
}
}
Currently I have problems with the timing, how to detect input within a span of 5s
I also have issues with ending the sketch, I had someone consult using break; but that didn't work.
My sketch is pretty repetitive so bare with me.