Controlling the speed movement of a RC servo

oneshot

Member
I need to control the movement speed of an RC servo with a PICAXE im currently using the PICAXE 20M2. I have this code below which works fine but need to change the movement speed so it can move slower. With that code below the servo is moving too fast, I need to slow it down. PLEASE PROVIDE ME WITH NEW CODE




Here is the code im working with now:


--------------------------------------------

init: servo 4,75 ; initialise servo
main:
do
random w0
loop until w0 <= 10000 and w0 >= 3000
pause w0
servopos b.4,75 ; move servo to one end
pause 2000 ; wait 2 seconds
servopos b.4,150 ; move servo to middle
do : loop until pinc.1 = 1
goto main ; loop back to start

----------------------------------------------
 

westaust55

Moderator
Try some code like this:
Code:
SYMBOL shortdelay = 10 ; adjust this for desired speed
SYMBOL posn = b5
SYMBOL orig = b6

init: 
	orig =75
	servo 4,orig ; initialise servo
 
main: 
	DO
		DO
			random w0
		LOOP until w0 <= 10000 and w0 >= 3000
		pause w0
		FOR posn =orig to 75 step 5 ; can also adjust the step size try 1 if still too fast
			servopos b.4,posn ; move servo to one end
			PAUSE shortdelay
		NEXT posn
		pause 2000 ; wait 2 seconds
		FOR posn = 75 to 150 step 5
			servopos b.4,posn ; move servo to middle
			PAUSE shortdelay
		NEXT posn
		orig = 150
		do 
		loop until pinc.1 = 1
	LOOP ; loop back to start
while your code as posted is quite small, a good habit to get into is to indent your code as I have done so the loop structures and test structures are clearly evident. then use the [code] and [/code] tags around your code
 
Last edited:

oneshot

Member
bluejets can't I slow down the speed movement of the servo just by using code instead of using that other device?

westaust55 I tried that code and did not work. Servo moves at a faster speed and rotates at a shorter distance.
 

bluejets

Senior Member
Point was that there is a device that does the job at less cost than the chip you're about to use.
It was just a suggestion.
 

westaust55

Moderator
Fail to see how adding a loop and delays makes the servo faster.

But there is a "bug" in my code.
When using a FOR...NEXT loop decrementing the step must have a negative sign.

The missing negative sign would severely limit travel of the servo.
 

westaust55

Moderator
Here is with the negative in front of the reducing step increments. Untested as I am not near a PC with the PE installed.

I also reduced the step from 5 to 1 since you commented that is was even faster than a servo going direct to the desired position.
If still too fast as per the earlier copy, for the line
SYMBOL shortdelay = 10 ; adjust this for desired speed​
increase the value for the short delay between each step.

Code:
SYMBOL shortdelay = 10 ; adjust this for desired speed
SYMBOL posn = b5
SYMBOL orig = b6

init: 
	orig =75
	servo 4,orig ; initialise servo
 
main: 
	DO

		DO
			random w0
		LOOP until w0 <= 10000 and w0 >= 3000
		pause w0

		FOR posn = orig to 75 step [B][COLOR="#FF0000"]-[/COLOR][/B][COLOR="#0000CD"][B]1[/B][/COLOR] ; can also adjust the step size try 1 if still too fast
			servopos b.4,posn ; move servo to one end
			PAUSE shortdelay
		NEXT posn

		pause 2000 ; wait 2 seconds

		FOR posn = 75 to 150 step [COLOR="#0000CD"][B]1[/B][/COLOR]
			servopos b.4,posn ; move servo to middle
			PAUSE shortdelay
		NEXT posn

		orig = 150
		do 
		loop until pinc.1 = 1

	LOOP ; loop back to start
 
Top