I've been having some struggles with the code part for an alarm system using Arduino Uno in TinkerCad. It's the first time using this programming language, so I'm still getting the hang of it. In this project, I have to create an alarm system that only goes off when someone breaks in the house through the door or the window, and it can only work at night (that's why there 2 buttons in the circuit, one for the door and the other for the window). And as soon as I press one of the buttons, the alarm sound and the LED have to activate and stay activated for 30 seconds straight.
About the circuit itself, everything is in the right place, but I'm having some troubles with the code, mainly with the LCD. The messages on the LCD are overlapping one another. And, sometimes, the message starts to lag and blink constantly. And I think I did something stupid because the sound is not going off when I press the button at night anymore, and neither the LED is working properly.
Also, I would to add a line of code so that I can chose the distance in which the sensor will make the alarm go off. Thanks already for the help and sorry to bother!
The code is down below, and the variable names are in Portuguese, so I'll put a translation to make it easier to understand. And I added the picture of the circuit as well. Translation:
botaoAtivarJanelaPin = buttonActiveWindowPin
botaoAtivarPortaPin = buttonActiveDoorPin
fotoresistorPin = photoresistorPin
alarmeAtivado = alarmActivated
sensorLuz = sensorLight
Sistema desligado = System turned off
Sistema ligado = System turned on
Janela Aberta = Window Opened
Porta Aberta = Door Opened
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
const int alarmePiezoPin = 3;
const int ledPin = 4;
const int botaoAtivarJanelaPin = 5;
const int botaoAtivarPortaPin = 6;
const int fotoresistorPin = A0;
const int sensorPirPin = A2;
int sensorState = LOW;
int ledState = LOW;
int alarmeAtivado = 0;
int sensorLuz = 0; //
void setup() {
pinMode(alarmePiezoPin, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(sensorPirPin, INPUT);
pinMode(botaoAtivarJanelaPin, INPUT_PULLUP);
pinMode(botaoAtivarPortaPin, INPUT_PULLUP);
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Sistema desligado");
}
void loop() {
sensorState = digitalRead(sensorPirPin);
sensorLuz = analogRead(fotoresistorPin);
if(sensorLuz < 500){
alarmeAtivado = 0;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Sistema desligado");
} else {
lcd.setCursor(0, 0);
lcd.print("Sistema Ligado ");
}
if ((digitalRead(botaoAtivarPortaPin) == LOW)) {
alarmeAtivado = 1;
lcd.setCursor(0, 1);
lcd.print("PORTA ABERTA! ");
}
if ((digitalRead(botaoAtivarJanelaPin) == LOW)) {
alarmeAtivado = 1;
lcd.setCursor(0, 1);
lcd.print("JANELA ABERTA!");
}
if(sensorLuz < 500){
if ((sensorState == HIGH) || (alarmeAtivado == 1)) {
ativarAlarme();
}
}
delay(100);
}
void ativarAlarme(){
tone(alarmePiezoPin, 2000);
digitalWrite(ledPin, HIGH);
delay(500);
noTone(alarmePiezoPin);
digitalWrite(ledPin, LOW);
}
