Im having a problem with my arduino code.Im trying to make an alarm system with a pir sensor.So when the pir activates and the buzzer starts buzzing i want to be able to type a set password in the keypad and the buzzer should stop buzzing. since my passcode is 4-digit (1331) I wrote this code, but it`s not working.
while(i<=4,i++)
{
char key = keypad.getKey();
pass.append(key);
}
if(pass.evaluate())
{
digitalWrite(buzzer, LOW);
keypad.waitForKey();
delay(6000);
}
My idea is that the program checks 4 times for the pressed key and adds it to the guessed password (im using the password library and the password is defined as well as int i=0;) But for some reason when I type 1331 the buzzing continues. Please help me with that project and excuse my grammar, English is not my native language.
edit: the full code:
#include <Password.h>
Password pass = Password( "1331" );
#include <Keypad.h>
int sensor = 11;
int buzzer = 10;
int i = 0;
const byte rows = 4; //four rows
const byte cols = 4; //four columns
char keys[rows][cols] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[rows] = {8, 7, 6, 9}; //connect to the row pinouts of the keypad
byte colPins[cols] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, rows, cols );
void setup() {
pinMode(sensor, INPUT);
pinMode (buzzer, OUTPUT);
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
delay(125);
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
delay(125);
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
keypad.waitForKey();
digitalWrite(buzzer,HIGH);
delay(125);
digitalWrite(buzzer, LOW);
delay(6000);
}
void loop() {
char state = digitalRead(sensor);
if (state == HIGH)
{
digitalWrite(buzzer, HIGH);
while(i<4,i++)
{
char key = keypad.getKey();
//keypad.waitForKey(); not sure if i should use that
pass.append(key);
}
if(pass.evaluate())
{
digitalWrite(buzzer, LOW);
keypad.waitForKey();
delay(6000);
}
}
}
1
, it will keep appending1
s till it reaches 4. – Gerben Mar 4 '17 at 12:34