2 Servos one 28x1 & simulator question.

xtech007

Senior Member
Hello Forum !!
Once again I turn to the forum for help.
In advance thank you for your time and support.

Here is my scenario:
I want to use 2 servos, each move independent from each other from the same 28x1. I will be using an analog stick for input. "Yes" I have read the manual several times for diagrams and power connections.

I don't have a wiring diagram yet.
all I have is the code, I have tested but it fails. when I simulate the code second servo don't move or at least that's what I see on the simulator.

Also if this was to work will there be a delay in commands when both servos are needed to move at the same time?

Once again thanks for the Help !
 

elios

Senior Member
oaky, firstly, need a program. secondly, (i know your havent got one yet) but need a wiring diagram.

Servos need to have a pause. i.e

do
servopos 2,b1
pause 200
loop
 

xtech007

Senior Member
My mistake :) forgot code!!

Code:
init: 
servo 1,75
servo 2,150 

main: 
readadc 0,b1
readadc 1,b2
  			        
if b1<126 then RT		  			  
if b1>134 then LT 				  
if b1>= 127 and b1 <= 133 then Center

if b2<126 then UP		  			  
if b2>134 then DOWN 				  
if b2>= 127 and b1 <= 133 then Center2

RT: servopos 1,75 
goto main		
Center: servopos 1,150 
goto main			
LT: servopos 1,225 			
goto main 
	
UP: servopos 1,75 
goto main		
Center2: servopos 1,150 
goto main			
DOWN: servopos 1,225 			
goto main
 

hippy

Ex-Staff (retired)
I have tested but it fails. when I simulate the code second servo don't move or at least that's what I see on the simulator.
Single step the code and watch where the code goes while executing. In this case, depending on the value in b1 you branch to RT, LT or Center labels which then execute a 'goto main'. No code which deals with b2 is ever executed as you note.

Also, if it did handle b2, it updates the same servo.

You will also get more mileage and have a much clearer, shorter and more easily understandable and debuggable piece of code if you use the 'block structured' commands the PICAXE supports instead of IF-GOTO and GOTO ( DO-LOOP, IF-THEN-ELSE-ENDIF, SELECT-CASE ) ...

Code:
Do 
  ReadAdc 0, b1
  ReadAdc 1, b2
  Select Case b1
    Case < 126 : ServoPos 1, 75
    Case > 134 : ServoPos 1, 150
    Else       : ServoPos 1, 225
  End Select
  Select Case b2
    Case < 126 : ServoPos 2, 75
    Case > 134 : ServoPos 2, 150
    Else       : ServoPos 2, 225
  End Select
Loop
 

xtech007

Senior Member
will try and post what happened.

Once again thanks for the fast reply and support.
Looking at the code above it seams that it does not need servo to initialize servo command. is this is correct?
also will both servo be able to move at the same time if requested or will there be a delay?

Thanks all for the help.
 

hippy

Ex-Staff (retired)
You need to issue an initial SERVO command but then can use SERVOPOS. The code was just showing an alternative main loop.

Servo positioning is only done every 20ms ( the frame rate ); the odds are that each SERVOPOS is issued when the PICAXE is 'idling' within a frame so in the next frame both servos will be updated as simultaneously as possible. Sometimes the timing will be such that one SERVOPOS affects one frame, the other the frame later, so a 20ms delay. This is in most cases as near simultaneous as to make no difference.
 

xtech007

Senior Member
Another Mistake by me. :(

Ups my mistake. I had servopost 1 for both b1 & b2 input

Also to clarify only servo A has to move only with input b1
same with servo B has to move only with input b2
here is the new code:
Code:
init: 
servo 1,75
servo 2,150 

main: 	
readadc 0,b1             			'controls servo a
readadc 1,b2		 			'controls servo b
  			        
if b1<126 then RT		  			  
if b1>134 then LT 				  
if b1>= 127 and b1 <= 133 then Center

if b2<126 then UP		  			  
if b2>134 then DOWN 				  
if b2>= 127 and b1 <= 133 then Center2

RT: servopos 1,75 
goto main		
Center: servopos 1,150 
goto main			
LT: servopos 1,225 			
goto main 
	
UP: servopos 2,75 
goto main		
Center2: servopos 2,150 
goto main			
DOWN: servopos 2,225 			
goto main
 

xtech007

Senior Member
Does anyone else have a different approach to my Code?

Finally home!
Out with the wife & Kids..
Really long day..

Well I was reading all the reply's and will try it tonight. i don't really understand the case code by Hippy (will take me a while to understand it) but will give a shot to it.
For the mean time Thanks for your help!
 

BeanieBots

Moderator
You haven't really given a very precise spec for anyone to follow.

What is all the testing of values for?
Are these servos meant to "follow" the stick movements?
If yes, then why not simply read the analogue, scale to servo values, output to servo, repeat for other pot/servo and loop forever?
Or does it need to do something else?
 

xtech007

Senior Member
Yeaah!! Finally some hope. !!

BeanieBots thanks for the reply.
That's right I want the servo to follow the stick and well 2 stick and 2 servo each following its own stick.
 

BeanieBots

Moderator
Converting a pot position to a servo demand has been covered several times before. Always worth having a search.
Here's the maths, I'll leave you to add in the second pot/servo.

Code:
#PICAXE 28X1
servo 1,150              'initiate servo on OP 1 to neutral position
do
    readADC 0,b0        'read stick position on IP 0.
    b0=b0*10/17+75    'scale 0 to 255 to be 75 to 225
    servopos 1,b0        'send new value to servo on OP 1
    pause 20               'small delay to prevent excessive updates.
loop
 
Top