Fica o código:
// Car Keypad
// Version 1.0
//
// Michael Casson Rogers (michael.casson@yahoo.com)
// June 14th, 2009
//
// I designed this program to emulate a Ford keypad as much as possible. I missed the one on my Lincoln.
//
// To unlock the car, type in the 5-digit 'factory' code.
// To lock the car, press the last two buttons together.
//
// Program up to three additional 'user' codes:
// Type the 'factory' code, then 1, then a new 5-digit code, and finish by pressing one of the first three buttons.
// Your new user code is now saved to that position. The car will confirm by locking and unlocking the doors.
// You can overwrite the code by saving another to the same position.
//
// Erase all user codes:
// Type the 'factory' code, then 1, then press 1 again and hold for two seconds. The car will confirm by locking
// and unlocking the doors.
//
// Input keys are assigned values 1, 3, 5, 7, and 9.
//
// Time out (reset input keys) after programmable time (default 5 seconds). All key presses must be pressed within
// this time from each other to be considered together. The backlight will turn off after timeout.
//
// Big thanks to Gian Pablo Villamil. I used his Simple Simon 2.0 to get me started.
// http://itp.nyu.edu/~gpv206/2006/09/simple_simon_v2.html
//
// and thanks to the many helpful people at Arduino Forum!
// http://www.arduino.cc
// For reading and storing user generated codes.
#include <EEPROM.h>
// Define the pin assignments for the input switches
#define KeyPin1 10
#define KeyPin3 9
#define KeyPin7 8
// Define backlight pin.
#define BackLight 19
// Define the pins the lock and unlock relays are connected to.
#define LockPin 15
#define UnlockPin 16
#define DebounceTime 20 // debounce time in milliseconds
#define PulseTime 150 // debounce time in milliseconds
#define TimeOutTime 5000 // timeout in milliseconds
int CodeArray[5] = {1, 3, 5, 7, 9}; // Enter desired combination code here, must be 5 digits. Use only 1, 3, 5, 7, and 9.
int InputArray[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // Starts InputArray with all 0's
int UserCodeArray1[5] = {EEPROM.read(0), EEPROM.read(1), EEPROM.read(2), EEPROM.read(3), EEPROM.read(4)};
int UserCodeArray2[5] = {EEPROM.read(5), EEPROM.read(6), EEPROM.read(7), EEPROM.read(8), EEPROM.read(9)};
int UserCodeArray3[5] = {EEPROM.read(10), EEPROM.read(11), EEPROM.read(12), EEPROM.read(13), EEPROM.read(14)};
int buttonPressed = 0;
int buttonPresses = 0;
// Remember when last press was for timeout function.
// Also, set lastPress as having just timed out so that the backlight doesn't light for the first few
// seconds on initialization because lastPress and millis are both zero.
unsigned long lastPress = millis() - (TimeOutTime + 1);
// counter for debouncing routine
unsigned long SwitchDownTime ;
// Define inputScore (used to compare InputArray to CodeArray)
int inputScore = 0 ;
void setup () {
//Serial.begin(9600); // set up Serial library at 9600 bps, used for debugging.
pinMode(BackLight, OUTPUT);
pinMode(LockPin, OUTPUT);
pinMode(UnlockPin, OUTPUT);
pinMode(KeyPin1, INPUT);
pinMode(KeyPin3, INPUT);
pinMode(KeyPin7, INPUT);
}
// Main loop - should be easy to follow
void loop () {
// Serial.println("loop");
getInput ();
timeOut ();
antiScan();
}
// Get the user's button presses and store it in InputArray
void getInput () {
// Serial.println("getInput");
//Button 1
if (digitalRead(KeyPin1) == LOW && digitalRead(KeyPin3) == HIGH && digitalRead(KeyPin7) == HIGH) {
SwitchDownTime = millis(); // Record time it went down (for debounce)
digitalWrite(BackLight, HIGH); // Light backlight while pressed
while (digitalRead(KeyPin1) == LOW && digitalRead(KeyPin3) == HIGH && digitalRead(KeyPin7) == HIGH) {
// Wait while a button is pressed
if (millis() - SwitchDownTime > DebounceTime && buttonPressed == 0) { // If button has been down longer than debounce
buttonPress(1);
}
if (millis() - SwitchDownTime > 2000 &&
InputArray[5] == CodeArray[0] &&
InputArray[6] == CodeArray[1] &&
InputArray[7] == CodeArray[2] &&
InputArray[8] == CodeArray[3] &&
InputArray[9] == CodeArray[4] &&
InputArray[10] == 0 &&
InputArray[11] == 1 &&
InputArray[12] == 2 &&
InputArray[13] == 1) {
eraseUserCodes();
}
}
buttonPressed = 0;
lastPress = millis() ; //Needed here also, in case button is held down for more than timeout value
} // end if
//Button 3
if (digitalRead(KeyPin1) == LOW && digitalRead(KeyPin3) == HIGH && digitalRead(KeyPin7) == LOW) {
SwitchDownTime = millis(); // Record time it went down (for debounce)
digitalWrite(BackLight, HIGH); // Light backlight while pressed
while (digitalRead(KeyPin1) == LOW && digitalRead(KeyPin3) == HIGH && digitalRead(KeyPin7) == LOW) {
// Wait while a button is pressed
if (millis() - SwitchDownTime > DebounceTime && buttonPressed == 0) { // If button has been down longer than debounce
buttonPress(3);
}
}
buttonPressed = 0;
lastPress = millis() ; //Needed here also, in case button is held down for more than timeout value
} // end if
//Button 5
if (digitalRead(KeyPin1) == LOW && digitalRead(KeyPin3) == LOW && digitalRead(KeyPin7) == HIGH) {
SwitchDownTime = millis(); // Record time it went down (for debounce)
digitalWrite(BackLight, HIGH); // Light backlight while pressed
while (digitalRead(KeyPin1) == LOW && digitalRead(KeyPin3) == LOW && digitalRead(KeyPin7) == HIGH) {
// Wait while a button is pressed
if (millis() - SwitchDownTime > DebounceTime && buttonPressed == 0) { // If button has been down longer than debounce
buttonPress(5);
}
}
buttonPressed = 0;
lastPress = millis() ; //Needed here also, in case button is held down for more than timeout value
} // end if
//Button 7
if (digitalRead(KeyPin1) == HIGH && digitalRead(KeyPin3) == LOW && digitalRead(KeyPin7) == HIGH) {
SwitchDownTime = millis(); // Record time it went down (for debounce)
digitalWrite(BackLight, HIGH); // Light backlight while pressed
while (digitalRead(KeyPin1) == HIGH && digitalRead(KeyPin3) == LOW && digitalRead(KeyPin7) == HIGH) {
// Wait while a button is pressed
if (millis() - SwitchDownTime > DebounceTime && buttonPressed == 0) { // If button has been down longer than debounce
buttonPress(7);
}
}
buttonPressed = 0;
lastPress = millis() ; //Needed here also, in case button is held down for more than timeout value
} // end if
//Button 9
if (digitalRead(KeyPin1) == HIGH && digitalRead(KeyPin3) == HIGH && digitalRead(KeyPin7) == LOW) {
SwitchDownTime = millis(); // Record time it went down (for debounce)
digitalWrite(BackLight, HIGH); // Light backlight while pressed
while (digitalRead(KeyPin1) == HIGH && digitalRead(KeyPin3) == HIGH && digitalRead(KeyPin7) == LOW) {
// Wait while a button is pressed
if (millis() - SwitchDownTime > DebounceTime && buttonPressed == 0) { // If button has been down longer than debounce
buttonPress(9);
}
}
buttonPressed = 0;
lastPress = millis() ; //Needed here also, in case button is held down for more than timeout value
} // end if
//Button 7&9 (Lock)
if (digitalRead(KeyPin1) == HIGH && digitalRead(KeyPin3) == LOW && digitalRead(KeyPin7) == LOW) {
SwitchDownTime = millis(); // Record time it went down (for debounce)
digitalWrite(BackLight, HIGH); // Light backlight while pressed
while (digitalRead(KeyPin1) == HIGH && digitalRead(KeyPin3) == LOW && digitalRead(KeyPin7) == LOW) {
digitalWrite(LockPin, HIGH); // Wait while a button is pressed
}
digitalWrite(LockPin, LOW);
lastPress = millis() - (TimeOutTime + 1);
timeOut ();
delay(200); //For when people let go of lock, it doesn't turn the light back on when one button is pressed longer (milliseconds!) than the other.
} // end if
} // end GetAnswer
// Compare CodeArray with InputArray
void checkAnswer () {
// Serial.println("checkAnswer");
//// Print current InputArray for debugging
// for (int i = 0; i <= 13; i++) {
// Serial.print(InputArray[i]);
// }
// Serial.print(" ");
// Serial.print(buttonPresses);
// Serial.println();
//// End print current InputArray
if (InputArray[7] == CodeArray[0] &&
InputArray[8] == CodeArray[1] &&
InputArray[9] == CodeArray[2] &&
InputArray[10] == CodeArray[3] &&
InputArray[11] == CodeArray[4] &&
InputArray[12] == 0 &&
InputArray[13] == 1) {
buttonPress(2);
buttonPresses = 0;
}
if (InputArray[0] == CodeArray[0] &&
InputArray[1] == CodeArray[1] &&
InputArray[2] == CodeArray[2] &&
InputArray[3] == CodeArray[3] &&
InputArray[4] == CodeArray[4] &&
InputArray[5] == 0 &&
InputArray[6] == 1 &&
InputArray[7] == 2 &&
InputArray[13] == 1) {
// Serial.print("Adding New Code: ");
// Serial.print(InputArray[7]);
// Serial.print(InputArray[8]);
// Serial.print(InputArray[9]);
// Serial.print(InputArray[10]);
// Serial.println(InputArray[11]);
EEPROM.write(0, InputArray[8]);
EEPROM.write(1, InputArray[9]);
EEPROM.write(2, InputArray[10]);
EEPROM.write(3, InputArray[11]);
EEPROM.write(4, InputArray[12]);
// Serial.println("New Code Added");
UserCodeArray1[0] = EEPROM.read(0);
UserCodeArray1[1] = EEPROM.read(1);
UserCodeArray1[2] = EEPROM.read(2);
UserCodeArray1[3] = EEPROM.read(3);
UserCodeArray1[4] = EEPROM.read(4);
doorConfirm();
}
if (InputArray[0] == CodeArray[0] &&
InputArray[1] == CodeArray[1] &&
InputArray[2] == CodeArray[2] &&
InputArray[3] == CodeArray[3] &&
InputArray[4] == CodeArray[4] &&
InputArray[5] == 0 &&
InputArray[6] == 1 &&
InputArray[7] == 2 &&
InputArray[13] == 3) {
// Serial.print("Adding New Code: ");
// Serial.print(InputArray[7]);
// Serial.print(InputArray[8]);
// Serial.print(InputArray[9]);
// Serial.print(InputArray[10]);
// Serial.println(InputArray[11]);
EEPROM.write(5, InputArray[8]);
EEPROM.write(6, InputArray[9]);
EEPROM.write(7, InputArray[10]);
EEPROM.write(8, InputArray[11]);
EEPROM.write(9, InputArray[12]);
// Serial.println("New Code Added");
UserCodeArray2[0] = EEPROM.read(5);
UserCodeArray2[1] = EEPROM.read(6);
UserCodeArray2[2] = EEPROM.read(7);
UserCodeArray2[3] = EEPROM.read(8);
UserCodeArray2[4] = EEPROM.read(9);
doorConfirm();
}
if (InputArray[0] == CodeArray[0] &&
InputArray[1] == CodeArray[1] &&
InputArray[2] == CodeArray[2] &&
InputArray[3] == CodeArray[3] &&
InputArray[4] == CodeArray[4] &&
InputArray[5] == 0 &&
InputArray[6] == 1 &&
InputArray[7] == 2 &&
InputArray[13] == 5) {
// Serial.print("Adding New Code: ");
// Serial.print(InputArray[7]);
// Serial.print(InputArray[8]);
// Serial.print(InputArray[9]);
// Serial.print(InputArray[10]);
// Serial.println(InputArray[11]);
EEPROM.write(10, InputArray[8]);
EEPROM.write(11, InputArray[9]);
EEPROM.write(12, InputArray[10]);
EEPROM.write(13, InputArray[11]);
EEPROM.write(14, InputArray[12]);
// Serial.println("New Code Added");
UserCodeArray3[0] = EEPROM.read(10);
UserCodeArray3[1] = EEPROM.read(11);
UserCodeArray3[2] = EEPROM.read(12);
UserCodeArray3[3] = EEPROM.read(13);
UserCodeArray3[4] = EEPROM.read(14);
doorConfirm();
}
//Entered Programming Mode, but tried to store in 7, not an option.
if (InputArray[0] == CodeArray[0] &&
InputArray[1] == CodeArray[1] &&
InputArray[2] == CodeArray[2] &&
InputArray[3] == CodeArray[3] &&
InputArray[4] == CodeArray[4] &&
InputArray[5] == 0 &&
InputArray[6] == 1 &&
InputArray[7] == 2 &&
InputArray[13] == 7) {
clearArray();
}
//Entered Programming Mode, but tried to store in 9, not an option.
if (InputArray[0] == CodeArray[0] &&
InputArray[1] == CodeArray[1] &&
InputArray[2] == CodeArray[2] &&
InputArray[3] == CodeArray[3] &&
InputArray[4] == CodeArray[4] &&
InputArray[5] == 0 &&
InputArray[6] == 1 &&
InputArray[7] == 2 &&
InputArray[13] == 9) {
clearArray();
}
if (InputArray[8] != 2 &&
InputArray[9] == CodeArray[0] &&
InputArray[10] == CodeArray[1] &&
InputArray[11] == CodeArray[2] &&
InputArray[12] == CodeArray[3] &&
InputArray[13] == CodeArray[4]) {
success();
}
if (InputArray[8] != 2 &&
InputArray[9] == UserCodeArray1[0] &&
InputArray[10] == UserCodeArray1[1] &&
InputArray[11] == UserCodeArray1[2] &&
InputArray[12] == UserCodeArray1[3] &&
InputArray[13] == UserCodeArray1[4]) {
success();
}
if (InputArray[8] != 2 &&
InputArray[9] == UserCodeArray2[0] &&
InputArray[10] == UserCodeArray2[1] &&
InputArray[11] == UserCodeArray2[2] &&
InputArray[12] == UserCodeArray2[3] &&
InputArray[13] == UserCodeArray2[4]) {
success();
}
if (InputArray[8] != 2 &&
InputArray[9] == UserCodeArray3[0] &&
InputArray[10] == UserCodeArray3[1] &&
InputArray[11] == UserCodeArray3[2] &&
InputArray[12] == UserCodeArray3[3] &&
InputArray[13] == UserCodeArray3[4]) {
success();
}
}
void success () {
// Serial.println("success");
digitalWrite(UnlockPin, HIGH);
delay(PulseTime);
digitalWrite(UnlockPin, LOW);
buttonPress(0);
buttonPresses = 0;
}
void timeOut () {
// Serial.println("timeout");
if (millis() - lastPress > TimeOutTime) {
clearArray();
digitalWrite(BackLight, LOW);
lastPress = millis() - (TimeOutTime + 1); //lastPress follows millis() just outside of timeout, to prevent problems with millis rollover lighting backlight after 50 days
buttonPresses = 0;
} else {
digitalWrite(BackLight, HIGH);
}
}
void clearArray () {
// Serial.println("clearArray");
for (int i = 0; i <= 13; i++) {
InputArray[i] = 0;
}
}
void buttonPress (int j) {
// Serial.println("buttonPress");
if (millis() - SwitchDownTime > DebounceTime) { // If button was down longer than debounce
InputArray[0] = InputArray[1];
InputArray[1] = InputArray[2];
InputArray[2] = InputArray[3];
InputArray[3] = InputArray[4];
InputArray[4] = InputArray[5];
InputArray[5] = InputArray[6];
InputArray[6] = InputArray[7];
InputArray[7] = InputArray[8];
InputArray[8] = InputArray[9];
InputArray[9] = InputArray[10];
InputArray[10] = InputArray[11];
InputArray[11] = InputArray[12];
InputArray[12] = InputArray[13];
InputArray[13] = j;
lastPress = millis();
buttonPresses++;
buttonPressed = 1;
checkAnswer ();
} // end if
}
void antiScan () {
// Serial.println("antiScan");
if (buttonPresses >= 35) {
for (int i = 0; i < 60; i++) {
digitalWrite(BackLight, HIGH); // Light backlight while pressed
delay(500);
digitalWrite(BackLight, LOW); // Light backlight while pressed
delay(500);
}
buttonPresses = 0;
}
}
void doorConfirm () {
// Serial.println("doorConfirm");
digitalWrite(LockPin, HIGH);
delay(PulseTime);
digitalWrite(LockPin, LOW);
delay(400);
digitalWrite(UnlockPin, HIGH);
delay(PulseTime);
digitalWrite(UnlockPin, LOW);
clearArray();
buttonPresses = 0;
// Serial.println("Confirmed");
}
void eraseUserCodes () {
// Serial.println("eraseUserCodes");
// Serial.println("Erase Mode Started");
EEPROM.write(0, 8);
EEPROM.write(1, 8);
EEPROM.write(2, 8);
EEPROM.write(3, 8);
EEPROM.write(4, 8);
EEPROM.write(5, 8);
EEPROM.write(6, 8);
EEPROM.write(7, 8);
EEPROM.write(8, 8);
EEPROM.write(9, 8);
EEPROM.write(10, 8);
EEPROM.write(11, 8);
EEPROM.write(12, 8);
EEPROM.write(13, 8);
EEPROM.write(14, 8);
// Serial.println("Erase Mode Finished");
UserCodeArray1[0] = 8;
UserCodeArray1[1] = 8;
UserCodeArray1[2] = 8;
UserCodeArray1[3] = 8;
UserCodeArray1[4] = 8;
UserCodeArray2[0] = 8;
UserCodeArray2[1] = 8;
UserCodeArray2[2] = 8;
UserCodeArray2[3] = 8;
UserCodeArray2[4] = 8;
UserCodeArray3[0] = 8;
UserCodeArray3[1] = 8;
UserCodeArray3[2] = 8;
UserCodeArray3[3] = 8;
UserCodeArray3[4] = 8;
doorConfirm();
}