I am making one of those memory games where leds display a sequence and you copy it by pressing the corresponding buttons in order. Problem arises when two of the same led are displayed sequentially at the end of a sequence requiring the user only to press the corresponding button once instead of twice. I am very new to coding arduinos and i have tried to work with ChatGPT to remedy the issue to no avail.
int currentNum = 0;
int randomNum[100];
int counting = 0;
int userPress = 0;
const int led1 = 10;
const int led2 = 9;
const int led3 = 8;
const int led4 = 7;
void setup()
{
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(A0));
randomNum[currentNum] = random(7,11);
}
void count()
{
digitalWrite(randomNum[counting], HIGH);
delay(500);
digitalWrite(randomNum[counting], LOW);
delay(500);
counting++;
}
void user(){
int btn1 = digitalRead(5);
int btn2 = digitalRead(4);
int btn3 = digitalRead(3);
int btn4 = digitalRead(2);
if(btn1 == HIGH && randomNum[userPress] == led1){
userPress++;
delay(50);
}else if(btn2 == HIGH && randomNum[userPress] == led2){
userPress++;
delay(50);
}else if(btn3 == HIGH && randomNum[userPress] == led3){
userPress++;
delay(50);
}else if(btn4 == HIGH && randomNum[userPress] == led4){
userPress++;
delay(50);
}else if(btn4 == HIGH && randomNum[userPress] == !led4 ||
btn3 == HIGH && randomNum[userPress] == !led3 ||
btn2 == HIGH && randomNum[userPress] == !led2 ||
btn1 == HIGH && randomNum[userPress] == !led1){
counting = 0;
userPress = 0;
currentNum = 1;
delay(1000);
}
}
void loop()
{
while(counting < currentNum){
count();
}
if(counting == currentNum){
counting = 0;
while(userPress < currentNum){
user();
}
}
if(userPress == currentNum){
currentNum++;
userPress = 0;
randomNum[currentNum] = random(7, 11);
}
}
Any help or even tips at any level are good as i am trying to learn and improve from this!
