hello, I am creating an animal feeder with an motor servo at a certain time defined by the user, but when it rotates 180 degrees it turns off and on the arduino and rotates to 0 degrees.
Does anyone know why they turn off and on the arduino?
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);
#include <DS1302.h>
#include <Wire.h>
#include <Servo.h>
int servoPin = 13;
Time t;
DS1302 rtc(6, 7, 8); //RST,DAT,CLK Pins of the DS1302 Module
//parâmetros de horário que serão atualizados
int horaAtual, minutoAtual, segundoatual;
bool LedCycleDone = false;
//parâmetros primeira alimentação
int horaAlimentacao1 = 16, minutoAlimentacao1 = 17, segundoAlimentacao1 = 30;
//parâmetros segunda alimentação
int horaAlimentacao2 = 16, minutoAlimentacao2 = 17, segundoAlimentacao2 = 0;
Servo Servo1;
void setup()
{
rtc.halt(false);
rtc.writeProtect(false);
lcd.backlight();
lcd.init();
lcd.begin(16, 2);
//rtc.setTime(16, 06, 00); //Hour, Min, Sec
//rtc.setDate(27, 6, 2023); //Day, Month, Year
Servo1.attach(servoPin);
Servo1.write(0);
}
void loop()
{
//determina o horário atual
t = rtc.getTime();
horaAtual = t.hour;
minutoAtual = t.min;
segundoatual = t.sec;
lcd.setCursor(0,1);
lcd.print("Hora:");
lcd.setCursor(5, 1);
lcd.print(rtc.getTimeStr());
lcd.setCursor(0, 0);
lcd.print("Data:");
lcd.setCursor(5,0);
lcd.print(rtc.getDateStr(FORMAT_SHORT, FORMAT_LITTLEENDIAN, '/'));
if(!LedCycleDone) {
if (horaAtual == horaAlimentacao1 && minutoAtual == minutoAlimentacao1 && segundoatual == segundoAlimentacao1) {
Servo1.write(180);
delay(650);
Servo1.write(0);
}
if (horaAtual == horaAlimentacao2 && minutoAtual == minutoAlimentacao2 && segundoatual == segundoAlimentacao2) {
Servo1.write(180);
delay(650);
Servo1.write(0);
}
if (LedCycleDone) {
if (horaAtual != horaAlimentacao1 || minutoAtual != minutoAlimentacao1 || segundoatual != segundoAlimentacao1) {
LedCycleDone = false; // Reset the flag once the timing condition is over
}
if (horaAtual != horaAlimentacao2 || minutoAtual != minutoAlimentacao2 || segundoatual != segundoAlimentacao2) {
LedCycleDone = false; // Reset the flag once the timing condition is over
}
}
}
}