For loop question
Nick Fox
Posts: 4
I have the following for loop but it doesnt seem to increment, the debug statement just displays 0 and then stops. is there something wrong there?
thanks
Nick
FOR speed = 0 TO -250 STEP -10
DEBUG SDEC speed, CR
PWM_REG1 = speed
PWM_REG2 = speed
GOSUB SETDC
NEXT
thanks
Nick
FOR speed = 0 TO -250 STEP -10
DEBUG SDEC speed, CR
PWM_REG1 = speed
PWM_REG2 = speed
GOSUB SETDC
NEXT
Comments
The answer to your question will probably only be revealed if we see what's in the SETDC routine (ergo see the whole program). If there's no RETURN statement in it, that's your answer.
I suppose I should add for completeness, that·your FOR ... NEXT·routine will NEVER increment, it will only DECREMENT, for fairly obvious reasons.
Regards,
Bruce Bates
Post Edited (Bruce Bates) : 11/1/2005 6:45:53 AM GMT
Yes, I should have said decrement. Here is SetDC. I don't think it should have any effect on the for loop.
SETDC:
CMMD = $D0 'SETDC command
ADDR = $01 'MMC default address of "1"
LENG = $04 'Length of SETDC is 4
CKSUM = CMMD+ADDR+LENG+P1HI+P1LO+P2HI+P2LO
SEROUT FM,BAUD,[noparse][[/noparse]CMMD,ADDR,LENG,P1HI,P1LO,P2HI,P2LO,CKSUM]
RETURN
END:
Make the beginning of the program look like that below, and try it again:
DEBUG "Starting"
FOR speed = 0 TO -250 STEP -10
...
Etc
If you see the word "Starting" more than once in any single iteration of the program, the Stamp is being reset. Therein, something is probably drawing down the power supply that is providing power to the Stamp.
Often, if the power source is powering both the Stamp, and some external device, such as a motor or relay, this can happen easily. At that juncture you will need either a beefier power supply, or two power supplies; one for the external device, and one for the Stamp. If you go the latter route, make sure the GROUNDS are COMMON at some point in the overall circuitry.
Regards,
Bruce Bates
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
MIGHT TRY
FOR SPEED = 250 TO 0 STEP -10
REF STAMP WORKS BASIC STAMP PROGRAMMING MANUAL 2.0b PAGE 118-119
SPENCE
Post Edited (SPENCE) : 11/1/2005 4:04:34 PM GMT
·· THAT WON'T WORK UNLESS YOU REMOVE THE NEGATIVE SYMBOL FROM THE STEP VALUE.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Chris Savage
Parallax Tech Support
csavage@parallax.com
Since decrementing the FOR ... NEXT index or counter is perfectly legitimate, the following from the HELP FILE may provide a better understanding of how to accomplish it:
quote
FOR Counter = StartValue TO EndValue {STEP StepValue} ... NEXT
StepValue is an optional variable/constant/expression (0 - 65535) by which the Counter increases or decreases with each iteration through the FOR...NEXT loop. On the BS1, use a minus sign (-) in front of the StepValue to indicate a negative step. On all other BASIC Stamps, if StartValue is larger than EndValue, PBASIC understands StepValue to be negative, even though no minus sign is used.
end quote
Regards,
Bruce Bates
Nick
CASE "b"
DEBUG CR, "Back", CR
IF PWM_REG1 = -250 THEN GOTO ExitB ' in case someone hits back twice
Ramp_Down:
PAUSE 10
PWM_REG1 = PWM_REG1 - 10
PWM_REG2 = PWM_REG2 - 10
GOSUB SETDC
IF PWM_REG1 <> -250 THEN Ramp_Down
ExitB: