Below is a description of what the Arduino should do, however i found that after the first loop in main, the pins are not activated again:
The mainRoutine function retrieves the current time from the RTC. If the current hour is between 6AM and 5PM, it triggers each relay module in sequence, which in turn controls the electric valves. If the current day is an odd day, it pauses for 4 hours before triggering the relay connected to pin8 again. After all the relay modules have been triggered, it calculates the delay time until the next 6AM and waits for that duration.
Below is the logic implementation:
#include <Wire.h>
#include "RTClib.h"
RTC_DS1307 rtc;
const int dayStart = 6;
const int dayEnd = 17;
const int pin8 = 8;
const int pin7 = 7;
const int pin6 = 6;
const int pin5 = 5;
const int pin4 = 4;
const unsigned long pinActiveTime = 420000;
unsigned long shutDown;
void setup() {
Wire.begin();
Serial.begin(9600);
pinMode(pin8, OUTPUT);
pinMode(pin7, OUTPUT);
pinMode(pin6, OUTPUT);
pinMode(pin5, OUTPUT);
pinMode(pin4, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
if (!rtc.begin()) {
blinkLED(10);
while (1);
}
}
void loop() {
//rtc.adjust(DateTime(__DATE__, __TIME__));
mainRoutine();
delay(60000);
}
void mainRoutine(){
DateTime now = rtc.now();
unsigned long delayTime;
if (now.hour() >= dayStart && now.hour() < dayEnd) {
activatePin(pin8);
activatePin(pin7);
activatePin(pin6);
activatePin(pin5);
activatePin(pin4);
if (evenDAY(now.day()) == false){
delay(14400000);
activatePin(pin8);
}
}
delayTime = computeDelay(now.hour(),now.minute(),now.second());
delay(delayTime);
}
bool evenDAY(int day){
return day % 2 ==0;
}
void activatePin(int pin) {
digitalWrite(pin, HIGH);
delay(pinActiveTime);
digitalWrite(pin, LOW);
}
unsigned long computeDelay(int currentHour, int currentMinutes, int currentSeconds) {
if (currentHour >= dayStart ){
shutDown = ((24 - currentHour + dayStart)* 3600 - currentMinutes * 60 - currentSeconds)*1000;
}
else{
shutDown = ((dayStart - currentHour)* 3600 - currentMinutes * 60 - currentSeconds)*1000;
}
return shutDown;
}
void blinkLED(int times) {
for (int i = 0; i < times; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
}
I have a small project that requires the Arduino to activate one pin at a time, starting at 6:00 AM, for a specified duration. The activation of these pins should follow a specific logic, which I have previously described.
At present, in order to irrigate my crops, I have to manually press a small red button on the Arduino board. Once this button is pressed, the Arduino successfully executes the aforementioned logic. I am seeking a solution that would automate this process and eliminate the need for manual intervention.