Servo Spasms

Hey Guys Im building a Hexapod and its powered by servos and an arduino mega. Every time i start the arduino or reset the programm my servos twitch around randomly. The only workaround ive found so far is disconnecting the power to the servos when starting the microcontroller. My question now is as following: Can I solve this problem in the code? Meaning can I do something that prevents these random movements at startup without disconnecting the servos?

it may make sense to establish a signal to all the servos before providing power as part of you init.

Maybe. Try doing a write() to each servo BEFORE you do the attach(). I'll leave you to choose the best value to write.

But it might not work. Some servos simply respond badly to having power but no useful control signal and that's bound to happen whenever you start/reset the Arduino.

Steve

Thank you for the answer. How would that work I mean I cant write to a servo object that I havent named yet. Or would I just use the pin number instead of the servo name?

That might very well be the case as I am using those shitty chinesium mg996r servos.

You might put the servos in a parked no load position before starting them up. If they all try to move to a loaded position all at the same time, there might be a transient low power situation depending on how you are powering the servos. How many servos are you using and what is the power supply?

AaronSilas: Thank you for the answer. How would that work I mean I cant write to a servo object that I havent named yet. Or would I just use the pin number instead of the servo name?

That might very well be the case as I am using those shitty chinesium mg996r servos.

I hope you have separate power to the servos because MG996s need quite a lot of current.

But the write() goes after the instantiation and before the attach e.g. something like:

Servo servo1;
Servo servo2;

void setup() {
servo1.write(90);
servo1.attach(servo1Pin);
etc.

Steve