Security Lock

#include <Servo.h> //Getting the Servo library
Servo myservo; //Initializing servo as myservo

int code[] = {0, 0, 0, 0}; //This variable is where button presses are stored.

//These variables initlaize pins (buttons and LEDs)
int buttonOnePin = 12;
int buttonTwoPin = 11;
int buttonThreePin = 10;
int buttonFourPin = 9;
int redLedPin = 8;
int greenLedPin = 7;

//buttonPin stores the pin numbers in an array so it is easier to access
int buttonPin[] = {12, 11, 10, 9};

//Initializing arrays (sets of variables) that store the states of the buttons
int reading[] = {0,0,0, 0};
int buttonState[] = {0,0,0, 0};
int lastButtonState[] = {0,0,0,0};

//The number of butons used minus one (To identify the location of the button
//being tested in an array, in which the first number is considered 0 not 1 so
//the total numbers in an array is one less than the actual number of buttons.
int numberButtons = 3;

int numberButtonsPressed=0; //Stores the number of times the button has been pressed

//An array that keeps track of the last time the pins were checked
long lastDebounceTime[] = {0, 0, 0, 0};

long debounceDelay = 50; //Milliseconds the button must be held down to not be noise

long resetDelay = 10000; //Sets the delay before resetting to 10 seconds

long lastPressTime = 0; //Time since the button was last pressed

//Stores whether or not each button has been pressed (true/false)
boolean buttonPressed[]={0,0,0,0};

void setup() {
//Initialize the buttons as inputs
pinMode(buttonOnePin, INPUT);
pinMode(buttonTwoPin, INPUT);
pinMode(buttonThreePin, INPUT);
pinMode(buttonFourPin, INPUT);

//Sets the servo pin
myservo.attach(6);

//Initial the LEDs as outputs
pinMode(redLedPin, OUTPUT);
pinMode(greenLedPin, OUTPUT);
}

void loop() {

//If no button has been pressed for more than resetDelay (10 seconds),
//set the values in code[] to 0
if((millis()-lastPressTime) > resetDelay) {
code[0] = code[1] = code[2] = 0;
}

//This for loop initializes the variable counter and runs through and then
//increases counter. It keeps looping through (changing counter) until counter
//is equal to numberButtons, with 4 buttons it is until counter is 3
for(int counter = 0; counter <= numberButtons; counter++) {
long time = millis(); //Interal timer

//Set the current reading of the button being tested to the
//opposite of what is read on buttonPin (it seems to be reading
//a non-pressed button as high and a pressed one as low, I’m not
//sure why).
reading[counter] = !digitalRead(buttonPin[counter]);

//If the button has been pressed, makes sure it is not just
//noise and if it’s not noise then set the number in code to
//the buttonpin of the pressed button.
if (reading[counter] == HIGH) {

lastPressTime=time; //Set the last time a button was pressed to the current time

if((time-lastDebounceTime[counter]) > debounceDelay) { //makes sure it isnt noise.
buttonPressed[counter]=true; //Set buttonPressed to true

//While the buttonPressed is true, increase numberButtonsPressed
//by one and then exit the loop.
while(buttonPressed[counter]==true) {
numberButtonsPressed++;
break;
}
//If pressed, put the pin of the button being tested into the correct spot
//in the array code. ie. If it is the first button pressed put it in the
//first spot, the second goes in the secondspot, etc.
code[(numberButtonsPressed-1)] = buttonPin[counter];
}

//This delay makes it so you can press a button once and the button number will only
//go into one spot in code[]. Changing the number of milliseconds here changes how
//long you need to press the button for.
delay(120);
}
else {
//The button state is low, reset the lastDebounceTime
lastDebounceTime[counter] = time;
}
}

//If 4 buttons have been pressed, reset the number of buttons to 0
if(numberButtonsPressed >= 4) {
numberButtonsPressed=0;
}

//Check if the code is what we want
if((code[0] == 12) && (code[1] == 11) && (code[2] == 10) && (code[3] == 9)) {
digitalWrite(greenLedPin, HIGH); //Turn green LED on
digitalWrite(redLedPin, LOW); //Turn red LED off
myservo.write(120); //Move the servo to 120 degrees (unlocked)
}

//If the code is not what we want
else {
digitalWrite(redLedPin, HIGH); //Turn red LED on
digitalWrite(greenLedPin, LOW); //Turn green LED off
myservo.write(140); //move the servo to 140 degrees (locked)
}
}

Leave a reply

Logged in as . Log out?

Your email address will not be published. Required fields are marked *