Unlock the Lights

Based on last week’s circuit, I wrote a new program to make it into a digital lock. The main idea is that if you press the buttons in the correct order, all three lights will light up (unlocked). I set two sets of password by incorporating potentiometer into my circuit.

Week 3 Assignment Illustration

The circuit on the breadboard is pretty similar to what I did last week. The only difference is the set up of the potentiometer. The potentiometer is connected to power, ground and analog input A0. When the voltage at A0 is below or equal to 512 volts, we use one set of the password. When the voltage at A0 is above 512 volts, we then use another set of the password.

For each set of password, the user needs to press three buttons in a particular order. (Each button is pressed exactly once.) For example, the password could be red, green and blue. It also could be green, blue and red. If the user enters the correct password, all three lights will light up; otherwise, the lights would remain off.  In my program, I set up a variable for the password at the beginning. Assume the correct password is red, blue and green. When the user presses red, blue and green in order, the variable password will increment by one each time. As a result, the variable password would equal to three after the correct password is entered. The code of the password program is attached below.

int potVal;
int threshold = 512; 
int statusLED =13;

int sRed;
int sBlue;
int sGreen;
int LEDred = 2;
int LEDblue = 4;
int LEDgreen = 8;
int pre_state_r;
int pre_state_b;
int pre_state_g;
int password = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(statusLED,OUTPUT);
  pinMode(LEDred, OUTPUT);
  pinMode(LEDblue, OUTPUT);
  pinMode(LEDgreen, OUTPUT);
  pinMode(3, INPUT);
  pinMode(5, INPUT);
  pinMode(9, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
potVal = analogRead(A0); //read the voltage of A0

if (potVal <= threshold){
  digitalWrite(statusLED,HIGH);
  
  //password 1
  digitalWrite(LEDred, LOW);
  digitalWrite(LEDblue, LOW);
  digitalWrite(LEDgreen, LOW);

  sRed = digitalRead(3); //status of the red button
  sBlue = digitalRead(5); //status of the blue button
  sGreen = digitalRead(9); // status of the green button
  
  switch (password) {

    case 0:
      if (sRed == HIGH && sRed != pre_state_r) { //1R
        digitalWrite(LEDred, HIGH);
        password += 1;
        //pre_state_r = 1;
      }
      break;

    case 1:
      if (sBlue == HIGH && sBlue != pre_state_b) { //1R 2B
        digitalWrite(LEDblue, HIGH);
        password += 1;
        // pre_state_b = 1;
      } else if (sRed == HIGH && sRed != pre_state_r) { //reset if blue is not pressed
        password = 0;
      } else if (sGreen == HIGH && sGreen != pre_state_g) { ////reset if blue is not pressed
        password = 0;
      }
      break;

    case 2:
      if (sGreen == HIGH && sGreen != pre_state_g) { //1R 2B 3G
        digitalWrite(LEDgreen, HIGH);
        password += 1;
        //  pre_state_g = 1;
      } else if (sRed == HIGH && sRed != pre_state_r) { //reset if green is not pressed
        password = 0;
      } else if (sBlue == HIGH && sBlue != pre_state_b) { ////reset if green is not pressed
        password = 0;
      }
      break;
  }
  
}else{
  digitalWrite(statusLED,LOW);
  
  //password 2
  digitalWrite(LEDred, LOW);
  digitalWrite(LEDblue, LOW);
  digitalWrite(LEDgreen, LOW);

  sRed = digitalRead(3); //status of the red button
  sBlue = digitalRead(5); //status of the blue button
  sGreen = digitalRead(9); // status of the green button
  
  switch (password) {

    case 0:
      if (sBlue == HIGH && sBlue != pre_state_b) { //1B
        digitalWrite(LEDblue, HIGH);
        password += 1;
        //pre_state_r = 1;
      }
      break;

    case 1:
      if (sGreen == HIGH && sGreen != pre_state_g) { //1B 2G
        digitalWrite(LEDgreen, HIGH);
        password += 1;
        // pre_state_b = 1;
      } else if (sRed == HIGH && sRed != pre_state_r) { //reset if green is not pressed
        password = 0;
      } else if (sBlue == HIGH && sBlue != pre_state_b) { ////reset if green is not pressed
        password = 0;
      }
      break;

    case 2:
      if (sRed == HIGH && sRed != pre_state_r) { //1B 2G 3R
        digitalWrite(LEDred, HIGH);
        password += 1;
        //  pre_state_g = 1;
      } else if (sGreen == HIGH && sGreen != pre_state_g) { //reset if red is not pressed
        password = 0;
      } else if (sBlue == HIGH && sBlue != pre_state_b) { ////reset if red is not pressed
        password = 0;
      }
      break;
  }
}


  if (password == 3) {
    digitalWrite(LEDred, HIGH);
    delay(200);
    digitalWrite(LEDred, LOW);
    delay(200);
    digitalWrite(LEDblue, HIGH);
    delay(200);
    digitalWrite(LEDblue, LOW);
    delay(200);
    digitalWrite(LEDgreen, HIGH);
    delay(200);
    digitalWrite(LEDgreen, LOW);
    delay(200);
    digitalWrite(LEDred, HIGH);
    delay(200);
    digitalWrite(LEDred, LOW);
    delay(200);
    digitalWrite(LEDblue, HIGH);
    delay(200);
    digitalWrite(LEDblue, LOW);
    delay(200);
    digitalWrite(LEDgreen, HIGH);
    delay(200);
    digitalWrite(LEDgreen, LOW);
    delay(200);
    password = 0;
  }
  pre_state_r = sRed;
  pre_state_b = sBlue;
  pre_state_g = sGreen;

}

However, the user might enter a wrong password. I used the switch/case statement to help execute code under such circumstances. (Thanks to Scott for telling me this powerful structure!) Click here for more info about switch/case statement. In my program, I set three cases where the variable password equals to zero, one and two, respectively. In each ease, only when the user presses the right button will the variable password increment by one; otherwise, the variable password will be reset to zero. Such a structure is less complicated than a bunch of nested if/else statements.

Here’s what the project looks like.

 

Leave a Reply