Shop OBEX P1 Docs P2 Docs Learn Events
Better code in spin 1 — Parallax Forums

Better code in spin 1

There must be a shorter way to write something like this. Help! Thanks Aaron

IFNOT  secs//15
      ReadRTC
    ELSEIFNOT secs//30      'read RTC 4 times per minute
      ReadRTC
      UpdateTemps            'UpdateTemps once per minute
    ELSEIFNOT secs//45
      ReadRTC
    ELSEIFNOT secs//58
      ReadRTC
    ELSEIFNOT secs//75
      ReadRTC
    ELSEIFNOT secs//90
      ReadRTC
      UpdateTemps
    ELSEIFNOT secs//105
      ReadRTC
    ELSEIFNOT secs//120
      ReadRTC
    ELSEIFNOT secs//135
      ReadRTC
    ELSEIFNOT secs//150

Comments

  • JonnyMacJonnyMac Posts: 9,243
    edited 2025-03-19 20:12

    What is your goal? One programmer's thinking is not always obvious to others, especially with uncommented code.

    Why would secs ever be more than 59 if you're reading an RTC?

    The one thing you do indicate is updating temps every minute. An easy way to determine that using an RTC is to look for the seconds to be a specific value (I use 0, but it can be anything between 0 and 59). In order to prevent multiple reads during the 1-second window, I use a variable that compares the current seconds value with the last read; a change indicates a new second.

    This is the check code

      last := -1
    
      repeat
        check_rtc
        if (scs <> last)                                            ' new second?
          last := scs                                               ' mark this read
          term.fstr3(string("\r%.2d:%.2d:%.2d  "), hrs, mns, scs)   ' show current time
          if (scs == 0)                                             ' new minute?
            term.str(string("Update temps"))
    

    This is output from the attached demo.

    Let's say you want to do something every five minutes. I would handle that like this (inside the 'new read' block):

          if (scs == 0) and (mns // 5 == 0)
            term.str(string("5-min process"))
    
Sign In or Register to comment.