Shop OBEX P1 Docs P2 Docs Learn Events
Multiple time periods in SPIN — Parallax Forums

Multiple time periods in SPIN

Is there an easy way to do different tasks at different rates? I mean I want to display time info from a RTC every second but don't need to update room temperature but every few minutes. A few other things may need 1 minute updates or even only every 10 minutes etc.

I have a second counter (don't confuse the ms name with milliseconds) that I have used like this
if (ms==600) OR (ms==1200) OR (ms==1800) OR (ms==2400) OR (ms==3000) OR (ms==3600){
            } OR (ms==4200) OR (ms==4800) OR (ms==5400) OR (ms==6000) OR (ms==7198)     
            gust:=0    ' every 10 minutes            'counter counts to 7200 and then resets to 0
But that gets long to write.

I've also used IF statements with the RTC like this
 IF  (timbuf[hours]==04) AND (timbuf[minutes]==03) AND (timbuf[seconds]<10)
          MaxWind := 0   'reset maxWind and HI/LO at 4:03 AM each day
But some times those happen multiple times or miss the intended time if the RTC is in its own COG. Results in needing flags.

There must be an easier way!! I hope to learn from the experts.

Thanks
Aaron

Comments

  • AribaAriba Posts: 2,690
        if ms // 600 == 0    ' every 10 minutes
            gust := 0
    
  • JonnyMacJonnyMac Posts: 9,159
    edited 2020-10-27 03:38
    I do that with my time object; I use one object for each element that needs its own period. Since it counts up, I set it with negative time then check for 0 as an indication of "ready."
    pub main
    
      repeat
        check_rtc
        check_sensor
    
    
    pub check_rtc
    
      if (rtctimer.millis < 0)
        return
      
      rtctimer.adjust(-1000)
    
      ' handle the rtc here
    
    
    pub check_sensor
    
      if (snsrtimer.millis < 0)
        return
      
      snsrtimer.adjust(-30_000) { 5 minutes }
    
      ' handle the sensor here
    
  • Thank you
    I knew you guys would come through!
    Aaron
Sign In or Register to comment.