1

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);
}

Screenshot of the system

3
  • Consider using lcd.clear() when you want to remove what's on the screen to print something new. That way the old characters won't be left there looking like things overlapped. Commented Apr 14, 2024 at 3:00
  • @Delta_G I tried using lcd.clear(), but the overlapping and blinking were still there. Maybe it was the position I put the lcd.clear() before. What would be the correct place to put it? Commented Apr 14, 2024 at 3:26
  • The correct place to put a line of code is sequentially where you want it to run. For me, I would probably clear the screen right before I wrote something new to it. I can't tell you why it didn't work for you because you haven't shared that code. You probably did it wrong. Commented Apr 14, 2024 at 16:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.