Help troubleshoot a push-button program
I've made this push button program for the BS1 (with a help of a few people on this site) except it doesn't work. What I want to achieve is the following sequence:
1 - I push a button
2 - a stepper motor goes clockwise one revolution
3 - I push a button OR there's a pause of 2 minutes
4 - the stepper motor goes COUNTER clockwise one revolution
5 - the program starts from the beginning
Program:
' {$STAMP BS1}
' {$PBASIC 1.0}
SYMBOL StpsPerRev = 48 ' whole steps per rev
SYMBOL idx = B2 ' loop counter
SYMBOL phase = B3 ' new phase data
SYMBOL stpIdx = B4 ' step pointer
SYMBOL stpDelay = B5 ' delay for speed control
Full_Steps:
EEPROM 0, (%00110000, %01100000, %11000000, %10010000)
Setup:
DIRS = %11110010 ' make P1, P4-P7 outputs
stpDelay = 15 ' set step delay
Setup2:
IF PIN1 = 1 THEN CW
GOTO Setup2
CW:
FOR idx = 1 TO StpsPerRev ' one revolution
GOSUB Step_Fwd ' rotate clockwise
GOTO Setup3:
Setup3:
IF PIN1 = 1 THEN CCW
IF PIN1 = 0 THEN PAUSE 120000 AND THEN CCW
GOTO Setup3
CCW:
FOR idx = 1 TO StpsPerRev ' one revolution
GOSUB Step_Rev ' rotate counter-clockwise
GOTO Setup1
END
Step_Fwd:
stpIdx = stpIdx + 1 // 4 ' point to next step
GOTO Do_Step
Step_Rev:
stpIdx = stpIdx + 3 // 4 ' point to previous step
GOTO Do_Step
Do_Step:
READ stpIdx, phase ' read new phase data
PINS = PINS & %00001111 | phase ' update stepper pins
PAUSE stpDelay ' pause between steps
RETURN
1 - I push a button
2 - a stepper motor goes clockwise one revolution
3 - I push a button OR there's a pause of 2 minutes
4 - the stepper motor goes COUNTER clockwise one revolution
5 - the program starts from the beginning
Program:
' {$STAMP BS1}
' {$PBASIC 1.0}
SYMBOL StpsPerRev = 48 ' whole steps per rev
SYMBOL idx = B2 ' loop counter
SYMBOL phase = B3 ' new phase data
SYMBOL stpIdx = B4 ' step pointer
SYMBOL stpDelay = B5 ' delay for speed control
Full_Steps:
EEPROM 0, (%00110000, %01100000, %11000000, %10010000)
Setup:
DIRS = %11110010 ' make P1, P4-P7 outputs
stpDelay = 15 ' set step delay
Setup2:
IF PIN1 = 1 THEN CW
GOTO Setup2
CW:
FOR idx = 1 TO StpsPerRev ' one revolution
GOSUB Step_Fwd ' rotate clockwise
GOTO Setup3:
Setup3:
IF PIN1 = 1 THEN CCW
IF PIN1 = 0 THEN PAUSE 120000 AND THEN CCW
GOTO Setup3
CCW:
FOR idx = 1 TO StpsPerRev ' one revolution
GOSUB Step_Rev ' rotate counter-clockwise
GOTO Setup1
END
Step_Fwd:
stpIdx = stpIdx + 1 // 4 ' point to next step
GOTO Do_Step
Step_Rev:
stpIdx = stpIdx + 3 // 4 ' point to previous step
GOTO Do_Step
Do_Step:
READ stpIdx, phase ' read new phase data
PINS = PINS & %00001111 | phase ' update stepper pins
PAUSE stpDelay ' pause between steps
RETURN
Comments
1) Every FOR needs a matching NEXT
2) Use GOTO Setup3 rather than GOTO Setup3:
3) There's no such thing as AND THEN. If you want two statement to be controlled by an IF, use IF ... THEN and ENDIF (see the manual for details).
4) Usually in handling a pushbutton, you test for it being pushed and go to some routine.
The first thing there is a loop that waits for the pushbutton to be released.
In this routine:
Setup3:
IF PIN1 = 1 THEN CCW
IF PIN1 = 0 THEN PAUSE 120000 AND THEN CCW
GOTO Setup3
I think you are wanting this to repeatedly check to see if the button has been pressed. The way it is written it will only check once, then do the PAUSE. Unless the button was pressed (and still held) prior to reaching the "IF PIN1=1..." line it will never see it and go directly to the next line (which isn't proper code, as Mike already stated).
As for this...
GOTO Setup1
There is no setup1 to go to. Setup2 is probably the correct destination.
Oh, I see what you mean. I definitely need to correct that. Any suggestions if I want to continuously check for the input while still pausing for the allowable durations of 2 minutes?