Help controlling servo

davehughes87

New Member
Hi guys,

Complete novice here. I bought the PICAXE-08 Servo Driver Board kit and I'm attempting to control a servo using an on-off-on toggle switch controlling two inputs to allow it to move either way. The end goal is that the system will open and close some window blinds at work so I don't have to keep getting up. I am THAT lazy!

I've completed the build of the board kit. I've connected my servo to "2" and written a very simple program to move it around. It works!

I've then connected the two "on" positions of the toggle switch to the pins intended for the positive voltage and signal pins of servo "1" and "4". I know these pins were intended to be servo outputs in this kit, but I was under the impression the PICAXE could be programmed to treat these pins as inputs. I've written a very simple (and probably wrong) program - pasted below - to cause the servo to react to the switch's position. It's no longer working. :(

I'm struggling to understand where I'm going wrong. Is my code wrong? Can I not use these pins as inputs? Any tips are greatly appreciated. (Please bear in mind I'm a complete beginner!)

Thanks for any help.

Dave

*pin numbers might be wrong - but I've tried all sorts of combinations and not got anywhere!

Code:
init:
	servopos 2, 100	; initialise servo
	input C.1
	input C.4

main:
	if pinC.1 = 1 then bob
	if pinC.4 = 1 then dog

bob:
	pause 100
	servopos 2, 200
	goto main

dog:
	pause 100
	servopos 2, 150
	goto main
 

Goeytex

Senior Member
Before the servo will work you must first use the "servo" command . Servospos will not initialize the servo.
 

hippy

Technical Support
Staff member
Also, after the two IF commands, a "goto main" will probably also help

And welcome to the forum.
 

davehughes87

New Member
Cheers guys. Am I right that it's pinC.1 and pinC.4 for the switch? I've (hopefully) attached a circuit diagram of what I'm trying to do. Also, since making the changes you suggested, the servo now seems to move randomly and not when I flick the switch. I can't see any obvious errors in the program.

circuit_diagram_v1.png
 

neiltechspec

Senior Member
I would do it slightly different to that :

have the centre of the toggle switch to Gnd & use the internal weak pullup resistors with 'pullup on' added to init:

main:
do
if pinC.1 = 0 then bob
if pinC.4 = 0 then dog
loop
 

Goeytex

Senior Member
Along the lines of what neiltechspec said.

Add a line to the beginning of code:
Code:
 Pullup %10010  [COLOR="#008000"]; Internal Pullups on C.1 & C.4[/COLOR]
This will prevent the inputs from "floating" when the switch is in the center position

See attached diagram

Here is how I might do the code:
Code:
#picaxe 08M2
#no_data

init:
    pullup %10010  'Internal Pullup on C.1 & C.4 
    input C.1     [COLOR="#008000"] 'Not needed. All pins default to inputs on power up[/COLOR]
    input C.4      [COLOR="#008000"]'Not needed. All pins default to inputs on power up[/COLOR]
    servo 2,100    [COLOR="#008000"]'Initalise servo[/COLOR]
	

main:  [COLOR="#008000"]'Main program loop[/COLOR]
	
    do 
        if pinC.1 = 1 and pinC.4 = 0 then bob   [COLOR="#008000"]'switch up[/COLOR]
        if pinC.1 = 0 and pinC.4 = 1 then dog   [COLOR="#008000"]'switch down[/COLOR]
        if pinC.1 = 1 and pinC.4 = 1 then cat   [COLOR="#008000"]'center pos [/COLOR]
   loop

bob:
    pause 100
    servopos 2, 200
    goto main

cat:
    pause 100
    servopos 2, 150
    goto main
	
dog:
    pause 100
    servopos 2, 100
    goto main
 

Attachments

Last edited:
Top