Shop OBEX P1 Docs P2 Docs Learn Events
If statement sets value to 274... should set to 0 — Parallax Forums

If statement sets value to 274... should set to 0

realolman1realolman1 Posts: 55
edited 2017-01-14 01:28 in Propeller 1
I have a 1 second timing loop that inserts a value into an array 1 per second.
The first time through it works ok, but when it reaches 1441, it should reset to the array element to 0 ... instead it resets to about 274 or so... I'm not sure exactly.
AnalogRecord[RecordCount++]:=value   'here to fill array element 1/sec
    
    'Should reset to 0 HERE ... resets to about 274????
    
    if RecordCount==1441
       RecordCount:=0

So the first 273 or so elements never change value, but stay the same as what they were the first time through... all the rest are replaced with the correct values

What is wrong?

thank you

Comments

  • SapphireSapphire Posts: 496
    edited 2017-01-14 02:04
    The problem is RecordCount goes to 1441 before resetting to zero, meaning you are writing 1441 records (0-1440) but your AnalogRecord array is only 1440 records big. So the last write is going to the next variable, which is RecordCount.

    You need to change the code to:
    if RecordCount == NumberRecords
    
    because that is already past the end of the array (0-1439).
  • JonnyMacJonnyMac Posts: 9,104
    edited 2017-01-14 05:59
    Yep... there are 1440 minutes in a day, so your index should be 0..1439
    pub second_timing | t
    
      dira[17] := 1
    
      t := cnt     
      repeat
        waitcnt(t += clkfreq)
        !outa[17]
    
        analogrecord[recordcount] := value
    
        if (++recordcount == 1440)
          recordcount := 0
    
        if (++scs == 60)
          scs := 0
          if (++mns == 60)
            mns := 0
            if (++hrs == 24)
              hrs := 0
    

    BTW... you should used a named constant for pin 17 instead of a literal as you're doing.
  • Thank you very much... I think I understand what you are saying.


    the LED is just so I can tell if the thing is running the program... I had trouble with resets when plugging and unplugging the serial cord ( which I understand now ) .. but thanks for the tip.

    You guys don't miss much do you?

    thanks again
  • realolman1realolman1 Posts: 55
    edited 2017-01-14 15:42
    Hey that worked... but of course you both knew it would. That was a bone head trick on my part. 0 being one of the set of things... like the number of possibilities in a number of bits.... always did "get" me

    And sapphire...
    if RecordCount == NumberRecords
    
    ... absolutely the way to do it

    thank you both very much
Sign In or Register to comment.