const int numAttempts = 4;
//const int correctCombination[4] = {3, 2, 5, 1};
const int correctCombination[4] = {1, 1, 1, 1};
// Variables for storing the current combination and number of attempts
int currentCombination[4];
int attempts = 0;
// Pin numbers for the components
const int potentiometerPin = A0;
const int redLedPin = 13;
const int greenLedPin = 12;
const int buzzerPin = 2;
const int buttonPin = 8;
int potentiometerValue = 0;
void setup() {
// Initialize the serial port
Serial.begin(9600);
// Set the pin modes for the components
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
// Read the value of the potentiometer
potentiometerValue = analogRead(potentiometerPin);
Serial.println(potentiometerValue);
// Calculate the current number based on the potentiometer value
int currentNumber = potentiometerValue/102;
// Print the current number to the serial port
Serial.println(currentNumber);
// Wait for the button to be pressed
if (digitalRead(buttonPin) == LOW) {
delay(500);
// Serial.println(potentiometerValue);
// Serial.println(currentNumber);
// Store the current number in the combination array
currentCombination[attempts] = currentNumber;
// Increment the number of attempts
attempts++;
Serial.println(potentiometerValue);
Serial.println(currentNumber);
}
// If all 4 numbers have been entered, check the combination
if (attempts == 4) {
Serial.println(potentiometerValue);
Serial.println(currentNumber);
// Check if the combination is correct
bool correct = true;
for (int i = 0; i < 4; i++) {
if (currentCombination[i] != correctCombination[i]) {
correct = false;
break;
}
}
// If the combination is correct, blink the green LED and reset the attempts
if (currentNumber == correctCombination) {
digitalWrite(greenLedPin, HIGH);
delay(10);
digitalWrite(greenLedPin, LOW);
attempts = 0;
}
// If the combination is incorrect, add 1 to the number of attempts and light the red LED
if (correct = false) {
attempts++;
digitalWrite(redLedPin, HIGH);
delay(1000);
digitalWrite(redLedPin, LOW);
}
// If the combination has been entered incorrectly 4 times, ring the buzzer and reset the attempts
if (attempts >= numAttempts) {
digitalWrite(buzzerPin, HIGH);
delay(20);
digitalWrite(buzzerPin, LOW);
attempts = 0;
}
}
}