Hi all, did anyone know how to call another function inside void loop function? My code as below:
Error: 'openDoor' was not declared in this scope
#include <Servo.h>
#include <Keypad.h>
#include <EEPROM.h>
int mode = 0;
int doorSens = A1;
const byte numRows= 4; //number of rows on the keypad
const byte numCols= 3; //number of columns on the keypad
char keymap[numRows][numCols]=
{
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
char keypressed; //Where the keys are stored it changes very often
char code[]= {'6','6','0','1'}; //The default code, you can change it or make it a 'n' digits one
char code_buff1[sizeof(code)]; //Where the new key is stored
char code_buff2[sizeof(code)]; //Where the new key is stored again so it's compared to the previous one
short a=0,i=0,s=0,j=0; //Variables used later
byte rowPins[numRows] = {8,7,6,5}; //Rows 0 to 3 //if you modify your pins you should modify this too
byte colPins[numCols]= {4,A5,A4}; //Columns 0 to 3
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
Servo lock; //定义LED控制引脚
#define buzzer A2
void setup()
{
pinMode(A1,INPUT_PULLUP);
pinMode(doorSens, INPUT_PULLUP);
Serial.begin(9600); //配置9600
Serial.println("STANBY!");
lock.attach(A3);
lock.write(0);
// for(i=0 ; i<sizeof(code);i++){ //When you upload the code the first time keep it commented
// EEPROM.get(i, code[i]); //Upload the code and change it to store it in the EEPROM
// } //Then uncomment this for loop and reupload the code (It's done only once)
}
void loop()
{
keypressed = myKeypad.getKey(); //Constantly waiting for a key to be pressed
if(keypressed == '*')
{ // * to open the lock
Serial.println("Enter code");
GetCode(); //Getting code function
if(a==sizeof(code))
{
openDoor();
}
else
{
Serial.println("Wrong passowrd");
}
delay(2000);
Serial.println("Stanby");
}
if(keypressed == '#')
{ //To change the code it calls the changecode function
ChangeCode();
Serial.println("Stanby"); //When done it returns to standby mode
}
}
void GetCode(){ //Getting code sequence
i=0; //All variables set to 0
a=0;
j=0;
while(keypressed != '0'){ //The user press 0 to confirm the code otherwise he can keep typing
keypressed = myKeypad.getKey();
if(keypressed == code[i]&& i<sizeof(code)){ //if the char typed is correct a and i increments to verify the next caracter
a++; //Now I think maybe I should have use only a or i ... too lazy to test it -_-'
i++;
}
else
a--; //if the character typed is wrong a decrements and cannot equal the size of code []
}
}
keypressed = NO_KEY;
}
void ChangeCode(){ //Change code sequence
Serial.println("Changing code");
delay(1000);
Serial.println("Enter old code");
GetCode(); //verify the old code first so you can change it
if(a==sizeof(code)){ //again verifying the a value
Serial.println("Changing code");
GetNewCode1(); //Get the new code
GetNewCode2(); //Get the new code again to confirm it
s=0;
for(i=0 ; i<sizeof(code) ; i++){ //Compare codes in array 1 and array 2 from two previous functions
if(code_buff1[i]==code_buff2[i])
s++; //again this how we verifiy, increment s whenever codes are matching
}
if(s==sizeof(code)){ //Correct is always the size of the array
for(i=0 ; i<sizeof(code) ; i++){
code[i]=code_buff2[i]; //the code array now receives the new code
EEPROM.put(i, code[i]); //And stores it in the EEPROM
}
Serial.println("Code Changed");
delay(2000);
}
else{ //In case the new codes aren't matching
Serial.println("Code not match");
delay(1000);
}
}
else{ //In case the old code is wrong you can't change it
Serial.println("Wrong");
delay(2000);
}
}
void GetNewCode1(){
i=0;
Serial.println("Enter a new code and press 0");
delay(2000);
while(keypressed != '0'){ //A to confirm and quits the loop
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != '0' ){
Serial.println("nothing");
code_buff1[i]=keypressed; //Store caracters in the array
i++;
}
}
keypressed = NO_KEY;
}
void GetNewCode2(){ //This is exactly like the GetNewCode1 function but this time the code is stored in another array
i=0;
Serial.println("Confirm code and press 0");;
delay(3000);
while(keypressed != '0'){
keypressed = myKeypad.getKey();
if(keypressed != NO_KEY && keypressed != '0' ){
Serial.println("nothing");
code_buff2[i]=keypressed;
i++;
}
}
keypressed = NO_KEY;
}
void openDoor()
{ //Lock opening function open for 3s
Serial.println("Door open");
lock.attach(A3);
delay(1000);
lock.write(90);
delay(1000);
lock.detach();
delay(500);
while(digitalRead(doorSens)){
//Serial.println("waiting for the action");
}
closeDoor();
}
void closeDoor(){
lock.attach(A3);
delay(1000);
lock.write(0);
delay(1000);
lock.detach();
delay(1000);
}
void playsound() {
tone(buzzer, 131);
delay(200);
noTone(buzzer);
}