BS2sx Loop optimizations? Different code behaviour.
Donivan
Posts: 23
I was writing some BS2 code last night, and noticed some pronounced differences with seemingly identical code.· The code was to make an LED slowly dim and used the PWM command.· The first code snippet worked, but went too fast.· That is, you don't really see the LED dim, just dims quickly then turns off.· The second (longer, less efficient) code snippet worked OK.· If someone could explain why, that would be great. ·Here they are:
'eyes is defined as pin 4
'
'this does not work well:
FOR tempByte = 20 TO 0 STEP -5
·· FOR temp = 1 TO 20
····· PWM eyes,tempByte,10
·· NEXT
NEXT
'******************
'but moving things to separate
'loops works very well:
FOR temp = 1 TO·20
·· PWM eyes,20,10
NEXT
FOR temp = 1 TO·20
·· PWM eyes,15,10
NEXT
FOR temp = 1 TO·15
· PWM eyes,10,10
NEXT
FOR temp = 1 TO 15
·· PWM eyes,5,10
NEXT
FOR temp = 1 TO 15
·· PWM eyes,1,10
NEXT
'*****************
I realize the last loop won't get executed in the first example, but despite this the difference is very noticable.
Thanks,
Donivan
·
'eyes is defined as pin 4
'
'this does not work well:
FOR tempByte = 20 TO 0 STEP -5
·· FOR temp = 1 TO 20
····· PWM eyes,tempByte,10
·· NEXT
NEXT
'******************
'but moving things to separate
'loops works very well:
FOR temp = 1 TO·20
·· PWM eyes,20,10
NEXT
FOR temp = 1 TO·20
·· PWM eyes,15,10
NEXT
FOR temp = 1 TO·15
· PWM eyes,10,10
NEXT
FOR temp = 1 TO 15
·· PWM eyes,5,10
NEXT
FOR temp = 1 TO 15
·· PWM eyes,1,10
NEXT
'*****************
I realize the last loop won't get executed in the first example, but despite this the difference is very noticable.
Thanks,
Donivan
·
Comments
FOR tempByte = 20 TO 0 STEP 5
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon Williams
Applications Engineer, Parallax
Dallas Office