SPIN Code for scheduling an event every 10 minutes based upon RTC time?
WBA Consulting
Posts: 2,934
in Propeller 1
I am putting the final touches on a pump controller for a pond and have one last feature I need to add. Currently, it sets the pump speed based upon an array of 24 speeds (one for each hour of the day) and I am simply using the hour to set the speed according to the array. There is a scenario where the pump will change the speed on it's own as part of a backwash process, but after the backwash, the pump always returns to low speed. If this happens at the beginning of an hour that should be running on high, than the pump will run at low for most of the hour. So, the thought is to send the speed every 10 minutes so if a backwash occurred, the pump only runs on low for a max of 10 minutes.
Is there an easy way to divide the "minutes" variable by 10 and if it returns a whole value, trigger an event? Then I am sending the speed every 10 minutes for the corresponding hour.
Is there an easy way to divide the "minutes" variable by 10 and if it returns a whole value, trigger an event? Then I am sending the speed every 10 minutes for the corresponding hour.
Comments
In C: result = minutes % 10
In Spin: result := minutes // 10
If the result is zero, (and previous result was not) trigger the event.
If you want to divide your hour up into six minute intervals, then you can directly use //6 with either the BCD or the flat number. Math fact: If a number is evenly divisible by 6, its BCD representation is also evenly divisible by 6. For example, $48 minutes is 72 in decimal, and both 48 and 72 as decimal are divisible by 6.
My preference is to convert the entire BCD time string to minutes or seconds past midnight, 0 to 1439 in the case of minutes. Then you can modulo that with any convenient factor that divides 1440 evenly. 10 minutes, 6 minutes, 15 minutes, whatever.
Just a thought!
In Spin I have done something similar although simpler. The background cog has a WAITCNT for 1ms and checks a set of timers counting them down if they are set and when they reach zero they can execute an ALARM function etc. You can of course synchronize this to an RTC if need be but that would only need to happen once at start-up and maybe resynch at midnight.
Many of my projects have a background cog that runs at 1ms; this lets me keep a millis value that I can use for differential timing (where cnt would not be convenient). Even in Spin one can do a lot of work in 1ms, so other background processes can be added to the background loop.