#include <Servo.h>
// Define servos
Servo frontLeft;
Servo frontRight;
Servo backLeft;
Servo backRight;
// Define initial positions for each servo
int frontLeftPos = 90;
int frontRightPos = 90;
int backLeftPos = 90;
int backRightPos = 90;
void setup() {
// Attach servos to pins
frontLeft.attach(10);
frontRight.attach(11);
backLeft.attach(5);
backRight.attach(6);
// Initialize servos to their starting position
frontLeft.write(frontLeftPos);
frontRight.write(frontRightPos);
backLeft.write(backLeftPos);
backRight.write(backRightPos);
delay(1000); // Allow servos to reach starting position
}
void loop() {
// Move servos to simulate walking
walkForward();
}
// Function to walk forward
void walkForward() {
// Step 1: Lift front left leg and back right leg
frontLeft.write(frontLeftPos - 30); // Lift leg
backRight.write(backRightPos - 30);
delay(500);
// Step 2: Move front left leg forward, back right leg backward
frontLeft.write(frontLeftPos + 30);
backRight.write(backRightPos + 30);
delay(500);
// Step 3: Return front left leg and back right leg to original position
frontLeft.write(frontLeftPos);
backRight.write(backRightPos);
delay(500);
// Step 4: Lift front right leg and back left leg
frontRight.write(frontRightPos - 30); // Lift leg
backLeft.write(backLeftPos - 30);
delay(500);
// Step 5: Move front right leg forward, back left leg backward
frontRight.write(frontRightPos + 30);
backLeft.write(backLeftPos + 30);
delay(500);
// Step 6: Return front right leg and back left leg to original position
frontRight.write(frontRightPos);
backLeft.write(backLeftPos);
delay(500);
}