#include "RTClib.h"
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 4
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
RTC_DS1307 rtc;
int val1 = 0;
int val2 = 0;
int iter = 0;
uint8_t tempo_detec, tempo_nao_detec;
uint8_t tempo_ult_detec;
int minuto_ult_detec = 0;
int hora_ult_detec = 0;
int i = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
lcd.init();
lcd.backlight();
if (! rtc.begin()) {
Serial.println("RTC não identificado!");
Serial.flush();
abort();
}
pinMode(13, OUTPUT); // Saída do LED
pinMode(12, OUTPUT); // Buzzer
pinMode(11, INPUT); // PIR 1
pinMode(10, INPUT); // PIR 2
}
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
lcd.setCursor(0,0);
lcd.print("Hora atual: ");
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
lcd.setCursor(0,1);
lcd.print("PIR 1: ");
lcd.setCursor(10,1);
lcd.print("PIR 2: ");
lcd.setCursor(0,2);
lcd.print("Qtd. de detc.: ");
lcd.setCursor(15,2);
lcd.print(iter);
val1 = digitalRead(11);
if (val1 == HIGH) {
digitalWrite(13, HIGH);
lcd.setCursor(7,1);
lcd.print("D "); // Indica que foi detectado movimento em PIR1
tone(12, 2000); // Buzzer irá acionar durante os 5 segundos que o sensor PIR 1 está ativado
iter++;
delay(4000);
tempo_detec = now.unixtime();
lcd.clear();
}
else{
digitalWrite(13, LOW);
lcd.setCursor(7,1);
lcd.print("N "); // Indica que foi não detectado movimento em PIR1
noTone(12);
}
val2 = digitalRead(10);
if (val2 == HIGH) {
digitalWrite(13, HIGH);
lcd.setCursor(17,1);
lcd.print("D "); // Indica que foi detectado movimento em PIR2
tone(12, 2000); // Buzzer irá acionar durante os 5 segundos que o sensor PIR 2 está ativado
iter++;
delay(4000);
tempo_detec = now.unixtime();
lcd.clear();
}
else{
digitalWrite(13, LOW);
lcd.setCursor(17,1);
lcd.print("N "); // Indica que foi não detectado movimento em PIR2
noTone(12);
}
if (iter > 0)
{
tempo_nao_detec = now.unixtime();
tempo_ult_detec = tempo_nao_detec - tempo_detec - i*60;
if (tempo_ult_detec == 59)
{
minuto_ult_detec++;
i++;
lcd.clear();
}
if (minuto_ult_detec == 59)
{
hora_ult_detec++;
minuto_ult_detec = 0;
lcd.clear();
}
lcd.setCursor(0,3);
lcd.print("Tempo: "); // Indica o tempo decorrido desde a última detecção
lcd.print(hora_ult_detec, DEC);
lcd.print(":");
lcd.print(minuto_ult_detec, DEC);
lcd.print(":");
lcd.print(tempo_ult_detec, DEC);
lcd.print(" ");
}
delay(1000);
}