# Servo programming problem

#### KiwiKid

Joined May 12, 2011
16
For my electronics project we must create a model barrier arm that is activated when a toy car drives up.

We must use a servomotor (hextronik hxt900 9gr) a Picaxe08M microprocessor and a LDR voltage drop.

I have the circuit set up correctly but I am having trouble programming the servo motor. I can get it to go between two postions correctly using the following program. (sorry if not correct, do not remember exactly the program)

Main: servo 2,2
wait 2
servo 2,155
wait 2
goto main

This works but when I use my main program:

Start: servo 2,2

if b1 <150 than Motoron (150 being the value of the LDR when covered)
servo 2,2
pause 100
goto Main

Motoron: servo 2,155
wait 5
goto Main

This does not work an I do not know why, all that happens is that the servo goes to the starting position and stays here but does not move when the LDR is covered.

Please explain why if you know and post a suitable replacement program. (Using picaxe commands)

#### KiwiKid

Joined May 12, 2011
16
Please post if you can help

#### SgtWookie

Joined Jul 17, 2007
22,221
I'm just adding CODE tags around your programming to preserve the formatting.
For my electronics project we must create a model barrier arm that is activated when a toy car drives up.

We must use a servomotor (hextronik hxt900 9gr) a Picaxe08M microprocessor and a LDR voltage drop.

I have the circuit set up correctly but I am having trouble programming the servo motor. I can get it to go between two postions correctly using the following program. (sorry if not correct, do not remember exactly the program)

Rich (BB code):
Main: servo 2,2
wait 2
servo 2,155
wait 2
goto main
This works but when I use my main program:

Rich (BB code):
Start: servo 2,2

if b1 <150 than Motoron  (150 being  the value of the LDR when covered)
servo 2,2
pause 100
goto Main

Motoron: servo 2,155
wait 5
goto Main
This does not work an I do not know why, all that happens is that the servo goes to the starting position and stays here but does not move when the LDR is covered.

Please explain why if you know and post a suitable replacement program. (Using picaxe commands)

#### SgtWookie

Joined Jul 17, 2007
22,221
Looking in the PICAXE manual, it seems that you're not using the servo command correctly; it should be used to initialize the servo position, but after that the servopos command should be used instead.
Also, "Do not use a pulse value less than 75 or greater than 225, as this may cause the
servo to malfunction." - I see you are using a pulse value of 2; why?

Also, while "wait n" is in seconds, you are using "pause 100"; pause is in milliseconds, so that's 100mS if your clock speed is as described in the manual. If you're clocking at a faster rate, it might only be 50mS, or some other amount of time. You might simply not have enough time for the servo to respond.

In your Motoron: routine, you have a 5 second wait delay. During this time, the state of the LDR will be ignored. Same thing with your "pause 100"; it might take up to 100mS before the state of the LDR is again measured.

I suggest that instead what you do is to constantly poll the LDR, pause for a period of time, say 10mS, and if the LDR stays dark for ~100mS or 10 samples, then the barrier needs to come down for a period of time. Then you might raise the barrier for another period of time to let the car proceed, and then continuing the polling of the LDR to see if it is light or dark.

You really need to define the series of events how you wish for them to occur.

#### KiwiKid

Joined May 12, 2011
16
I used the value of 2 because 2 and 150 were at a angle of 90° to each other (approximately). Why does this cause it to malfunction?

I understand that pause is in ms bu I merely used this because I heard it was good to have a pause after a servo command.

The state of the LDr because at this time it is irrelevant. The car drives up, breaks beam of light shining on LDR, arm opens and waits 5 second for car to drive through, however if it is still dark it will sat pen for a further 5 seconds.
If we shut the gate as soon as the LDR is bright than we may shut the gate on the car because the LDR is not positioned directly below the arm. or for instance the car has a tralier and in between the car and the trailer the LDR is lit up for a moment.
as for the sequence of series here it is-
-servo goes on and goes to 90°
-car drives up, block light
-servo opens
-car drives through
-after 5s the arm closes ONLY if the LDR is lit up again though

Thanks for response and i will try suggestions at school today and will report back this avo.

#### KiwiKid

Joined May 12, 2011
16
I used your suggestions and the servo now works. Thanks

However when ever the servo moves to a position it make a loud noise as if it is still turning. One of my classmates said something about it being the servo stalling but I thought I'd double check. What is it and how do I stop it?

If this does not get solved soon I'll just start a new thread specifically for it.

Thanks

#### SgtWookie

Joined Jul 17, 2007
22,221
Your servo may be slamming into it's limits of travel.

Read the PICAXE manual 2, pages 211, 212, and 213, which describe the SERVO and SERVOPOS functions, but perhaps the most relevant piece of information at the moment is this bit from page 211:
Do not generally use a pulse value less than 75 or greater than 225, as this may
cause the servo to malfunction. Due to tolerances in servo manufacture all values
are approximate and will require fine-tuning by experimentation (e.g. 60 to 200).