Servos on an 08M2

radiogareth

Senior Member
Very simple circuit, 3 servos on an 08M2, Pins 0,1&2.
I want them to move slowly in both directions in a sort of anti-phase - the middle servo drives in the opposite direction to the left and right ones.

What I'd like is for all 3 servos to go at the same speed in EACH direction, but with 3 on the go I cant think of a way to achieve this. One would be easy, but three....some clever nesting may be required.

TIA for suggestions.

Code:
init: 'sets all three servos up to central position on pins 0,1 &2
	servo 0,150	'150 being approximately their central position
	servo 1,150	'You will need to experiment with different servos
	servo 2,150	'to find the end stop limits. Don't drive them beyond this point
'set up startpoints - these can be adjusted up or down as you wish
	b0=60
	b1=90
	b2=120

automatic_advance:
	inc b0:if b0 > 180 then let b0=60:endif 	'This increments each variable until
	dec b1:if b1 < 60 then let b1=180:endif	'your chosen end position is reached
	inc b2:if b2 > 180 then let b2=60:endif	'then it resets them
	'Drives each servo to its new position using relevant variable
	servopos 0,b0
	servopos 1,b1
	servopos 2,b2
	'Reads a potentiometer across the V supply feeding ADC4
	readadc 4,b3	'stores it in B3
	pause b3		'uses B3 for the delay, effectively slowing the servos
	goto automatic_advance 'goes back and does it all again
		'loop back to start of main loop
 
Last edited:

westaust55

Moderator
Consider using a Fwd/Rev flag for each Servo.
Then the code for each servo might be like this:
Code:
    IF FwdRev1 = 1 then
        INC b0 : IF b0 > 180 THEN : FwdRev1 = 0 : ENDIF
    ELSE
        DEC b0 : IF b0 < 60 THEN : FwdRev1 = 1 : ENDIF
    ENDIF

    Servopos 0,b0
You will need to set/initialise the initial direction for each FwdRev<x> flag to suit
 
Last edited:

radiogareth

Senior Member
OK, very useful as it will allow me individual control of each servo which is what I ultimately wanted.
In the short term I am using this, longer term I'll re-write using the above.
Thanks :)
Gareth
Code:
'*******************************************************
'* Sample PICAXE Software for use with PICAXE Micros   *
'* For educational use only. E & OE                    * 
'* By R G Evans,known as Gareth or radiogareth on Forum*
'* For more information and PCBs visit www.g4xat.co.uk *
'* Three servos waving backwards and forwards on an 08M*
'*******************************************************
init: 'sets all three servos up to central position on pins 0,1 &2
	servo 0,150	'150 being approximately their central position
	servo 1,150	'You will need to experiment with different servos
	servo 2,150	'to find the end stop limits. Don't drive them beyond this point
	'set up startpoints - these can be adjusted up or down as you wish
	b0=95		'lower number = further left
	b1=175	'higher number = further right
			'DIFFERENCE should be the same as used in the for-next B2 loop below
			'software now drops into the next routine
automatic_advance:
	for b2=0 to 80	'for-next loop to repeat endlessly. Make the SAME as difference above
	inc b0:		'This increments each variable until...
	dec b1:		'This decrements each variable until...
	servopos 0,b0	'Drives each servo to its new position
	servopos 1,b1 	'using relevant variable
	servopos 2,b0	'copy of servo 0
	readadc 4,b3	'Reads a potentiometer across the V supply feeding ADC4
				'stores it in B3
	pause b3		'pauses for the time set
	next b2		'does the loop again			

	for b2=0 to 80	'for-next loop to repeat endlessly Make the SAME as difference above
	dec b0:		'This decrements this variable until...
	inc b1:		'This increments this variable until...
	servopos 0,b0	'Drives each servo to its new position
	servopos 1,b1 	'using relevant variable
	servopos 2,b0
	readadc 4,b3	'Reads a potentiometer across the V supply feeding ADC4
				'stores it in B3 used twice to speed up reaction to change
	pause b3		'pauses for the time set in b3 0- 255 mSec per step
	next b2		'does the loop again
	goto automatic_advance 'goes back and does it all again
 

radiogareth

Senior Member
OK, wrote this:
Code:
init: 'sets all three servos up to central position on pins 0,1 &2
	servo 0,150	'150 being approximately their central position
	servo 1,150	'You will need to experiment with different servos
	servo 2,150	'to find the end stop limits. Don't drive them beyond this point
	symbol FwdRev0=0
	symbol FwdRev1=1
	symbol FwdRev2=0	
	'set up startpoints - these can be adjusted up or down as you wish
	b0=60		'lower number = further left
	b1=90	'higher number = further right
	b2=120

