Project 1

Combination Lock

Components

-solderless breadboard
-LEDs (red, green, and yellow)
-pushbuttons
-220-ohm resistors
-10-kilohm resistors
-jumper wires
-Arduino Nano IOt33 microcontroller

For this project, Akshita and I worked together to create a user-friendly combination lock.

We synthesized what we learned in class over the past month about working with analog and digital inputs and output. Unfortunately, while our circuit seems to be set up correctly, there is a bug in our code that prevents the lock from actually functioning. We hope to take a look at what went wrong in class and demo the lock once it’s working!

Proposal

Here is a sketch of our initial proposal:

Our proposal originally included a servomotor

We chose not to include a servomotor or speaker to keep it simple this time. However, we could add these elements in for vision-impaired users or those who prefer to interface with audio or motion.

Circuit Diagram

We sketched our circuit diagram in Fritzing: 

Note that there is one pushbutton missing above (our lock has 7); individual wires connecting the pushbuttons to the Arduino are not shown above

Circuit

The final circuit
The final circuit

Pushbuttons

The 7 pushbuttons are connected in parallel.

LED Indicators

We decided to add individual yellow LED indicators above each pushbutton so users know that the lock is registering or keeping count of the buttons as they are pressing them. These LEDs are not connected to the Arduino as outputs. This is one way to integrate LED indicators into a design without having to code for the outputs in Arduino.

Coding in Arduino

Each LED is powered via a 220-ohm resistor on one leg and ground on the other. Tom suggested attaching 10-kilohm resistor via switch to ground to complete the circuit. We trimmed the wires running from each pushbutton to the Arduino.

Tom Igoe and ITP Residents, Arnab and August, helped us troubleshoot our code in Arduino. We were still unable to get the red and green LEDs to work.

Here is our updated code:

int buttonPins[] = {10, 9, 8, 7, 6, 5, 4};
int openSequence[] = {0, 2, 4, 5};
int closeSequence[] = {3, 6, 0, 1, 4};

int lastButtonStates[] = {0, 0, 0, 0, 0, 0, 0};
int buttonPressOrder[] = {9, 9, 9, 9};
int buttonCloseOrder[] = {9, 9, 9, 9, 9};

int flag = 0;
int buttonClicks = 0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting...");
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT);
}

void loop()
{
  // Read all the pushbuttons
  // See if they changed
  for (int i = 0; i < 7; i++)
  {
    readButtonChange(i);
  }

  if (buttonClicks == 4 && flag == 0)
  {
    // Do the open comparision
    if (buttonPressOrder[0] == 0 && buttonPressOrder[1] == 2 && buttonPressOrder[2] == 4 && buttonPressOrder[3] == 5)
    {
      // Led win condition
      Serial.println("open");
      digitalWrite(14, HIGH);
      digitalWrite(15, LOW);
      flag = 1;
    }
    buttonClicks = 0;
  }

  if (buttonClicks == 5 && flag == 1)
  {
    if (buttonCloseOrder[0] == 3 && buttonCloseOrder[1] == 6 && buttonCloseOrder[2] == 0 && buttonCloseOrder[3] == 1 && buttonCloseOrder[4] == 4)
    {
      //led win condition
      Serial.println("close");
      digitalWrite(14, LOW);
      digitalWrite(15, HIGH);
      flag = 0;
    }
    buttonClicks = 0;
  }
}

void readButtonChange(int index)
{

  // Read the pushbutton input pin
  int buttonState = digitalRead(buttonPins[index]);

  // Compare the buttonState to its previous state
  if (buttonState != lastButtonStates[index])
  {
    //    Serial.print(index);
    //    Serial.print(" ");
    //    Serial.println(buttonState);
    lastButtonStates[index] = buttonState;

    if (buttonState == HIGH && flag == 0)
    {
      buttonPressOrder[buttonClicks] = index;
      buttonClicks++;
      printPreviousPressArray();
    }

    if (buttonState == HIGH && flag == 1)
    {
      buttonCloseOrder[buttonClicks] = index;
      buttonClicks++;
      printPreviousCloseArray();
    }
  }

  // Delay a little bit to avoid bouncing
  delay(10);
}

void printPreviousPressArray()
{
  Serial.print("flag: ");
  Serial.print(flag);
  Serial.print(" ");
  Serial.print("buttonClicked: ");
  Serial.print(buttonClicks);
  Serial.print(" buttonPressOrder: ");
  Serial.print(buttonPressOrder[0]);
  Serial.print(" ");
  Serial.print(buttonPressOrder[1]);
  Serial.print(" ");
  Serial.print(buttonPressOrder[2]);
  Serial.print(" ");
  Serial.println(buttonPressOrder[3]);
}
void printPreviousCloseArray()
{
  Serial.print("flag: ");
  Serial.print(flag);
  Serial.print(" ");
  Serial.print("buttonClicked: ");
  Serial.print(buttonClicks);
  Serial.print(" buttonCloseOrder: ");
  Serial.print(buttonCloseOrder[0]);
  Serial.print(" ");
  Serial.print(buttonCloseOrder[1]);
  Serial.print(" ");
  Serial.print(buttonCloseOrder[2]);
  Serial.print(" ");
  Serial.print(buttonCloseOrder[3]);
  Serial.print(" ");
  Serial.println(buttonCloseOrder[4]);
}

Everyone suggested we create an array (“lastButtonStates”) followed by a code (e.g. 0 (“o”); 2 (“p”); 4 (“e”); 5 (“n”) and sequence (“buttonPressOrder”). We added another sequence to close the lock.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s