Shop OBEX P1 Docs P2 Docs Learn Events
looking for code advice...Am i doing this right? — Parallax Forums

looking for code advice...Am i doing this right?

'in earlier spot in code
cntr := (RTC.ReadTimeReg(2)) 

PUB HourlyMeter 
 
  IF  cntr <> (RTC.ReadTimeReg(2))
    slot[cntr] := volume
    volume := 0
    IF cntr < 23
      cntr ++
    else cntr := 0

RTC.ReadTimeReg(2) returns the hours
cntr is a byte variable
volume is a byte variable
Slot[23] is also a variable.

what I am trying to accomplish is each time the hour changes, I want to write the value of volume (this is a value that cumulates as the rain gauge tips) to its appropriate slot variable so that at the end of the day I can do a dump to my sms (in different section off code)

does it look right?

Comments

  • You have 24 hours, 0 to 23. But slot is only dimensioned as 23 elements.

    John Abshier
  • isn't there a slot[0] ?
  • I am assuming this is your code:

    VAR
    LONG slot[23]

    This makes an array of 23 elements from slot[0] to slot[22]

    John Abshier
  • oh. I was unaware. I thought it would be 0 - 23. makes sense.

    thx
  • I try to use variable names that make things self-documenting -- after reading your description I would have done it like this:
    pub program_setup
    
      lastHr := -1
    
    
    pub hourly_meter | thisHr
    
      thisHr := rtc.readtimereg(2) 
    
      if (thisHr <> lastHr)
        slot[thisHr] := rainAcc
        rainAcc := 0
        lastHr := thisHr
    
Sign In or Register to comment.