Hello, Arduino world! This is my first time on the forum, let me know if I'm doing anything wrong. Thanks!
Anyway, I am building a burglar alarm system with an Arduino UNO, a keypad, 16x2 LCD, buzzer, and relay. I wrote a really long code for it and it does really weird things.
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <math.h>
int buzzer = A4;
int relay = 1; //I've got a normally-closed relay, so if something happens to the arduino or the cable, the bell will go on.
int bcklte = A5; //I wanted to control when the backlight of the LCD is on or off, depending on the time of day I'm working on it!
int num = 0; //Variable about the digit displyed of the password on the LCD
int input = 0; //Composition of all password digits
int cntdn = 40; //Countdown timer
int buzlogic = LOW; //This stuff is for the buzzer-without-delay section of the sketch
long previousMillis = 0;
long interval = 500;
const byte ROWS = 3; //Kind of wonky keypad, sorry!
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'9','8','7','#'},
{'6','5','4'},
{'3','2','1','*'},
};
byte rowPins[ROWS] = {5, 4, 3};
byte colPins[COLS] = {8, 7, 6, A2};
LiquidCrystal lcd(13, 2, 12, 11, 10, 9);
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
pinMode(buzzer, OUTPUT);
pinMode(1, OUTPUT);
digitalWrite(1, HIGH);
pinMode(A5, OUTPUT);
lcd.begin(16, 2);
lcd.noBlink();
lcd.print(" ENTER PASSCODE");
lcd.setCursor(0, 1);
lcd.print("you have secs");
}
void loop(){
if(cntdn > 0) { //here is the buzz-without-delay section
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (buzlogic == LOW) {
buzlogic = HIGH;
}
else {
buzlogic = LOW;
}
digitalWrite(buzzer, buzlogic);
cntdn = cntdn - 1;
lcd.setCursor(9, 1);
lcd.print(cntdn / 2); //you have 20 seconds (40 buzzer revolutions) before relay is supposed to turn off
lcd.print(" ");
}
}
if(cntdn == 0); {
digitalWrite(relay, LOW);
} //Why doesn't this work??? it's supposed to turn the relay off after "cntdn" is 0, or after 20 seconds have passed.
char key = keypad.getKey();
if (key != NO_KEY){
switch (key){
case '#':
case '*':
default: {
input = input + key * pow(10, (3 - num)); //right here "input" displays value of combined digits
lcd.setCursor(num, 1); //it is supposed to output 1000 times the first entered, adds 100 times the second, so on.
lcd.print("*"); //but instead, from Serial.println, it does not do what it's supposed to do.
num = num + 1;
}
}
}
if(num > 3){ //recycles digit of input to 0 after 4 have been pressed
num = 0;
}
switch (key){
case '#': lcd.setCursor(0, 1); lcd.print("you "); num = 0; //# means clear entered digits
case '*': { //* means check selected digits
if(input == 1234) { //comparing the entered password "input" to this number right here
cntdn = -1;
lcd.setCursor(0, 0);
lcd.print(" SUCCESS ");
lcd.setCursor(0, 1);
lcd.print(" ");
digitalWrite(buzzer, LOW);
}
else {
lcd.setCursor(0, 0);
lcd.print("wrong, try again");
lcd.setCursor(0, 1);
lcd.print("you ");
num = 0;
}
}
}
Serial.println(input); //this is just temporary to see how "input" is doing.
delay(1); //to keep Serial.print stable
}
First, the variable "input" does not do what I told it to do. It outputs really large, sometimes negative numbers, and I'm not sure why. Also, I can't figure out how to make the relay turn off after the countdown finishes. As far as I know, it should be right.
I know for a fact that all my hardware works... the LCD, the relay driver, the buzzer, etc. from other sketches that do work. The sketch isn't finished, this is just a step of my project. I'll add a motion detector and arming buttons on the outside, but those aren't included here.
If anyone can help me with those two issues, it'd be greatly appreciated!