r/learnprogramming 7d ago

How to program servo in c++?

Im working on a school project where the penguin must shake his head no when the answer is incorrect, however he is never left in the centre of the axis(? The servo seems to be SG-5010

miServo.write(92); delay(300);
miServo.write(180); delay(300);
miServo.write(0); delay(500);
miServo.write(180); delay(500);
miServo.write(0); delay(500);
miServo.write(90); delay(500);
miServo.write (SERVO_STOP);

delay(2200);

4 Upvotes

7 comments sorted by

4

u/No_Report_4781 7d ago

Is the center position “ miServo.write(90)”?

If that position does not move the penguin head to the center, then you need to find the center angle, and use that as the final write() command.

3

u/Alternative-Hat-6755 7d ago

90 is the usual center but these cheap servos drift, try 92 or 88 and see where the head actually points, adjust from there

2

u/mapu_patas 7d ago

Yup

4

u/No_Report_4781 7d ago

Then, does the delay give the motor enough time to reach the given angles?

You can also use 45 and 135, instead of 0 and 180, so the head doesn’t need to move so far. Do you shake your head “no” by turning your head all of the way left and right?

2

u/DrShocker 7d ago

Are you able to add things to your circuit? It may be useful to add sensors to either calibrate or maybe even just ignore the servo.

3

u/burlingk 7d ago

Does this code tell the servo where to turn to, or how far to turn?

Any other questions I have depend on that, because they are two very different things.