Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ulrichzwingli/06e8ef836aab7aaf921be73e9aaa992a to your computer and use it in GitHub Desktop.
Save ulrichzwingli/06e8ef836aab7aaf921be73e9aaa992a to your computer and use it in GitHub Desktop.
Physical computing : Combination Lock
/*
sketch to open a digital lock
*/
int attemptCount = 0;
int lastButtonState3 = 0;
int lastButtonState4 = 0;
int lastButtonState5 = 0;
int lastButtonState6 = 0;
int buttonState3;
int buttonState4;
int buttonState5;
int buttonState6;
int warningCount;
int aButCount = 0;
int bButCount = 0;
int cButCount = 0;
void setup() {
pinMode(3, INPUT);
pinMode(4, INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
Serial.begin(9600);
}
void loop() {
if (attemptCount <=2)
{
buttonState6 = digitalRead(6);
if (buttonState6 == lastButtonState6)
{
buttonState3 = digitalRead(3);
if (buttonState3 != lastButtonState3)
{
if (buttonState3 == HIGH)
{
aButCount ++;
}
Serial.println("A was pressed these number of times :");
Serial.println(aButCount);
lastButtonState3 = buttonState3;
}
delay(50);
buttonState4 = digitalRead(4);
if (buttonState4 != lastButtonState4)
{
if (buttonState4 == HIGH)
{
bButCount ++;
}
Serial.println("B was pressed these number of times :");
Serial.println(bButCount);
lastButtonState4 = buttonState4;
}
delay(50);
buttonState5 = digitalRead(5);
if (buttonState5 != lastButtonState5)
{
if (buttonState5 == HIGH)
{
cButCount ++;
}
Serial.println("C was pressed these number of times :");
Serial.println(cButCount);
lastButtonState5 = buttonState5;
}
delay(50);
}
else
{
int code = (aButCount * 100) + (bButCount * 10) + cButCount;
if (code == 215)
{
buttonState6 = 0;
aButCount = 0;
bButCount = 0;
cButCount = 0;
attemptCount ++;
Serial.println("Success !!!");
digitalWrite(13,HIGH); // lights up green LED
delay(5000);
digitalWrite(13,LOW);
delay(1000);
}
else
{
Serial.println("Failed Attempt !!!");
attemptCount ++;
buttonState6 = 0;
aButCount = 0;
bButCount = 0;
cButCount = 0;
digitalWrite(12,HIGH); // lights up red LED
delay(5000);
digitalWrite(12,LOW);
delay(1000);
}
}
}
else
{
for ( warningCount = 0; warningCount<=20; warningCount ++)
{
digitalWrite(12 , HIGH);
delay(300);
digitalWrite(12 , LOW);
delay(300);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment