I need to power 8 servos for the four legged robot I'm building, and the servos are glitching out. It's coded with an Arduino Uno R3 I'm assuming the problem is the power source since its just 3 AA alkaline batteries and it only started glitching when multiple servos were being powered. Also this is put together via bread board and I want to know if there is a better way to connect all these servos to the power source without all these stupid wires.
I was thinking maybe 4 rechargeable AA batteries would do, and I was looking into protoboards. Is this the right direction?
I tried taking pictures of my breadboard setup but theres like no good angle so I'm sorry ;-;
Also here is my code:
#include <Servo.h>
//////////////////////////////////////////////////
// States
//////////////////////////////////////////////////
enum RobotState { IDLE, WALK };
RobotState currentState = IDLE;
//////////////////////////////////////////////////
// Gait Settings (SAFE VALUES)
//////////////////////////////////////////////////
float phase = 0.0;
float speed = 0.04; // smaller = slower walk
float stepLength = 20; // hip swing amount
float stepHeight = 25; // knee lift amount
int neutralHip = 90;
int neutralKnee = 95;
//////////////////////////////////////////////////
// Servo Offsets
//////////////////////////////////////////////////
int offsetFLHip = 0;
int offsetFLKnee = -9;
int offsetFRHip = 0;
int offsetFRKnee = 4;
int offsetBLHip = 11;
int offsetBLKnee = 0;
int offsetBRHip = -5;
int offsetBRKnee = 15;
//////////////////////////////////////////////////
// Servo Setup
//////////////////////////////////////////////////
Servo frontLeftHip, frontLeftKnee;
Servo frontRightHip, frontRightKnee;
Servo backLeftHip, backLeftKnee;
Servo backRightHip, backRightKnee;
void setupServos() {
frontLeftHip.attach(2);
frontLeftKnee.attach(3);
frontRightHip.attach(4);
frontRightKnee.attach(5);
backLeftHip.attach(6);
backLeftKnee.attach(7);
backRightHip.attach(8);
backRightKnee.attach(9);
}
//////////////////////////////////////////////////
// Safe Write (clamped)
//////////////////////////////////////////////////
void writeServo(Servo &s, int angle, int offset) {
int corrected = constrain(angle + offset, 0, 180);
s.write(corrected);
}
//////////////////////////////////////////////////
// Smooth Trot Gait
//////////////////////////////////////////////////
void updateLeg(
Servo &hip,
Servo &knee,
int hipOffset,
int kneeOffset,
float legPhase
) {
// Hip swings full sine wave
float hipAngle = neutralHip + stepLength * sin(legPhase);
// Knee lifts only during forward swing
float lift = max(0.0, sin(legPhase));
float kneeAngle = neutralKnee - stepHeight * lift;
writeServo(hip, (int)hipAngle, hipOffset);
writeServo(knee, (int)kneeAngle, kneeOffset);
}
//////////////////////////////////////////////////
// IDLE
//////////////////////////////////////////////////
void Idle() {
writeServo(frontLeftHip, neutralHip, offsetFLHip);
writeServo(frontLeftKnee, neutralKnee, offsetFLKnee);
writeServo(frontRightHip, neutralHip, offsetFRHip);
writeServo(frontRightKnee,neutralKnee, offsetFRKnee);
writeServo(backLeftHip, neutralHip, offsetBLHip);
writeServo(backLeftKnee, neutralKnee, offsetBLKnee);
writeServo(backRightHip, neutralHip, offsetBRHip);
writeServo(backRightKnee, neutralKnee, offsetBRKnee);
}
//////////////////////////////////////////////////
// WALK (Diagonal Trot)
//////////////////////////////////////////////////
void Walk() {
phase += speed;
if (phase > TWO_PI) phase -= TWO_PI;
float oppositePhase = phase + PI;
if (oppositePhase > TWO_PI) oppositePhase -= TWO_PI;
// FL + BR
updateLeg(frontLeftHip, frontLeftKnee, offsetFLHip, offsetFLKnee, phase);
updateLeg(backRightHip, backRightKnee, offsetBRHip, offsetBRKnee, phase);
// FR + BL
updateLeg(frontRightHip, frontRightKnee, offsetFRHip, offsetFRKnee, oppositePhase);
updateLeg(backLeftHip, backLeftKnee, offsetBLHip, offsetBLKnee, oppositePhase);
}
//////////////////////////////////////////////////
// Setup & Loop
//////////////////////////////////////////////////
void setup() {
Serial.begin(9600);
setupServos();
}
void loop() {
checkInput();
switch (currentState) {
case IDLE:
Idle();
break;
case WALK:
Walk();
break;
}
delay(15); // smooth + safe refresh rate
}
//////////////////////////////////////////////////
// Serial Control
//////////////////////////////////////////////////
void checkInput() {
if (Serial.available()) {
char cmd = Serial.read();
if (cmd == 'w') currentState = WALK;
if (cmd == 'i') currentState = IDLE;
if (cmd == '+') speed += 0.01;
if (cmd == '-') speed -= 0.01;
}
}