Automatic_waving: 
  
    	IF FwdRev0=1 then
        INC b0 : IF b0 > 180 THEN : FwdRev0 = 0 : ENDIF
    	ELSE
        DEC b0 : IF b0 < 60 THEN : FwdRev0 = 1 : ENDIF
    	ENDIF
    	Servopos 0,b0
    	    	IF FwdRev1=1 then
        INC b1 : IF b1 > 180 THEN : FwdRev0 = 0 : ENDIF
    	ELSE
        DEC b1 : IF b1 < 60 THEN : FwdRev0 = 1 : ENDIF
    	ENDIF
    	Servopos 1,b1
    	    	IF FwdRev2=1 then
        INC b2 : IF b2 > 180 THEN : FwdRev2 = 0 : ENDIF
    	ELSE
        DEC b2 : IF b2 < 60 THEN : FwdRev2 = 1 : ENDIF
    	ENDIF
    	Servopos 2,b2
    	goto automatic_waving
and the syntax fails on this line: IF FwdRev0=1 then

Not sure whats wrong with it, it seems 'logically correct'.

TIA
 

radiogareth

Senior Member
Thanks, that works as follows: Finished for the time being, I'm sure it could be expanded to 8 servos for a "Mexican Wave" (but not on an 08Mx).

Code:
'********************************************************
'* Sample PICAXE Software for use with PICAXE Micros    *
'* For educational use only. E & OE  19/2/2013          * 
'* By R G Evans,known as Gareth or radiogareth on Forum *
'* For more information and PCBs visit www.g4xat.co.uk  *
'* Three servos waving backwards and forwards on an 08M *
'* With improvements suggested by westaust55 & eclectic *
'********************************************************
init: 'sets all three servos up to central position on pins 0,1 &2
	servo 0,135	'150 being approximately their central position
	servo 1,135	'You will need to experiment with different servos
	servo 2,135	'to find the end stop limits. Don't drive them beyond this point
	symbol FwdRev0=b10	'Flags for if:then:else
	symbol FwdRev1=b11
	symbol FwdRev2=b12
	FwdRev0=0			'L to R
	FwdRev1=1			'R to L
	FwdRev2=0			'L to R
	'set up startpoints - these can be adjusted up or down as you wish
	b0=95		'lower number = further left
	b1=130	'higher number = further right
	b2=165	'to get a good staggered effect - nothing in sync.

Automatic_waving: 'main routine
  	IF FwdRev0 = 0 then
        INC b0 : IF b0 >= 175 THEN : FwdRev0 = 1 : ENDIF
    	ELSE
        DEC b0 : IF b0 <= 95 THEN : FwdRev0 = 0 : ENDIF
    	ENDIF
    	Servopos 0,b0	'drives servo to value of Bx
    	readadc 4,b4	'reads a pot across Vsupply, value into b4
    	pause b4		'does a pause using value in b4 0-255 mSec
    	
    	IF FwdRev1=0 then	'and again
        inc b1 : IF b1 >=175 THEN : FwdRev1 = 1 : ENDIF
    	ELSE
        DEC b1 : IF b1 <=95 THEN : FwdRev1 = 0 : ENDIF
    	ENDIF
    	Servopos 1,b1
    	readadc 4,b4
    	pause b4
    	
    	IF FwdRev2=0 then	'and again
        INC b2 : IF b2 >= 175 THEN : FwdRev2 = 1 : ENDIF
    	ELSE
        DEC b2 : IF b2 <= 95 THEN : FwdRev2 = 0 : ENDIF
    	ENDIF
    	Servopos 2,b2
    	readadc 4,b4
    	pause b4
    	goto automatic_waving	'goes back and does it all again.
And here is what it looks like....fast

http://www.youtube.com/watch?v=E1xJ1GMnU9g and S-L-O-W http://www.youtube.com/watch?v=Yj3zI7WfuAg
 
Last edited:

westaust55

Moderator
For some explanation:
The reason that your code with
IF FwdRev0 = 0
Did not work is that the IF...THEN condition test can only test a variable or a pin against another value.
You had defined FwdRev0 as a constant rather than a byte variable hence it would not work until you implemented the change given by eclectic.

It is also possible to use bit variables ( bit0, bit1, etc) for those direction flags (FwdRevx.
That can save many byte variables, but you must then reserve (not use elsewhere) byte variable b0 or word variable w0.
 
Top