Shop OBEX P1 Docs P2 Docs Learn Events
"If "condition not working — Parallax Forums

"If "condition not working

realolman1realolman1 Posts: 55
edited 2016-12-15 14:25 in Propeller 1
I have a spin program that works

It contains a loop to increment seconds which works:
Pub SecondTiming
  
   repeat
     waitcnt (clkfreq + cnt)                  ' one second wait
     sec:= sec +1                             ' add one to seconds

As soon as I add an "If" condition to the repeat loop, "sec" is always zero:
Pub SecondTiming
  
   repeat
     waitcnt (clkfreq + cnt)                  ' one second wait
     sec:= sec +1                             ' add one to seconds
     If sec >= MinuteHasPassed                 'check second count against a minute
       AnalogRecord[ElapsedMinutes]:= value    'first iteration will be 0 
       sec:= 0                                 'reset seconds to 0
       ElapsedMinutes:= ElapsedMinutes +1       'increment elapsed minutes  


The "If" command is to compare the "sec" count to 60 for a minute passing.
MinuteHasPassed is a CON set to 60. I have tried making it a VAR and even tried comparing "sec" to 60 in the "If" command .

I have tried different indentations on the "IF" and in the "IF"

It seems that every time the "If" condition evaluates, it thinks "sec" is larger than or equal to what should be 60.
Either that, or it is only doing the repeat once, and never adding 1 to the "sec"

all I have to do is comment out the "If" command with its statements, and the "sec" increments properly

What I am trying to do is add a value to an array every 60 seconds. The code compiles and downloads to the propeller, and everything is happy... except me

What am I doing wrong?

Edit: I added another variable to the "repeat" loop that does not get modified anywhere, just incremented, and it works OK, ... so for some reason, it seems that the "If" statement is always true. I don't see how that could be possible.

I must have something wrong with the syntax of it. If the "If" is always true, ElapsedMinutes should be getting incremented, and it is not
Pub SecondTiming
  
   repeat
     waitcnt (clkfreq + cnt)                  ' one second wait
     sec:= sec +1                                ' add one to seconds
     CountLoops :=CountLoops+1       'count loop iterations       
     If sec >= MinuteHasPassed           'check second count against a minute
       AnalogRecord[ElapsedMinutes]:= value    'first iteration will be 0 
       sec:= 0                                 'reset seconds to 0
       ElapsedMinutes:= ElapsedMinutes +1       'increment elapsed minutes  
       
The problem here is: sec is always zero and so is ElapsedMinutes, but CountLoops increments... ???

Comments

  • Switch to "=>". ">=" is an assignment operator. Think of ">=" as being the same as "sec = sec > MinuteHasPassed".
  • Good Lord!... that was it.

    thank you very much.

    Only good thing that comes from being driven nuts about something like this is I bet I will never forget it.

    Thank you
  • @realolman, In a timekeeping application the 'add' assignment operator takes the overhead into account to help keep your time accurate.
    +=
    
    You can find a great timekeeping tutorial in the Propeller tool by clicking HELP, selecting the Propeller Education Kit (pdf)... on page 64.
  • thank you
    I will look it up
  • JonnyMacJonnyMac Posts: 9,104
    edited 2016-12-16 01:49
    To get precise timing you need to synchronize the loop so that the overhead is accounted for -- like this:
    pub second_timing | t
    
      t := cnt                                                      ' sync timing to cnt
      repeat
        waitcnt(t += clkfreq)                                       ' wait for 1s to elapse
        ++countloops                                                ' update loop count
        if (++sec == 60)                                            ' one minute passed?
          sec := 0                                                  ' reset seconds counter
          analogrecord[elapsedminutes++] := value                   ' add value to array
    
    So long as your loop code comes in just under one second (to allow for other loop overhead), the loop will run exactly once per second.
  • thank you all

Sign In or Register to comment.