so I have a project which is to make a simple memory game using Arduino where the LEDs have to blink in a certain pattern and the player has to imitate the same pattern but unfortunately I'm having so much trouble with the code I want to make an if statement to check whether the button was pressed or not if it was it continues to check on the second button and if it wasn't the red led (which means wrong) has to turn on this is how i started:
enter code here
const int button1 = 2; // 1st button controls Blue LED
const int button2 = 3; // 2nd button controls Yellow LED
const int button3 = 4; // 3rd button controls Green LED
const int button4 = 5; // 4th button controls Red LED
const int led1 = 7; // Blue LED
const int led2 = 8; // Yellow LED
const int led3 = 9; // Green LED
const int led4 = 10; // Red LED
const int correctLED = 6;
const int wrongLED = 11;
int val = 0;
// Variables
int buttonState[] = {0,0,0,0}; // current state of the button
int lastButtonState[] = {0,0,0,0}; // previous state of the button
int buttonPushCounter[] = {0,0,0,0};
void setup() {
// initialize inputs :
randomSeed(analogRead(0));
pinMode(button1, INPUT);
pinMode(button2, INPUT);
pinMode(button3, INPUT);
pinMode(button4, INPUT);
// initialize outputs:
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
// initialize serial communication for debugging:
Serial.begin(9600);
}
}
void loop() {
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(7, HIGH);
delay(500);
digitalWrite(7,LOW);
delay(500);
digitalWrite(8, HIGH);
delay(500);
digitalWrite(8,LOW);
delay(500);
digitalWrite(9, HIGH);
delay(500);
digitalWrite(9,LOW);
delay(500);
digitalWrite(10, HIGH);
delay(500);
digitalWrite(10,LOW);
delay(500);
val = digitalRead(2); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(wrongLED, LOW); // turn LED OFF
} else {
digitalWrite(wrongLED, HIGH); // turn LED ON
}
val = digitalRead(3); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(wrongLED, LOW); // turn LED OFF
} else {
digitalWrite(wrongLED, HIGH); // turn LED ON
}
val = digitalRead(4); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(wrongLED, LOW); // turn LED OFF
} else {
digitalWrite(wrongLED, HIGH); // turn LED ON
}
val = digitalRead(5); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(wrongLED, LOW); // turn LED OFF
} else {
digitalWrite(wrongLED, HIGH); // turn LED ON
}
digitalWrite(correctLED, HIGH);
}
}