// TheUnlocker v1.5
// by Myth!
// The code defines a combination lock system using a sequence of button presses.
/// Declaration of constants and pins used
const int buttonPin[] = {5, 6, 7, 8, 9, 10, 11, 12};
const int ledPin[] = {2, 3, 4, 13, 14, 15, 16, 17};
const int RedLed = 18;
const int GreenLed = 19;
const int numButtonsLEDs = 8;
const int combination[] = {1, 2, 3, 4, 5}; // Combination sequence
int enteredSequence[sizeof(combination)] = {0};
unsigned long lastButtonPressTime = 0;
const unsigned long debounceDelay = 200;
void setup() {
// Set pin mode and initialize LEDs
pinMode(RedLed, OUTPUT);
pinMode(GreenLed, OUTPUT);
for (int i = 0; i < numButtonsLEDs; i++) {
pinMode(buttonPin[i], INPUT_PULLUP);
pinMode(ledPin[i], OUTPUT);
digitalWrite(ledPin[i], HIGH);
delay(200);
}
delay(2000);
turnOffLEDs();
}
void loop() {
// Static variable to keep track of current sequence index
static int sequenceIndex = 0;
unsigned long currentTime = millis();
// Loop through all buttons
for (int i = 0; i < numButtonsLEDs; i++) {
// Check button pressed and debounce time passed
if (digitalRead(buttonPin[i]) == LOW && (currentTime - lastButtonPressTime) > debounceDelay) {
enteredSequence[sequenceIndex++] = i + 1;
digitalWrite(ledPin[i], HIGH);
lastButtonPressTime = currentTime;
}
}
// Check if the entered sequence length matches the combination length
if (sequenceIndex == sizeof(combination) / sizeof(combination[0])) {
bool correctSequence = true;
for (int i = 0; i < sizeof(combination) / sizeof(combination[0]); i++) {
if (enteredSequence[i] != combination[i]) {
correctSequence = false;
break;
}
}
// Turn on the appropriate LED based on sequence correctness
digitalWrite(correctSequence ? GreenLed : RedLed, HIGH);
if (correctSequence) {
delay(1000);
turnOffLEDs();
while (true) { /* UNLOCKED! */ }
}
delay(1000);
digitalWrite(RedLed, LOW);
turnOffLEDs();
// Reset sequence index and entered sequence array
sequenceIndex = 0;
memset(enteredSequence, 0, sizeof(enteredSequence));
}
}
void turnOffLEDs() {
// Turn off all LEDs
for (int i = 0; i < numButtonsLEDs; i++) {
digitalWrite(ledPin[i], LOW);
delay(100);
}
}