Welcome to the N1IR Electronics Website. Totally off the cuff, one take, unrehearsed video projects for anyone interested in amateur radio, electronic design, makers, hardware hackers and science.


Get off you duff and build something!
Training the hand and mind since 1982.

Saturday, July 13, 2013

ATTIny85 Basic Combo Lock

Today I made a simple 4 button combo lock of a shed, I would like to thank Tanner Ewing for the original code. I've changed it to work on a ATTINY85 and eliminated the kill switch, a reset switch is optional for a 5th button but I wanted to make this as minimal as possible. With a little bit more code you can put a retry limit and lock it out for x amount of time after so many retries. Also you can extend the number of button pushes or even have double taps to make the combo more complex. I also can open the shed with a key so if there is an issue with power I can still get in.





ATTIny85 Basic Combo Lock


ATTIny85 Basic Combo Lock Code:
*************************************************************
 EXAMPLE 1
/*
Basic Combo Lock password 1234

N1IR - Chris Johnson
email: n1ir@arrl.net

Arduino 1.0 IDE using ATTINY85
http://hlt.media.mit.edu/?p=1695

Pin 1 = RESET  Pin 8 = VCC
Pin 2 = PB3    Pin 7 = PB2
Pin 3 = PB4    Pin 6 = PB1
Pin 4 = Gnd    Pin 5 = PB0

Work Cited
http://www.electronics123.com/s.nl/it.I/id.30/.f

*/

// set pin numbers:
const int buttonPin1 = 0; // Pin 5 = PB0
const int buttonPin2 = 1; // Pin 6 = PB1
const int buttonPin3 = 2; // Pin 7 = PB2
const int buttonPin4 = 3; // Pin 2 = PB3
const int RelayPin =  4;  // Pin 3 = PB4

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int combCheckSum = 0;

void setup() {
 
  pinMode(RelayPin, OUTPUT);  // initialize the LED pin as an output:   
  pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  // initialize the pushbutton pin as an input:
}

void loop(){
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);

 if (buttonState1 == LOW) {    // check if the 1st pushbutton is pressed
     combCheckSum = 1000;}     // if all statements are true make the checksum 1000

 if (buttonState2 == LOW) {    // check if the 2nd pushbutton is pressed
 if (buttonState1 == HIGH) {   // check if the 1st pushbutton is released
    
 if (combCheckSum == 1000) {   // check previous checksum
     combCheckSum = 1100;      // if all statements are true make the checksum 1100
 }
 }

else {
   combCheckSum = 0;          // If 1st and 2nd push button is not the corect one restart loop
 }
 }

 if (buttonState3 == LOW) {    // check if the 3rd pushbutton is pressed
 if (buttonState2 == HIGH) {   // check if the 2nd pushbutton is released
 if (combCheckSum == 1100) {   // check previous checksum
     combCheckSum = 1110;      // if all statements are true make the checksum 1110


}

else {
   combCheckSum = 0;          // If 3rd push button is not the corect one restart loop
}
}

if (buttonState4 == LOW) {    // check if the 4th pushbutton is pressed
if (buttonState3 == HIGH) {   // check if the 3rd pushbutton is released
if (combCheckSum == 1110) {   // check previous checksum
    combCheckSum = 1111;      // if all statements are true make the checksum 1111
}
}
else {
   combCheckSum = 0;          // If 4th push button is not the corect one restart loop
}
}

 //unlock the door.

 if (combCheckSum == 1111) {
   digitalWrite(RelayPin, HIGH);
   delay(3000);
   digitalWrite(RelayPin, LOW);
   combCheckSum = 0;
 }
}

*************************************************************
 EXAMPLE 2


/*
Basic Combo Lock password 2413

N1IR - Chris Johnson
email: n1ir@arrl.net

Arduino 1.0 IDE using ATTINY85
http://hlt.media.mit.edu/?p=1695

Pin 1 = RESET  Pin 8 = VCC
Pin 2 = PB3    Pin 7 = PB2
Pin 3 = PB4    Pin 6 = PB1
Pin 4 = Gnd    Pin 5 = PB0

Work Cited
http://www.electronics123.com/s.nl/it.I/id.30/.f

*/

// set pin numbers:
const int buttonPin1 = 0; // Pin 5 = PB0
const int buttonPin2 = 1; // Pin 6 = PB1
const int buttonPin3 = 2; // Pin 7 = PB2
const int buttonPin4 = 3; // Pin 2 = PB3
const int RelayPin =  4;  // Pin 3 = PB4

// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int combCheckSum = 0;

void setup() {
 
  pinMode(RelayPin, OUTPUT);  // initialize the LED pin as an output:   
  pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input:
  pinMode(buttonPin2, INPUT);
  pinMode(buttonPin3, INPUT);
  pinMode(buttonPin4, INPUT);
  // initialize the pushbutton pin as an input:
}

void loop(){
  // read the state of the pushbutton value:
  buttonState1 = digitalRead(buttonPin1);
  buttonState2 = digitalRead(buttonPin2);
  buttonState3 = digitalRead(buttonPin3);
  buttonState4 = digitalRead(buttonPin4);

 if (buttonState2 == LOW) {    // check if the 1st pushbutton is pressed
     combCheckSum = 1000;}     // if all statements are true make the checksum 1000

 if (buttonState4 == LOW) {    // check if the 2nd pushbutton is pressed
 if (buttonState2 == HIGH) {   // check if the 1st pushbutton is released
    
 if (combCheckSum == 1000) {   // check previous checksum
     combCheckSum = 1100;      // if all statements are true make the checksum 1100
 }
 }

else {
   combCheckSum = 0;          // If 1st and 2nd push button is not the corect one restart loop
 }
 }

 if (buttonState1 == LOW) {    // check if the 3rd pushbutton is pressed
 if (buttonState4 == HIGH) {   // check if the 2nd pushbutton is released
 if (combCheckSum == 1100) {   // check previous checksum
     combCheckSum = 1110;      // if all statements are true make the checksum 1110


}

else {
   combCheckSum = 0;          // If 3rd push button is not the corect one restart loop
}
}

if (buttonState3 == LOW) {    // check if the 4th pushbutton is pressed
if (buttonState1 == HIGH) {   // check if the 3rd pushbutton is released
if (combCheckSum == 1110) {   // check previous checksum
    combCheckSum = 1111;      // if all statements are true make the checksum 1111
}
}
else {
   combCheckSum = 0;          // If 4th push button is not the corect one restart loop
}
}

 //unlock the door.

 if (combCheckSum == 1111) {
   digitalWrite(RelayPin, HIGH);
   delay(3000);
   digitalWrite(RelayPin, LOW);
   combCheckSum = 0;
 }
}