'this simple code drives a servo
' a complete help of Annex32 can be found here
' https://cicciocb.com/annex32help/V1.442/
DIRECTION = 2 : STEPS = 15
LEFTLIMIT = 35: HOME = 4: RESET = 32: RIGHTLIMIT = 5
HOMEpressed = 0: FAST = 90
pin.mode DIRECTION, output 'dir
pin.mode STEPS, output 'step
pin.mode HOME, input, pullup 'Home
pin.mode RESET, input, pullup 'Reset
pin.mode LEFTLIMIT, input, pullup 'Left limit sw
pin.mode RIGHTLIMIT, input, pullup 'Right limit sw
interrupt HOME, Home 'Home
interrupt RESET, Reset 'Reset
interrupt LEFTLIMIT, Llimit 'Left limit sw
interrupt RIGHTLIMIT, Rlimit 'Right limit sw
timer0 100, getJOY
pwm.setup STEPS, OFF
freq_p = -1
wait
Home:
pause 40
if pin(HOME) = 1 then return
HOMEpressed = 1
if STEPS > 0 then dir = 0
if STEPS < 0 then dir = 1
print "HOME pressed"
return
Reset:
pause 40
if pin(RESET) = 1 then return
'RESETpressed = 1
HOMEpressed = 0
print "RESET pressed"
return
Llimit:
pause 40
if pin(LEFTLIMIT) = 1 then return
pwm.setup STEPS, OFF
print "HALT at LEFT limit"
return
Rlimit:
pause 40
if pin(RIGHTLIMIT) = 1 then return
pwm.setup STEPS, OFF
print "HALT at RIGHT limit"
return
getJOY:
v = adc(34)
' computes the
ratio = (v - 2048)/2048
if (HOMEpressed = 1) then
freq = FAST
else
freq = abs(ratio * 100)
dir = ratio > 0
endif
pin(DIRECTION) = dir
'print ratio, freq
if (abs(ratio) < 0.1) then
pwm.setup STEPS, OFF
else
print STEPS
if ((HOMEpressed=1) and (STEPS=0)) then pwm.setup STEPS, OFF
if (freq_p <> freq) then
pwm.setup STEPS, 1, 100, freq, 12
freq_p = freq
endif
end if
return