Hi everyone
I decided to dig out an Arduino Pro Mini for a little plant misting project.
I found a great little programmable garden water timer that lets me adjust sprays down to the second, I need 15-second sprays, so far so good, but it's programmed for 1 hour, and that's it, so it sprays all night and soaks everything every hour, so it's no good, however, its battery powered and uses a 3volt water solenoid switch, so I`m going to be using that.
I haven't built it yet but wanted some advice on the code, I need the unit to be battery powered so I will use a 18650 2000mAH LiPo battery to provide the power, I have modified the Pro Mini by removing the LED and replaced the voltage regulator with a low leakage one to get the lowest hardware power consumption, also will be using the lowpower library to put the Pro Mini to sleep, I have DS3231 RTC to keep the time accurate, and a BME280 sensor for humidity.
So the idea is that the unit goes to sleep and wakes up every 8 seconds to check the time and humidity reading, if the humidity is low enough and one of the alarm times is correct then the sprayer will run for 15 seconds and then turn off and the system go back to sleep.
That's all I need it to do, however as I finished the code I realized a big error, as the system wakes up every 8 seconds it could miss its alarm time, therefore it won't do the spraying!
If I was not using the sleep function I see no problem with the code, but with the sleep running, I cant see how to get this going, maybe I need a different method?.
Any ideas are greatly appreciated.
Many thanks in advance Alan
#include <LowPower.h>
#include <TimeAlarms.h> // System Libraries
#include <TimeLib.h>
#include <Wire.h>
#include "RTClib.h"
#include "Seeed_BME280.h"
#define Solenoid 2 // Device Setups
BME280 bme280;
RTC_DS3231 rtc;
time_t syncProvider() // RTC to system time
{
return rtc.now().unixtime();
}
void setup() {
Serial.begin(9600); Wire.begin(); rtc.begin(); // Devices Start
//rtc.adjust(DateTime(2019, 12, 18, 12, 26, 00));
setSyncProvider(syncProvider); delay(1000); Serial.println(); // Confirm system time is synced with RTC
if (timeStatus() != timeSet)
Serial.println("Unable to sync with the RTC");
else
Serial.println("RTC has set the system time");
bme280.init(); // Start Sensor
delay(100);
pinMode(Solenoid, OUTPUT); digitalWrite(Solenoid, LOW); // Setup Solenoid
// alarms, to trigger at specific times ------------------// // Setup Solenoid start and stop times
Alarm.alarmRepeat(9, 00, 00, StartAlarm); Alarm.alarmRepeat(9, 00, 15, StopAlarm);
Alarm.alarmRepeat(9, 30, 00, StartAlarm); Alarm.alarmRepeat(9, 30, 15, StopAlarm);
Alarm.alarmRepeat(10, 00, 00, StartAlarm); Alarm.alarmRepeat(10, 00, 15, StopAlarm);
Alarm.alarmRepeat(10, 30, 00, StartAlarm); Alarm.alarmRepeat(10, 30, 15, StopAlarm);
Alarm.alarmRepeat(11, 00, 00, StartAlarm); Alarm.alarmRepeat(11, 00, 15, StopAlarm);
Alarm.alarmRepeat(11, 30, 00, StartAlarm); Alarm.alarmRepeat(11, 30, 15, StopAlarm);
Alarm.alarmRepeat(12, 00, 00, StartAlarm); Alarm.alarmRepeat(12, 00, 15, StopAlarm);
Alarm.alarmRepeat(12, 30, 00, StartAlarm); Alarm.alarmRepeat(12, 30, 15, StopAlarm);
Alarm.alarmRepeat(13, 00, 00, StartAlarm); Alarm.alarmRepeat(13, 00, 15, StopAlarm);
Alarm.alarmRepeat(13, 30, 00, StartAlarm); Alarm.alarmRepeat(13, 30, 15, StopAlarm);
Alarm.alarmRepeat(14, 00, 00, StartAlarm); Alarm.alarmRepeat(14, 00, 15, StopAlarm);
Alarm.alarmRepeat(14, 30, 00, StartAlarm); Alarm.alarmRepeat(14, 30, 15, StopAlarm);
Alarm.alarmRepeat(15, 00, 00, StartAlarm); Alarm.alarmRepeat(15, 00, 15, StopAlarm);
Alarm.alarmRepeat(15, 30, 00, StartAlarm); Alarm.alarmRepeat(15, 30, 15, StopAlarm);
Alarm.alarmRepeat(16, 00, 00, StartAlarm); Alarm.alarmRepeat(16, 00, 15, StopAlarm);
Alarm.alarmRepeat(16, 30, 00, StartAlarm); Alarm.alarmRepeat(16, 30, 15, StopAlarm);
Alarm.alarmRepeat(17, 00, 00, StartAlarm); Alarm.alarmRepeat(17, 00, 15, StopAlarm);
Alarm.alarmRepeat(17, 30, 00, StartAlarm); Alarm.alarmRepeat(17, 30, 15, StopAlarm);
}
void loop() {
Alarm.delay(1000);
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
void StartAlarm() // Start Solenoid
{
if (bme280.getHumidity() < 60) {
digitalWrite(Solenoid, HIGH);
delay(100);
}
else {
digitalWrite(Solenoid, LOW);
delay(100);
}
}
void StopAlarm() // Stop Solenoid
{
digitalWrite(Solenoid, LOW);
delay(100);
}