#include <Servo.h>
// Define the servo objects
Servo frontLeftLeg;
Servo frontRightLeg;
Servo backLeftLeg;
Servo backRightLeg;
// Define the servo pins
const int frontLeftPin = 5;
const int frontRightPin = 6;
const int backLeftPin = 9;
const int backRightPin = 10;
// Define the positions for the walking pattern
const int standPosition = 90;
const int stepForward = 120;
const int stepBackward = 60;
void setup() {
// Attach the servos to the corresponding pins
frontLeftLeg.attach(frontLeftPin);
frontRightLeg.attach(frontRightPin);
backLeftLeg.attach(backLeftPin);
backRightLeg.attach(backRightPin);
// Set the initial position to standing
frontLeftLeg.write(standPosition);
frontRightLeg.write(standPosition);
backLeftLeg.write(standPosition);
backRightLeg.write(standPosition);
// Wait for the servos to reach the initial position
delay(500);
}
void loop() {
// Move the legs in a walking pattern
// Step 1: Move front left and back right legs forward
frontLeftLeg.write(stepForward);
backRightLeg.write(stepForward);
delay(500); // Wait for the movement to complete
// Step 2: Move front right and back left legs backward
frontRightLeg.write(stepBackward);
backLeftLeg.write(stepBackward);
delay(500); // Wait for the movement to complete
// Step 3: Move front left and back right legs backward
frontLeftLeg.write(stepBackward);
backRightLeg.write(stepBackward);
delay(500); // Wait for the movement to complete
// Step 4: Move front right and back left legs forward
frontRightLeg.write(stepForward);
backLeftLeg.write(stepForward);
delay(500); // Wait for the movement to complete
// Step 5: Return all legs to standing position
frontLeftLeg.write(standPosition);
frontRightLeg.write(standPosition);
backLeftLeg.write(standPosition);
backRightLeg.write(standPosition);
delay(500); // Wait for the movement to complete
// Repeat the walking pattern
}