Go Down

Topic: Super simple, uber crackable (lol) digital combination lock (Read 618 times) previous topic - next topic

Hello all! I just finished my first "all-me" project. It's a digital combination lock. Easily crackable, and super simple. I wanted to go into this all on my own (not referring to tutorials or other projects) using only what I can recall from throughout my endeavours. It incorporates 5 tactile buttons, an LCD screen from Adafruit, a servo, two LEDs, and of course, the Uno. The code is below, followed by a pic. Comments most definitely welcome! The code is very messy (I'm sure there are dozens of other ways to do it) but I'm still a rookie/n00b, so suggestions and tips are also welcome :) (I'm a fan of constructive criticism!).
Code: [Select]
#include <Servo.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(2,3,4,5,6,7);
Servo myservo;
const int button1 = 8;
const int button2 = 9;
const int button3 = 10;
const int button4 = 11;
const int button5 = 12;

int count1 = 0;
int count2 = 0;
int count3 = 0;
int count4 = 0;
int count5 = 0;

int state1 = 0;
int state2 = 0;
int state3 = 0;
int state4 = 0;
int state5 = 0;

int prev1 = 0;
int prev2 = 0;
int prev3 = 0;
int prev4 = 0;
int prev5 = 0;

int totalcount = 0;
int rows = 0;

int red = A0;
int green = A1;

void setup(){
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(red, OUTPUT);
  pinMode(green, OUTPUT);
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("  :ENTER CODE:  ");
  Serial.begin(9600); // For debug. Visually see the button presses.
  myservo.attach(13);
  myservo.write(0);
}

void loop(){
  state1 = digitalRead(button1);
  state2 = digitalRead(button2);
  state3 = digitalRead(button3);
  state4 = digitalRead(button4);
  state5 = digitalRead(button5);
 
  if (state1 != prev1){
    delay(10);
    if (state1 == HIGH){
      count1++;
    }
    else{}
  }
 
  if (state2 != prev2){
    delay(10);
    if (state2 == HIGH){
      count2++;
    }
    else{}
  }
 
  if (state3 != prev3){
    delay(10);
    if (state3 == HIGH){
      count3++;
    }
    else{}
  }
 
  if (state4 != prev4){
    delay(10);
    if (state4 == HIGH){
      count4++;
    }
    else{}
  }
 
  if (state5 != prev5){
    delay(10);
    if (state5 == HIGH){
      count5++;
    }
    else{}
  }
 
  prev1 = state1;
  prev2 = state2;
  prev3 = state3;
  prev4 = state4;
  prev5 = state5;
 
  totalcount = count1 + count2 + count3 + count4 + count5;
 
  for (rows = 0; rows < totalcount; rows+1){
    lcd.setCursor(rows,1);
    lcd.print("*");
    rows++;
  }
 
 
   if (totalcount == 5){
    if (count1 == 2 && count2 == 0 && count3 == 1 && count4 == 2 && count5 == 0){
      lcd.clear();
      lcd.print(" CODE  ACCEPTED ");
      myservo.write(90);
      digitalWrite(green, HIGH);
      digitalWrite(red, LOW);
      count1 = 0;
      count2 = 0;
      count3 = 0;
      count4 = 0;
      count5 = 0;
      totalcount = 0;
      delay(10000);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("  :ENTER CODE:  ");
      myservo.write(0);
      digitalWrite(green, LOW);
    }
    else if (count1 != 2 && count2 != 0 && count3 != 1 && count4 != 2 && count5 != 0 && totalcount == 5){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(" CODE  REJECTED ");
      digitalWrite(red, HIGH);
      digitalWrite(green, LOW);
      count1 = 0;
      count2 = 0;
      count3 = 0;
      count4 = 0;
      count5 = 0;
      totalcount = 0;
      delay(5000);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("  :ENTER CODE:  ");
      digitalWrite(red, LOW);
    }
    else if (count1 > 2 && count2 > 0 && count3 > 1 && count4 > 2 && count5 > 0 || totalcount == 5){
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print(" CODE  REJECTED ");
      digitalWrite(red, HIGH);
      digitalWrite(green, LOW);
      count1 = 0;
      count2 = 0;
      count3 = 0;
      count4 = 0;
      count5 = 0;
      totalcount = 0;
      delay(5000);
      lcd.clear();
      lcd.setCursor(0,0);
      lcd.print("  :ENTER CODE:  ");
      digitalWrite(red, LOW);
    }
  }
  Serial.print("1=");
  Serial.print(count1);
  Serial.print(" 2=");
  Serial.print(count2);
  Serial.print(" 3=");
  Serial.println(count3);
}


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy