#include <Servo.h>
// Define servo objects
Servo servoLeft;
Servo servoRight;
// Pins connected to servo motors
int leftServoPin = 9; // Change this to the actual pin you're using
int rightServoPin = 10; // Change this to the actual pin you're using
// Initial positions for servo motors
int leftInitialPos = 0; // 90 degrees is usually the center position
int rightInitialPos = 0;
// Incremental angle change for walking motion
int angleIncrement = 90; // You can adjust this value for the desired motion
void setup() {
// Attach servo objects to pins
servoLeft.attach(leftServoPin);
servoRight.attach(rightServoPin);
// Initialize servo positions
servoLeft.write(leftInitialPos);
servoRight.write(rightInitialPos);
// Optional: You can add a delay here for initial setup if needed
// delay(1000);
}
void loop() {
// Move the servo motors back and forth for walking motion
for (int angle = leftInitialPos - 180; angle <= leftInitialPos + 90; angle += angleIncrement) {
servoLeft.write(180 + angle);
servoRight.write(180 - angle); // To keep legs in sync, use opposite angles for the other leg
delay(100); // Adjust delay for desired walking speed
}
// Return to initial position
servoLeft.write(leftInitialPos);
servoRight.write(rightInitialPos);
delay(1000); // Delay before starting the next walking cycle
}