Hello everyone,
I'm trying to control a buzzer and a keypad using an Arduino Nano. The idea of this project is that a buzzer will turn on after 3 seconds when a button is pressed. The buzzer should turn off after the correct code is entered. The problems that I face are: When I used a delay(3000), during those 3 seconds none of the buttons on the keypad will be read. And I don't know how to program that the first button that is pressed (to activate the buzzer) won't count for the password.
#include <Key.h>
#include <Keypad.h>
#define buzzpin 2
#define DEBUG
const byte ROWS =1;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','4'}
};
byte rowPins[ROWS] = {5};
byte colPins[COLS] = {8,9,6,7};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
char data[] = " ";
int sequenceNum = 0;
void setup() {
#ifdef DEBUG
Serial.begin(9600);
#endif
pinMode(buzzpin, OUTPUT);
}
void loop(){
char key = keypad.getKey();
if (key != NO_KEY){ // if a key is pressed the buzzer should turn on after 3 seconds.
Serial.println(key);
delay (3000);
digitalWrite(buzzpin, HIGH);
Serial.print("buzzer activated");
}
if (key){
data[sequenceNum] = key;
sequenceNum++;
if (sequenceNum == 4){
#ifdef DEBUG
Serial.print(F("Code entered: "));
Serial.println(data);
#endif
if(strcmp(data, "2341") == 0){ //if the correct code is entered the buzzer turns off.
Serial.print("Correct ");
Serial.print("Buzzer disabled");
digitalWrite(buzzpin, LOW);
delay(100000000);
}
else
Serial.print("Wrong");
memset(data, 0, sizeof(data));
sequenceNum = 0;
}}}