Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Keypad.h>
- #include <Servo.h>
- #include <EEPROM.h>
- #include <LiquidCrystal.h>
- Servo myservo;
- #define Password_Lenght 7 // Give enough room for six chars + NULL char
- int pos = 0; // variable to store the servo position
- char Data[Password_Lenght]; // 6 is the number of chars it can hold + the null char = 7
- char Master[Password_Lenght] = "123456";
- char current[Password_Lenght];
- byte data_count = 0, master_count = 0;
- bool Pass_is_good;
- const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
- LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
- int c=0;
- bool cpress=false;
- int i=0;
- const byte ROWS=4;
- const byte COLS=4;
- char keys[ROWS][COLS]={
- {'1','2','3','A'},
- {'4','5','6','B'},
- {'7','8','9','C'},
- {'*','0','#','D'},
- };
- byte rowPins[ROWS]={9,8,7,6};
- byte colPins[COLS]={5,4,3,2};
- bool door = true;
- int e=0;
- //connect to the column pinouts of the keypad
- Keypad customKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS); //initialize an instance of class NewKeypad
- void setup()
- {
- Serial.begin(9600);
- myservo.attach(11);
- ServoClose();
- cpress=false;
- lcd.begin(16,2);
- c=0;
- e=0;
- }
- void loop()
- {
- // char customKey = customKeypad.getKey();
- // if(customKey)Serial.println(customKey);
- lcd.print("hello, world!");
- if (door == 0)
- {
- char customKey = customKeypad.getKey();
- if(customKey)Serial.println(customKey);
- if (customKey == '#')
- {
- ServoClose();
- door = 1;
- }
- }
- else Open();
- }
- void clearData()
- {
- while (data_count != 0)
- { // This can be used for any array size,
- Data[data_count--] = 0; //clear array for new data
- }
- cpress=false;
- c=0;
- return;
- }
- void ServoOpen()
- {
- for (pos = 180; pos >= 0; pos -= 5) { // goes from 0 degrees to 180 degrees
- // in steps of 1 degree
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- cpress=false;
- c=0;
- }
- void ServoClose()
- {
- for (pos = 0; pos <= 180; pos += 5) { // goes from 180 degrees to 0 degrees
- myservo.write(pos); // tell servo to go to position in variable 'pos'
- delay(15); // waits 15ms for the servo to reach the position
- }
- cpress=false;
- c=0;
- }
- void Open()
- {
- char customKey = customKeypad.getKey();
- if (customKey) // makes sure a key is actually pressed, equal to (customKey != NO_KEY)
- {
- Serial.println(customKey);
- if(customKey == 'C')
- {
- //Serial.println(535665);
- c=0;
- cpress=true;
- Serial.println(0);
- e=0;
- }
- else if(cpress)
- {
- current[c] = customKey; // store char into data array
- EEPROM.write(c++, customKey);
- if(c==6)
- {
- c=0;
- cpress=false;
- Serial.println(current);
- }
- Serial.println(1);
- }
- else {
- Data[data_count] = customKey; // store char into data array
- Serial.println(2);
- data_count++;
- }// increment data array by 1 to store new char, also keep track of the number of chars entered
- }
- if (data_count == Password_Lenght - 1) // if the array index is equal to the number of expected chars, compare data to master
- {
- Serial.println(Master);
- Serial.println(Data);
- for(int i=0; i<6; i++)
- {
- current[i]=EEPROM.read(i);
- }
- Serial.println(current);
- if (!strcmp(Data, Master) || !strcmp(Data, current)) // equal to (strcmp(Data, Master) == 0)
- {
- ServoOpen();
- door = 0;
- }
- else
- {
- delay(1000);
- door = 1;
- }
- clearData();
- }
- }
Add Comment
Please, Sign In to add comment