Hello, I am completely new to programming so forgive me in advance if my code is inefficient or my question seems obvious. I have tried searching but am having a hard time finding a scenario that matches my situation.
I am trying to make an alarm system as a fun project to learn about arduino, programming, and electronics. My main question involves how to exit a while loop with the press of a keypad button.
I have successfully figured out how to have a user enter a password and then verify this password. I change the states of the alarm to "ARMED" and "OFF" accordingly. When the alarm enters the "ARMED" state, there is a countdown timer that allows the user to leave the house, before it enters the "ARMED" state.
What I am trying to figure out is how to exit out of this countdown timer with a key press? Essentially cancel the timer if necessary. I have tried while loops, do-while loops, and switch-case statements and I am having trouble finding something that works. I think the main problem in the code is the "delay" statement. I think while the delay is active, the arduino is not able to search for a keypad entry. I was able to kind of get it to work, but I have to press a buttons very fast in succession to get the program to recognize it in the middle of the while loop. I have read using .getKey twice is not good practice, but I am unsure of another way to accomplish finding a key press while in the while loop. Any suggestions? Code is below:
#include <Password.h>
#include <Keypad.h>
Password password = Password("2508");
const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
byte rowPins[ROWS] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
String alarmStatus = "DISARMED";
String prevalarmStatus = "NOTHING";
String switchStatus = "ARMED";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
keypad.addEventListener(keypadPress);
pinMode(13, OUTPUT);
pinMode(9,OUTPUT);
Serial.println("Enter Code: ");
}
void loop() {
keypad.getKey();
}
void keypadPress(KeypadEvent keyPress) {
switch (keypad.getState()) {
case PRESSED:
Serial.println(keyPress);
switch (keyPress) {
case '*': checkPassword(); break;
//case '#': password.reset(); break;
default: password.append(keyPress);
}
}
}
void checkPassword() {
if (password.evaluate()) {
alarmStatus = switchStatus;
if (alarmStatus == "ARMED"){
digitalWrite(13, HIGH);
digitalWrite(9,HIGH);
delay(1000);
digitalWrite(9,LOW);
Serial.println(alarmStatus);
switchStatus="OFF";
delay(1100);
countdownTimer();
}
if (alarmStatus =="OFF"){
digitalWrite(13, LOW);
confirmCommand();
Serial.println(alarmStatus);
switchStatus = "ARMED";
}
}
else {
Serial.println("Password Incorrect. Retry");
Serial.println("Enter Code: ");
password.reset();
}
password.reset();
}
void countdownTimer(){
int seconds = 10;
while (seconds > 0){
char cancelKey = keypad.getKey();
if (cancelKey != NO_KEY){
Serial.println("CANCELLED");
switchStatus = "ARMED";
confirmCommand();
break;
}
seconds -= 1;
delay(1000);
Serial.println(seconds);
}
}
void confirmCommand(){
digitalWrite(9,HIGH);
delay(300);
digitalWrite(9,LOW);
delay(200);
digitalWrite(9,HIGH);
delay(300);
digitalWrite(9,LOW);
}