include <ThreeWire.h>
#include <RtcDS1302.h>
#define countof(a)(sizeof(a) / sizeof(a[0]))
#define time1 5000
#define motor 3
#define potenciometro A0
//A0 --> Analogo giro
//8,7,6 --> RTC
//3 --> Transistor
//2 --> Boton
int hora;
int minutos;
int segundos;
unsigned long tiempoMotor;
ThreeWire myWire(7, 6, 8); // IO, SCLK, CE
// CLK, DAT, RST
RtcDS1302<ThreeWire> Rtc(myWire);
void setup() {
//Water pump
pinMode(motor, OUTPUT);
digitalWrite(motor, LOW);
// Module RTC
Serial.begin(57600);
Serial.print("compiled: ");
Serial.print(__DATE__);
Serial.println(__TIME__);
Rtc.Begin();
RtcDateTime compiled = RtcDateTime(__DATE__, __TIME__);
printDateTime(compiled);
Serial.println();
if (!Rtc.IsDateTimeValid())
{
// Common Causes:
// 1) first time you ran and the device wasn't running yet
// 2) the battery on the device is low or even missing
Serial.println("RTC lost confidence in the DateTime!");
Rtc.SetDateTime(compiled);
}
if (Rtc.GetIsWriteProtected())
{
Serial.println("RTC was write protected, enabling writing now");
Rtc.SetIsWriteProtected(false);
}
if (!Rtc.GetIsRunning())
{
Serial.println("RTC was not actively running, starting now");
Rtc.SetIsRunning(true);
}
RtcDateTime now = Rtc.GetDateTime();
if (now < compiled)
{
Serial.println("RTC is older than compile time! (Updating DateTime)");
Rtc.SetDateTime(compiled);
}
else if (now > compiled)
{
Serial.println("RTC is newer than compile time. (this is expected)");
}
else if (now == compiled)
{
Serial.println("RTC is the same as compile time! (not expected but all is fine)");
}
}
void loop() {
// Module RTC
RtcDateTime now = Rtc.GetDateTime();
printDateTime(now);
Serial.println();
if (!now.IsValid()) {
// Common Causes:
// 1) the battery on the device is low or even missing and the power line was disconnected
Serial.println("RTC lost confidence in the DateTime!");
}
delay (1000);
// config time
segundos = now.Second();
minutos = now.Minute();
hora = now.Hour();
/*
if((hora%2 == 0)and minutos == 0 and segundos == 0){
if(hora <=0 and hora > 4){
activarMotor();
}
}
*/
// this is a test
if (hora == 1 and minutos == 49 and segundos == 0 ){
activarMotor();
}
//config Analog
int ajusteAnalogo = analogRead(potenciometro);
if (ajusteAnalogo >= 0 and ajusteAnalogo <= 170) {
tiempoMotor = 5000;
Serial.println("El agua circulara durante 5 minutos");
} if (ajusteAnalogo >= 171 and ajusteAnalogo <= 341) {
tiempoMotor = 600000;
Serial.println("El agua circulara durante 10 minutos");
} if (ajusteAnalogo >= 342 and ajusteAnalogo <= 512) {
tiempoMotor = 900000;
Serial.println("El agua circulara durante 15 minutos");
} if (ajusteAnalogo >= 513 and ajusteAnalogo <= 683 ) {
tiempoMotor = 1200000;
Serial.println("El agua circulara durante 20 minutos");
} if (ajusteAnalogo >= 684 and ajusteAnalogo <= 854) {
tiempoMotor = 1500000;
Serial.println("El agua circulara durante 25 minutos");
} if (ajusteAnalogo >= 855 and ajusteAnalogo <= 1025) {
tiempoMotor = 1800000;
Serial.println("El agua circulara durante 30 minutos");
}
}
void activarMotor() {
for (uint8_t i = 0; i < 100; i++) {// arranque suave
analogWrite(motor, i); // pwm de 0-255
}
analogWrite(motor, 100); // pwm de 0-255
Serial.println("Motor Activo");
delay(tiempoMotor);
digitalWrite(motor, LOW);
}
void printDateTime(const RtcDateTime& dt)
{
char datestring[20];
snprintf_P(datestring,
countof(datestring),
PSTR("%02u/%02u/%04u %02u:%02u:%02u"),
dt.Month(),
dt.Day(),
dt.Year(),
dt.Hour(),
dt.Minute(),
dt.Second() );
Serial.print(datestring);
}