#include "RTClib.h"
#include <Servo.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;
Servo meuservo;
int pot1, pot2, ang1, ang2, PIR;
long ang_rand1, ang_rand2;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
randomSeed(analogRead(0));
lcd.init();
if (! rtc.begin()) {
Serial.println("RTC não identificado!");
Serial.flush();
abort();
}
pinMode(13, OUTPUT); // Buzzer
pinMode(12, INPUT); // Sensor PIR
}
void loop() {
// put your main code here, to run repeatedly:
DateTime now = rtc.now();
lcd.setCursor(0,0);
lcd.print("Hora atual: "); // Exibindo o horário atual
lcd.print(now.hour(), DEC);
lcd.print(':');
lcd.print(now.minute(), DEC);
lcd.print(':');
lcd.print(now.second(), DEC);
pot1 = analogRead(A0);
ang1 = map(pot1,0,1023,0,90); // Definindo o movimento entre 0 e 90 graus
pot2 = analogRead(A1);
ang2 = map(pot2,0,1023,0,90);
PIR = digitalRead(12); // Define se o sensor PIR está ativado ou não
ang_rand1 = random(0,180); // Gera os ângulos aleatórios para caso da ativação do sensor PIR
ang_rand2 = random(0,180);
if (PIR == LOW)
{
meuservo.attach(11); // Servo 1
meuservo.write(ang1); // Movendo o servo motor para o ângulo relativo ao potenciômetro
delay(100);
meuservo.attach(10); // Servo 2
meuservo.write(ang2);
lcd.setCursor(1,1); // Exibindo os ângulos
lcd.print("Angulo 1:");
lcd.print(ang1,1);
lcd.print(" [graus] ");
lcd.setCursor(1,2);
lcd.print("Angulo 2:");
lcd.print(ang2,1);
lcd.print(" [graus] ");
if(ang1==ang2) // Caso os ângulos forem iguais acender a luz de fundo e acionar o buzzer
{
lcd.backlight();
tone(13,4000);
delay(500);
noTone(13);
lcd.noBacklight();
}
}
else
{
meuservo.attach(11);
meuservo.write(ang_rand1);
delay(100);
meuservo.attach(10);
meuservo.write(ang_rand2);
lcd.setCursor(1,1);
lcd.print("Angulo 1:");
lcd.print(ang_rand1,1);
lcd.print("[graus] ");
lcd.setCursor(1,2);
lcd.print("Angulo 2:");
lcd.print(ang_rand2,1);
lcd.print("[graus] ");
if(ang_rand1==ang_rand2)
{
lcd.backlight();
tone(13,4000);
delay(500);
noTone(13);
lcd.noBacklight();
}
}
delay(500);
}