const int buttonPin[] = {2, 3, 4, 5, 6, 7}; // array of button pins connected to digital pins 2, 3, 4, 5, 6 and 7
int buttonState[6] = {0}; // array to store the current states of the buttons
int buttonCount[6] = {0}; // array to store the number of button presses
const int numberOfButtons = 6; // number of buttons
const int password[] = {1, 2, 3, 4}; // password array
int passwordSize = sizeof(password) / sizeof(password[0]); // size of the password array
bool isCorrect = false; // variable to store if the password is correct
void setup() {
for (int i = 0; i < numberOfButtons; i++) {
pinMode(buttonPin[i], INPUT); // set each button pin as an input
}
Serial.begin(9600); // initialize serial communication
}
void loop() {
for (int i = 0; i < numberOfButtons - 2; i++) {
buttonState[i] = digitalRead(buttonPin[i]); // read the current state of each button
if (buttonState[i] == HIGH) { // if the button is pressed
buttonCount[i]++; // increment the button press count
if (buttonCount[i] > 9) { // if the button press count is greater than 9
buttonCount[i] = 0; // reset the button press count to 0
Serial.print("Button ");
Serial.print(i + 1);
Serial.print(" Press Count: ");
Serial.println(buttonCount[i]); // print the current button press count to the serial monitor
}
}
// Check the penultimate button for password check
buttonState[numberOfButtons - 2] = digitalRead(buttonPin[numberOfButtons - 2]);
if (buttonState[numberOfButtons - 2] == HIGH) {
// compare the buttonCount array with the password array
for (int i = 0; i < passwordSize; i++) {
if (buttonCount[i] == password[i]) {
isCorrect = true;
} else {
isCorrect = false;
break;
}
}
if (isCorrect) {
Serial.println("Correct Password!");
} else {
Serial.println("Wrong Password!");
}
// Reset buttonCount and isCorrect
for (int i = 0; i < numberOfButtons - 2; i++) {
buttonCount[i] = 0;
}
isCorrect = false;
}
// Check the last button for resetting the buttonCount array
buttonState[numberOfButtons - 1] = digitalRead(buttonPin[numberOfButtons - 1]);
if (buttonState[numberOfButtons - 1] == HIGH) {
for (int i = 0; i < numberOfButtons - 2; i++) {
buttonCount[i] = 0;
}
Serial.println("Button Counts Reset!");
}
}
}