I have this snippet of code which works fine. The basic function of the code below is this:
1/ When "(TimeNow % 1800) == 0)" is true which is every 30 minutes it triggers a dosing pump to switch on.
2/ The dosing pump will come on for a period of time.
My query is I want to add a countdown timer from when the pump is off to next on. While the actual code for the countdown is not the issue I am trying to work out the maths as to how much time to count down from because "TimeNow" is not constant. So for example lets say the pump on period is 3000 millis and the pump is activated every 30min, the period NOT active would be 30min - 3000millis so far ok. Now I need to tie this result in with "TimeNow" because there could be a situation the programme is uploaded between the 30 min intervals.
Proietti:
Now I need to tie this result in with "TimeNow" because there could be a situation the programme is uploaded between the 30 min intervals.
it sounds like you'd like to maintain a watering schedule after restarting the program. you would like a way to restart the schedule in an arbitrary amount of time (i.e. 12 minutes from now).
looks like you are using the Serial monitor and can issue a command thru it to do so.
to make things easier, the command should be # of minute from now to start. one way is to reset TimeNow to 1800 - 60 * # minutes from now (e.g. 1080 = 1800 - (12 * 60)).
but isn't there a problem using modulo arithmetic after a rollover at some arbitrary value (65536)? it might be better to have a count down time that is reset after triggering. TimeUntil can be set to 1800 and decremented each sec. when it reaches zero, it trigger the action and is reset to 1800.
after reprogramming, timeUntil would just be the # seconds until the next scheduled waters (e.g. 12 * 60).
Ok let me try and work on that. I am using Serial only for debugging so the serial print you see are my indicators to tell me where my code is.
Lastly I did not know of any problems with using modulo arithmetic. I will do a search.
Proietti:
Lastly I did not know of any problems with using modulo arithmetic.
16 bit values have a max value 65535. If TimeNow is an int, it will iterate up to 64800 (36 * 1800). When 1800 is added to 64800, the max value be exceeded and it will wrap to 1064. But 1064 % 1800 != 0
TimeNow is a time in seconds. To turn something on for 3 seconds every 30 minutes:
const unsigned long MINUTES = 60;
const unsigned DOSE_IN_SECONDS = 3;
if ((TimeNow % (30*MINUTES)) < DOSE_IN_SECONDS)
{
// Turn it on
}
else
{
// Turn it off
}
Ok I managed to get it working. this is the syntax I used for future reference:
void CountDown()
{
Hr = dateTime.hour;
Min = dateTime.minute;
Sec = dateTime.second;
Yr = dateTime.year;
Mth = dateTime.month;
Dy = dateTime.day;
Wk = dateTime.dayofWeek;
TotalSecs = ((Hr*3600) + (Min*60) + Sec); //TIMENOW
unsigned long x = TotalSecs % 1800; //1800 is every 30min
unsigned long y = 1800 - 5; //5 second arbratory on period
unsigned long xy = y - x;
Serial.print("CountDown :");
Serial.println(xy);
Blynk.virtualWrite(V20, xy);
}
Ok I have got the dosers working I just have one issue. There are 4 dosers which operate every 30 minutes so 48 times a day. My initial thinking and what I have used is the MODULO expression as posted by jhonwasser. the issue is:
PUMP 1 - Time % 1800
PUMP 2 - Time % 2250
PUMP 3 - Time % 2700
PUMP 4 - Time % 3150
Basically I wanted a gap of 450 seconds between each pump dose. Now I set this up and noted that at Time 68400 both pumps 1 & 3 come on at the same time because Time%1800 = 0 and Time%2700 = 0.
Can anybody please advise how to solve this issue please.
Proietti:
Ok I have got the dosers working I just have one issue. There are 4 dosers which operate every 30 minutes so 48 times a day. My initial thinking and what I have used is the MODULO expression as posted by jhonwasser. the issue is:
PUMP 1 - Time % 1800
PUMP 2 - Time % 2250
PUMP 3 - Time % 2700
PUMP 4 - Time % 3150
Basically I wanted a gap of 450 seconds between each pump dose. Now I set this up and noted that at Time 68400 both pumps 1 & 3 come on at the same time because Time%1800 = 0 and Time%2700 = 0.
Can anybody please advise how to solve this issue please.
So you want "EAch pump runs every 30 minutes, but pumps are staggered by 7.5 minutes (450 seconds)"?
PUMP 1 - Time % 1800
PUMP 2 - (Time + 450) % 1800
PUMP 3 - (Time + 900) % 1800
PUMP 4 - (Time + 1350) % 1800