Hi,
I have been working on a project i.e. a keypad lock using arduino nano
the code which I have made is working fine for 4 digit code but when I enter 8 digit code the arduino restarts itself in fact before entering the 7th digit it restarts itself. I couldn't find any tutorial or article regarding 8 digit lock.
Thank You.
#include <Keypad.h>
#include <EEPROM.h>
#define Solenoid 11
#define Buzzer 12
//initializing the keypad
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 4; //number of columns on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows] = {9,8,7,6};
byte colPins[numCols]= {5,4,3,2};
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
// defining all the variables
char keypressed,extra = '1';
char code1[]={'1','1','1','1','1','1','1','1'},code2[]={'2','2','2','2','2','2','2','2'},code3[]={'3','3','3','3','3','3','3','3'},code4[]={'4','4','4','4','4','4','4','4'};
char resetcode1[]={'1','1','1','1','1','1','1','1'},resetcode2[]={'2','2','2','2','2','2','2','2'},resetcode3[]={'3','3','3','3','3','3','3','3'},resetcode4[]={'4','4','4','4','4','4','4','4'};
// code1 is also the master code
// code1 address = 0-7
// code2 address = 31-38
// code3 address = 51-58
// code4 address = 71-78
char codebuff1[9],codebuff2[9],codebuff3[9],codebuff4[9],change1[9],change2[9];
int add1=0,add2=31,add3=51,add4=71,i=0,whichcode=0,codecounter=0,mutecounter=0;
unsigned int stringcounter,tempcounter;
String tempcode1,tempcode2,tempcode3,tempcode4,tempcode;
String changecode1,changecode2;
String strcode="";
void setup() {
Serial.begin(115200);
pinMode(Solenoid,OUTPUT);
pinMode(Buzzer,OUTPUT);
Serial.println("Online");
Serial.println("Standby");
}
void loop() {
keypressed = myKeypad.getKey();
if(keypressed == '*'){
Serial.println(keypressed);
EnterCode();
//Serial.println("Returned from entercode");
//GetCode();
CompareCode();
}
if( keypressed == '#' ){
Serial.println(keypressed);
//Serial.println("Enter old code");
EnterCode();
Serial.println("Going to CompareNewCode");
delay(200);
CompareNewCode();
Serial.println("Going to GetNewCode");
delay(200);
GetNewCode();
}
if(keypressed == 'B'){
Serial.println(keypressed);
//keypressed = NO_KEY;
//keynewpressed = myKeypad.getKey();
EnterCode();
GetCode();
while(keypressed!='B'){
keypressed = myKeypad.getKey();
if((tempcode == tempcode2 || tempcode == tempcode3 || tempcode == tempcode4) && (keypressed == 'A')){
HardReset();
strcode ="";
tempcode ="";
tempcode1="";
tempcode2="";
tempcode3="";
tempcode4="";
keypressed = 'B';
}
if((tempcode != tempcode2 || tempcode != tempcode3 || tempcode != tempcode4)&&(keypressed == 'A')){
Serial.println("Incorrect Code");
//Serial.println("Standby");
keypressed = 'B';
}
}
tempcode = "";
keypressed = NO_KEY;
Serial.println("Standby");
}
if(keypressed == 'C'){
if(mutecounter == 0){
pinMode(Buzzer,INPUT);
mutecounter=1;
}
if(mutecounter == 1){
pinMode(Buzzer,OUTPUT);
mutecounter=0;
}
}
}
void EnterCode() {
delay(200);
//Serial.println("Enter the code");
tempcode = "";
strcode = "";
while(keypressed != 'A') {
keypressed = myKeypad.getKey();
//Serial.print(keypressed);
strcode += String(keypressed);
}
Serial.println(keypressed);
delay(200);
stringcounter = strcode.length();
stringcounter = stringcounter - 1;
delay(200);
tempcounter = stringcounter;
delay(200);
strcode.remove(tempcounter,1);
//delay(200);
tempcode = strcode;
//delay(200);
strcode = "";
//delay(200);
//Serial.println(tempcode);
keypressed = NO_KEY;
}
void CompareCode() {
Serial.println("Comparing Code......");
GetCode();
if(tempcode1==tempcode){
Serial.println("Code1 opening......");
OpenDoor();
}
else {
if(tempcode2 == tempcode){Serial.println("Code2 opening......");OpenDoor();}
else {
if(tempcode3 == tempcode){Serial.println("Code3 opening......");OpenDoor();}
else {
if(tempcode4 == tempcode){Serial.println("Code4 opening......");OpenDoor();}
else {
Serial.println("Incorrect code");
codecounter++;
if(codecounter == 3){
Beep();
codecounter=0;
}
}
}
}
}
strcode ="";
tempcode ="";
tempcode1="";
tempcode2="";
tempcode3="";
tempcode4="";
Serial.println("Standby");
}
void GetCode() {
strcode = "";
i=0;
for(add1=0;add1<=tempcode.length()-1;add1++){
EEPROM.get(i,codebuff1[i]);
strcode += codebuff1[i];
i++;
}
i=0;
tempcode1 = strcode;
i=31;
strcode = "";
for(add2=0;add2<=tempcode.length()-1;add2++){
EEPROM.get(i,codebuff2[i]);
strcode += codebuff2[i];
i++;
}
i=0;
tempcode2 = strcode;
i=51;
strcode = "";
for(add3=0;add3<=tempcode.length()-1;add3++){
EEPROM.get(i,codebuff3[i]);
strcode += codebuff3[i];
i++;
}
i=0;
tempcode3 = strcode;
i=71;
strcode = "";
for(add4=71;add4<=tempcode.length()-1;add4++){
EEPROM.get(i,codebuff4[i]);
strcode += codebuff4[i];
i++;
}
i=0;
tempcode4 = strcode;
}
void OpenDoor() {
digitalWrite(Solenoid,HIGH);
delay(3000);
digitalWrite(Solenoid,LOW);
Serial.println("Closing......");
}
void CompareNewCode() {
GetCode();
if(tempcode1==tempcode){
whichcode = 1;
}
else {
if(tempcode2 == tempcode){whichcode = 2;}
else {
if(tempcode3 == tempcode){whichcode = 3;}
else {
if(tempcode4 == tempcode){whichcode = 4;}
else {
Serial.println("Incorrect code");
Serial.println("Standby");
}
}
}
}
strcode = "";
tempcode ="";
tempcode1="";
tempcode2="";
tempcode3="";
tempcode4="";
}
void GetNewCode() {
//Serial.println("Enter new code");
delay(200);
EnterNewCodeb();
changecode1 = tempcode;
delay(200);
EnterNewCodeb();
changecode2 = tempcode;
//tempcode = ;
if(changecode1 == changecode2) {
changeCode();
}
}
void EnterNewCodea() {
Serial.println("New Code1");
tempcode = "";
strcode = "";
i=0;
while(keypressed != 'A') {
keypressed = myKeypad.getKey();
Serial.print(keypressed);
code1[i] = keypressed;
i++;
strcode += String(keypressed);
}
tempcode = strcode.toInt();
strcode = "";
keypressed = NO_KEY;
}
void EnterNewCodeb() {
Serial.println("New Code2");
delay(200);
tempcode = "";
strcode = "";
i=0;
delay(200);
while(keypressed != 'A') {
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != 'A' ){
Serial.print(keypressed);
change1[i]=keypressed; //Store caracters in the array
i++;
strcode += String(keypressed);
}
}
Serial.println("");
tempcode = strcode;
//Serial.println(tempcode);
for(i=0;i<=tempcode.length()-1;i++){
//code2[i] = (char)change1[i];
Serial.print((char)change1[i]);
}
strcode = "";
keypressed = NO_KEY;
delay(200);
Serial.println("");
}
void changeCode() {
//i=0;
delay(200);
Serial.println("In Changing code");
delay(200);
Serial.println(tempcode);
if(whichcode == 1){
i=0;
for(add1=0;add1<=tempcode.length()-1;add1++){
EEPROM.put(i,(char)change1[i]);
i++;
}
i=0;
}
else {
if(whichcode == 2){
for(add2=31;add2<=34;add2++){
EEPROM.put(add2,(char)change1[i]);
i++;
}
i=0;
}
else {
if(whichcode == 3){
for(add3=51;add3<=54;add3++){
EEPROM.put(add3,(char)change1[i]);
i++;
}
i=0;
}
else {
if(whichcode == 4){
for(add4=71;add4<=74;add4++){
EEPROM.put(add4,(char)change1[i]);
i++;
}
i=0;
}
else {
//Serial.println("Incorrect code");
}
}
}
}
Serial.println("Code changed at location");
Serial.println(whichcode);
whichcode = "";
strcode = "";
tempcode ="";
tempcode1="";
tempcode2="";
tempcode3="";
tempcode4="";
//changecode1="";
//changecode2="";
Serial.println("Standby");
}
void HardReset() {
Serial.println("Hard Reseting..");
i=0;
for(add1=0;add1<=7;add1++){
EEPROM.put(add1,resetcode1[i]);
i++;
}
i=0;
Serial.println("Code1 Written....");
for(add2=31;add2<=38;add2++){
EEPROM.put(add2,resetcode2[i]);
i++;
}
i=0;
Serial.println("Code2 Written....");
for(add3=51;add3<=58;add3++){
EEPROM.put(add3,resetcode3[i]);
i++;
}
i=0;
Serial.println("Code3 Written....");
for(add4=71;add4<=78;add4++){
EEPROM.put(add4,resetcode4[i]);
i++;
}
i=0;
Serial.println("Code4 Written....");
Serial.println("Hard Reset Done...");
}
void Beep() {
Serial.println("Beeping");
pinMode(Buzzer,OUTPUT);
digitalWrite(Buzzer,HIGH);
delay(5000);
digitalWrite(Buzzer,LOW);
if(mutecounter==1){
pinMode(Buzzer,INPUT);
}
Serial.println("Enter the code");
EnterCode();
GetCode();
CompareBeepCode();
}
void CompareBeepCode() {
Serial.println("Comparing Code......");
GetCode();
if(tempcode1==tempcode){
Serial.println("Code1 opening......");
OpenDoor();
}
else {
if(tempcode2 == tempcode){Serial.println("Code2 opening......");OpenDoor();}
else {
if(tempcode3 == tempcode){Serial.println("Code3 opening......");OpenDoor();}
else {
if(tempcode4 == tempcode){Serial.println("Code4 opening......");OpenDoor();}
else {
Serial.println("Incorrect code");
Beep();
}
}
}
}
strcode ="";
tempcode ="";
tempcode1="";
tempcode2="";
tempcode3="";
tempcode4="";
Serial.println("Standby");
}