Hello, I am trying to make a game that five leds alternate randomly and when a button is pressed, the last light that was on stays on. Each led has 5 questions and chooses one of its five questions and displays it on the lcd when it is the led that stays on. But right now the code is only working for one led and the questions aren't displaying for the other leds.(using three leds just for this)
#include <Adafruit_LiquidCrystal.h>
Adafruit_LiquidCrystal lcd(0);
int Button = 6;
int button_state;
int White = 11;
int White_state;
const char* wq1 = "what is WAR";
const char* wq2 = "what is WBR";
const char* wq3 = "what is WCR";
const char* wq4 = "what is WDR";
const char* wq5 = "what is WER";
int Yellow = 9;
int Yellow_state;
const char* yq1 = "what is YAR";
const char* yq2 = "what is YBR";
const char* yq3 = "what is YCR";
const char* yq4 = "what is YDR";
const char* yq5 = "what is YER";
int Green = 10;
int Green_state;
const char* gq1 = "what is GAR";
const char* gq2 = "what is GBR";
const char* gq3 = "what is GCR";
const char* gq4 = "what is GDR";
const char* gq5 = "what is GER";
int lights[] = {White, Green, Yellow};
int numLights = sizeof(lights) / sizeof(lights[0]);
long random_question;
void setup() {
Serial.begin(9600);
pinMode(Button, INPUT);
lcd.begin(16, 2);
lcd.clear();
lcd.print(" Button Press");
delay(1000);
// Set each pin as an output
pinMode(White, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(Yellow, OUTPUT);
pinMode(Button, INPUT);
}
void loop() {
random_question = random(1, 5);
// Turn off all lights
turnOffAllLights();
lcd.setCursor(0,0);
lcd.print("COMP SCI GAME");
// Turn on a random light
int randomIndex = random(numLights);
digitalWrite(lights[randomIndex], HIGH);
delay(1000); // Adjust delay as needed
button_state = digitalRead(Button);
White_state = digitalRead(White);
Yellow_state = digitalRead(Yellow);
Green_state = digitalRead(Green);
while(button_state == HIGH)
{
lcd.clear();
button_state = digitalRead(Button);
White_state = digitalRead(White);
Yellow_state = digitalRead(Yellow);
Green_state = digitalRead(Green);
while(White_state == HIGH)
{
button_state = digitalRead(Button);
if(button_state == LOW){
break;}
while(button_state == HIGH)
{White_state = digitalRead(White);
Serial.println(random_question);
if(random_question == 1){lcd.setCursor(0,0); lcd.print(wq1);}
if(random_question == 2){lcd.setCursor(0,0); lcd.print(wq2);}
if(random_question == 3){lcd.setCursor(0,0); lcd.print(wq3);}
if(random_question == 4){lcd.setCursor(0,0); lcd.print(wq4);}
if(random_question == 5){lcd.setCursor(0,0); lcd.print(wq5);}
button_state = digitalRead(Button);
}
while(Yellow_state == HIGH)
{
button_state = digitalRead(Button);
if(button_state == LOW){
break;}
while(button_state == HIGH)
{Yellow_state = digitalRead(Yellow);
Serial.println(random_question);
if(random_question == 1){lcd.setCursor(0,0); lcd.print(yq1);}
if(random_question == 2){lcd.setCursor(0,0); lcd.print(yq2);}
if(random_question == 3){lcd.setCursor(0,0); lcd.print(yq3);}
if(random_question == 4){lcd.setCursor(0,0); lcd.print(yq4);}
if(random_question == 5){lcd.setCursor(0,0); lcd.print(yq5);}
button_state = digitalRead(Button);
}
while(Green_state == HIGH)
{
button_state = digitalRead(Button);
if(button_state == LOW){
break;}
while(button_state == HIGH)
{Green_state = digitalRead(Green);
Serial.println(random_question);
if(random_question == 1){lcd.setCursor(0,0); lcd.print(gq1);}
if(random_question == 2){lcd.setCursor(0,0); lcd.print(gq2);}
if(random_question == 3){lcd.setCursor(0,0); lcd.print(gq3);}
if(random_question == 4){lcd.setCursor(0,0); lcd.print(gq4);}
if(random_question == 5){lcd.setCursor(0,0); lcd.print(gq5);}
button_state = digitalRead(Button);
}
}
}
}}
}
void turnOffAllLights() {
// Turn off all lights
for (int i = 0; i < numLights; i++) {
digitalWrite(lights[i], LOW);
lcd.clear();
}
}