Concept:
While thinking of unusual switches, the first thing that came to mind was how suitcase locks work. How setting some sort of combination and pressing a button would make that into a ‘password’ that you could repeatedly use to open the suitcase. Inspired and fascinated by the workings of such a simple concept I came up with the idea of mimicking a combination lock but instead using buttons on a breadboard. Although using our hands wasn’t technically a part of the assignment, I do believe that being able to set a combination using the code, and having the circuit only work when the correct combination is entered is a creative switch.
Code Highlight:
Approaching this problem I had to think very creatively about how the Arduino code would work with multiple switches. In the end the best solution I arrived at was setting a lock using the pins each switch was connected to and then checking if each switch pressed in order was one corresponding to its index in the combination array.
const int buttonPins[] = {2, 3, 4, 5}; // Digital pins connected to the buttons const int ledPin = 13; // Digital pin connected to the LED int combo[] = {0, 1, 2, 3}; // The correct sequence of button presses (in order) int input[4]; // Array to store the user's button press sequense int index = 0; // Keeps track of how many correct buttons have been pressed bool isUnlocked = false; // Flag to indicate if the correct combination has been entered void setup() { // Set up each button pin as an input for (int i = 0; i < 4; i++) { pinMode(buttonPins[i], INPUT); } // Set up the LED pin as an output pinMode(ledPin, OUTPUT); } void loop() { if (!isUnlocked) { // Only check for button presses if the lock is not yet unlocked for (int i = 0; i < 4; i++) { // Iterate over each button if (digitalRead(buttonPins[i]) == HIGH) { // Check if the current button is pressed delay(50) //Small delay to ensure one press is not registered multiple times if (digitalRead(buttonPins[i]) == HIGH) { // Confirm the button is still pressed if (i == combo[index]) { // Check if this button is the next in the correct sequence input[index] = i; // Record the correct press in the input array index++; // Move to the next position in the sequence delay(500); // Delay to avoid multiple readings from a single press } else { index = 0; // Reset if a wrong button is pressed break; // Exit the loop to start checking from scratch } } } } if (index == 4) { // If all buttons have been pressed in the correct order digitalWrite(ledPin, HIGH); // Turn on the LED to indicate success isUnlocked = true; // Set flag to indicate that the lock has been unlocked } } }
Demonstration:
The led only lights up when the correct combination is pressed
In this case the correct combination is yellow, red, green, blue for clarity of demonstration.