0

Help pls

Hi, im a student and im trying to make a alarme for my school project i made a sketch white arduino uno, keypad 4x4, a switch and a buzer.

And i add made a code to make all of this just work as in the code below but dosent work the way it should can someone help me pls

 

#include <Keypad.h>

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] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A4, A3, A2, A1}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

const int pinSensor = 9;
const int pinSirene = 11;

const int tempoParaSair = 3 * 1000; //3 segundos
const int tempoParaEntrar = 4 * 1000; //4 segundos


int secreto = 1234; //cant be changed by the time
int codigo;

int lerCodigo(){
int valor;
int dig1, dig2, dig3, dig4;
dig1 = keypad.getKey()-'0';
dig2 = keypad.getKey()-'0';
dig3 = keypad.getKey()-'0';
dig4 = keypad.getKey()-'0';

valor = ( ( dig1*10 + dig2)*10 + dig3)*10 + dig4;
return (valor);
}

int lerSensor(){
return(digitalRead(pinSensor));
}

void tocar(){
// makes the buzer ring
digitalWrite(pinSirene, HIGH);
}

void stopTocar(){
// stops the buzer from ringing
digitalWrite(pinSirene, LOW);
}

void setup(){
Serial.begin(9600);
Serial.begin(9600); //to tests
pinMode(pinSensor, INPUT);
pinMode(pinSirene, OUTPUT);
}

void loop(){
char key = keypad.getKey();

if (key){
Serial.println(key);

codigo=lerCodigo();
if(codigo==secreto){
//activar alarme
delay(tempoParaSair);
while(!lerSensor()); //to enter home activates the alarm
delay(tempoParaEntrar);

do {
codigo=lerCodigo();
if(codigo==secreto){
stopTocar();
} else{
tocar();
}
} while(codigo!=secreto);
}
}
}

 

0 comments

Please sign in to leave a comment.