[code];The program initializes setting a DC motor and a servo at a zero position and speed.
;The program loops until a button is pressed.
;Once the button is pressed, the board ramps up the speed of a DC motor and then holds the speed until a second button is pressed
;A servo also begins moving along with the DC motor when the first button is pressed and stops moving when the second button is pressed
ENABLEHSERVO ;Enable the hservo command
workspace1 var byte ;provide holding variable for button press timeout
workspace2 var byte ;provide holding variable for button press timeout
valuespeed var long ;current speed of the DC motor
direction var long ;current direction of the servo rotation… either 1 or 0
initialize var long ;create a variable that says whether the program has been initialized… if 0 then run first set of code; if 1 then ignore first set of code
valuespeed = 1020 ;set the intial speed of the DC motor at 0… value of 0 is full reverse; value of 2000 is full forward; value of 1020 is speed 0
onvar var long ;Create a timer that acts as a safety if the operator forgets to press the second button
onvar = 0 ;Set the intitial timer at 0
initialize = 0 ;Set the intial variable to 0 when program begins
hservo [p13\1000\255] ;Set the starting position of the first servo
pause 500 ;give the servo time to respond
hpwm p11, 2000, 1020 ; Set the intial motor speed at 0
sleep 1000 ;Give the motor time to stabilize
Main ; begin button searching loop
if initialize = 0 then ;If the program is running for the first time, make sure the motor is at 0
hpwm p11, 2000, 1020 ;Set the motor at speed 0
initialize = 1 ;remove the intialize flag
sleep 1000 ;give the motor time to adjust
endif
button P14,0,80,40,workspace1,1,pressedon ;waiting for button press. Once button is pressed go to function pressedon
goto main
pressedon ;Function to handle when button on pin 14 is pressed. This funtion starts the motor and servo sequence
valuespeed = 1020 ;Sets the starting speed of the motor speed variable to the current speed of the motor which should be 0
for valuespeed = 1020 to 2000 step 15 ;Create a loop to handle the increasing speed of the motor
hpwm p11, 2000, valuespeed ;set the speed of the motor which increases each step of the loop
sleep 50 ;give the motor some slight wiggle room so code does not skip
;HERE IS WHERE I AM ENCOUNTERING PROBLEMS
if (hservoidle p13) then ;if the servo on pin 13 is idle then move the servo
if direction = 0 then ;check to see if last direction was clockwise or counterclockwise
hservo [13\0\1000] ;change the position of the servo
direction = 1 ;update the servo direction variable
else ;check to see if last direction was clockwise or counterclockwise
hservo [13\10000\1000] ;change the position of the servo
direction = 0 ;update the servo direction variable
endif
endif
next ;End of speed ramping loop
onvar = 1 ;set the intial variable of the fail safe if the operator forgets to press the second button
for onvar = 1 to 100000 step 1 ;start the fail safe loop with a nice large quantity
button P15,0,80,40,workspace2,1,counteroff ;check for the button 2 press; if pressed, go to the function counteroff SEE BELOW
if (hservoidle p13) then ;if the servo on pin 13 is idle then move the servo
if direction = 0 then ;check to see if last direction was clockwise or counterclockwise
hservo [13\0\1000] ;change the position of the servo
direction = 1 ;update the servo direction variable
else ;check to see if last direction was clockwise or counterclockwise
hservo [13\10000\1000] ;change the position of the servo
direction = 0 ;update the servo direction variable
endif
endif
sleep 50
next
sleep 100
goto main
counteroff ;This handles the second button press by ramping down the speed of the motor and eventually turning off the servo
valuespeed = 2000 ;Sets the starting speed of the motor speed variable to the current speed of the motor which should be Full Forward
for valuespeed = 2000 to 1020 step -50 ;Create a loop to change the speed every 50 steps downward
hpwm p11, 2000, valuespeed ;change the motor speed to a smaller value eventually reaching 0
sleep 50
next
sleep 1000
;ENDED HERE, BUT WILL NEED TO PUT IN CODE TO STOP THE SERVO. THEN THE PROGRAM GOES TO INITIAL LOOP UNTIL FIRST BUTTON IS PRESSED.
goto main[/code]