; Focus Servo for underwater camera using 08M Picaxe
; Servo lead on Output 0 (Pin 7)
; Focus Pot wiper Input 1 (Pin 6) 5k Pot between +5v and ground
; Centralise Switch Input 4 (Pin 3) Pull up 'R' with switch to ground
; Save End Posn Switch Input 3 (Pin 4) Pull up 'R' with switch to ground
; Update/Save switch Input 2 (Pin 5) Pull up 'R' with switch to ground
; 400 to 1100uS PWM needs 80 to 216 to servo (Gives 8 turns of feedback pot)
Output 0 ;Configure i/o pins of 08M, not sure if all are needed
Input 1
Input 2
Input 3
Input 4
;Setfreq m8 ; Might try increasing pulsout resolution later
Symbol ADCVAL = b0 ; ADC reading (0 to 255)
Symbol POSN = b2 ; Servo Position
Symbol Multi = b3 ; Multiplier value for formula
Symbol Divi = b4 ; Divisor value for formula
Symbol Offset = b5 ; Offset value for formula
Symbol Span = b6
Main:
pause 20 ; Adjust this value to get pulse repetion rate to 20mS
if Pin3 = 0 then Goto Record ; Write current position value to memory
if Pin2 = 0 then Goto Update ; Write default equation values to memory
if Pin4 = 0 then Goto Centralise ; Put servo mid travel and reset end stops to allow full travel
Read 2,Multi ; Multiplier (might always be 100)
Read 3,Divi ; Divisor so get correct output scaling
Read 4,Offset ; Basically same value as one end stop
Readadc 1,ADCVAL ; Gives 0-255
POSN = ADCVAL *Multi/Divi+Offset ; Convert ADC number to 80 to 216 (full travel of my servo)
Pulsout Output0,POSN ; Send signal to servo
Goto Main
Centralise: ; Also 'resets' system so full travel available for users setup
Pause 20 ; Adjust this value to get pulse repetion rate to 20mS
Pulsout Output0,148 ; Force servo to mid position for user setup (148 gives 744uS)
Write 2,100 ; Default Value for Multiplier
Write 3,187 ; Default for Divisor
Write 4,80 ; Default for offset
Goto main
Record:
Readadc 1, ADCVAL ; I put this line in to 'refresh' value as seen in simulator
if ADCVAL <=128 Then WriteLowVal ; Store end (stop) positions for both ends of servo travel
if ADCVAL >128 then WriteHighVal ;
Return
Update: ; Takes the two stored values. Calculates correct value for
; using in the equation and writes the new factors to memory
Span = b9 - b7 ; Difference between the two end stop numbers (ADC values)
Divi = 255 / Span ;
Write b6,Divi ; Divisor ;
Write b8,b3 ; Offset
Goto Main
WriteLowVal:
Write 6,ADCVAL ; I save both numbers here until I can decide which one is really
Write 5,POSN ; needed (by seeing in simulator whisch is the one I should use)
Goto Main
WriteHighVal:
Write 7,POSN ; I save both numbers here until I can decide which one is really
Write 8,ADCVAL ; needed (by seeing in simulator whisch is the one I should use)
Goto Main