Hi, I am new to Aduino, and have only little programming experience.
I am using a Nano clone, with a DS1307 RTC.
In my Code below, I have been trying to get the StartWatering function to call with the Alarm.alarmRepeat function. I have been changing the time in the
Alarm.alarmRepeat(15,25,0,StartWatering);
to a minute or two in the future, and recompiling each time I make a change, but each time the StartWatering is not called when the clock reaches the set alarm time.
/*
=======================
Watering system control
=======================
- 5 watering zones garden
- 24VAC solenoid valves
- generic 8 channel relay board
- watering suppressed when raining.
- Manual switch to increase watering during heatwave
*/
#include <Wire.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <DS1307RTC.h>
bool HeatWave = LOW; // initialise for normal weather
const int PlannedHour = 7; //initialise with normal starts at 7am.
int StartHour = PlannedHour; // Working variable for start time
const int BonusStartHour = 20; // If needed a second watering in the evening at 20:00
const int nZones = 5; // five zone watering system.
const int ZoneTime = 1000; // default watering time for each zone (seconds)
int ZoneDuration = ZoneTime;
int PumpPin = 7;
int OnPin = 8;
int RainPin = 9;
int HeatWavePin = 10;
int ZonePins[] = {
2,
3,
4,
5,
6,
};
void setup() {
Serial.begin(9600);
while (!Serial)
; // wait for serial
delay(200);
for (int i = 0; i < nZones; i++) {
pinMode(ZonePins[i], OUTPUT);
digitalWrite(ZonePins[i], HIGH);
}
pinMode(OnPin, INPUT);
pinMode(RainPin, INPUT);
pinMode(HeatWavePin, INPUT);
pinMode(PumpPin, OUTPUT);
digitalWrite(PumpPin, HIGH);
}
void StartWatering() {
if (digitalRead(HeatWavePin) == HIGH) {
ZoneDuration = ZoneTime * 1.5;
} else {
ZoneDuration = ZoneTime;
}
digitalWrite(PumpPin, LOW);
for (int i = 0; i < nZones; i++) {
digitalWrite(ZonePins[i], LOW);
delay(ZoneDuration);
digitalWrite(ZonePins[i], HIGH);
}
digitalWrite(PumpPin, HIGH);
}
void loop() {
tmElements_t tm;
RTC.read(tm);
while (digitalRead(RainPin) == LOW) {
Serial.print(tm.Hour);
Serial.print(":");
Serial.print(tm.Minute);
Serial.print(":");
Serial.println(tm.Second);
Alarm.alarmRepeat(15,25,0,StartWatering);
Alarm.delay(0);
}
}
I have read a number of forum posts on the subject:
- ChatGPT says the syntax is correct, and StartWatering() should be called.
-I have tried adding Alarm.delay(0) both before, and after the alarm line.
-I have tried commenting out all the Serial.Print lines which show me the Aduino's clock time, to avoid it interfering with timing.
I switched out the Alarm for
if (tm.Hour == StartHour || (tm.Hour == BonusStartHour && digitalRead(HeatWavePin) == HIGH)) {
StartWatering();
}
And played with those StartHour variables, and that works, but then I have the problem with the loop starting a second watering cycle, if the total watering time of all zones is less than an hour, hence my desire to use the alarm function.
Thanks in advance for your assistance.
Mark