First a little background. I'm primarily a modeler, specifically, though not limited to, Star Trek ships, and because I wanted to light some of them...led me into hobby electronics and eventually Picaxe, mostly for the PWM aspect, as well as parallel-tasking without having to write dozens of lines of Arduino code, but I've never had the need to use a servo in my projects...until now. I recently watched a youtube video on how to motorize the 1/670 scale Voyager warp pylons using two HS-40 nano-servos. So, I googled to try to get info on how to go about using Picaxe to control them. What I need to do is have the servos turn only 40 degrees at the command of a momentary switch. I.E. at Case 1 turn the servos 40 degrees (pylons up), then Case 2 have the servos return to the starting point (pylons down). I ended up, if you can believe it, having a long conversation with the Google A.I. The darn thing wrote a Picaxe program for me, but I want to run it by you guys to make sure that AI is not leading me astray. Anyway, here is Google AI's code. Oh, and by the way, I haven't even put this in the Picaxe Editor to see if it would compile.
Code:
; PICAXE 08M2 Momentary Switch Servo Control
;
; --- Pin Assignments ---
; C.3 = Input for momentary switch
; C.2 = Output for HS-40 nano servo signal wire
; --- Variable Definitions ---
symbol switch_pin = C.3 ; Assign a friendly name to the switch pin
symbol servo_pin = C.2 ; Assign a friendly name to the servo pin
symbol servo_state = b0 ; Variable to hold the current servo state (0 = start, 1 = 40 deg)
symbol start_pos = 150 ; Initial servo position (adjust as needed for your specific servo)
symbol forty_degree_pos = 190 ; Position for 40 degrees (ADJUST THIS VALUE by experimenting)
; --- Main Program ---
init:
; Set the internal oscillator frequency to 16MHz for faster response
setfreq m16
; Initialize the servo to the starting position
servo servo_pin, start_pos
pause 1000 ; Wait for the servo to settle
main:
; Check if the switch is pressed (pin goes high)
if switch_pin = 1 then
pause 50 ; Debounce delay to prevent multiple button press reads
; Wait for the button to be released before proceeding
do while switch_pin = 1: loop
; Increment the state variable
inc servo_state
; Handle the two servo states using SELECT CASE
select case servo_state
case 1
; First press: move servo to 40 degrees
servopos servo_pin, forty_degree_pos
case 2
; Second press: return servo to starting position
servopos servo_pin, start_pos
; Reset the state for the next cycle
servo_state = 0
endselect
endif
goto main ; Loop back to check the button again