#include <Servo.h>
// Define Servo objects for each part
Servo frontLeft, frontRight, backLeft, backRight;
Servo mouth, tail;
void setup() {
// Attach each servo to the respective pins
frontLeft.attach(3);
frontRight.attach(5);
backLeft.attach(6);
backRight.attach(9);
mouth.attach(10);
tail.attach(11);
// Set initial positions
initialPosition();
delay(1000); // Allow time for the servos to reach initial positions
}
void loop() {
moveForward();
delay(2000);
moveBackward();
delay(2000);
moveRun();
delay(2000);
sit();
delay(2000);
stand();
delay(2000);
bark();
delay(2000);
wagTail();
delay(2000);
}
// Function to set initial servo positions
void initialPosition() {
frontLeft.write(0); // Set front left leg to neutral position
frontRight.write(0); // Set front right leg to neutral position
backLeft.write(0); // Set back left leg to neutral position
backRight.write(0); // Set back right leg to neutral position
mouth.write(90); // Close mouth
tail.write(90); // Tail in neutral position
}
// Function to move the robot forward with all legs moving smoothly
void moveForward() {
for (int i = 0; i < 3; i++) { // Repeat for continuous motion
// Lift front left and back right legs while moving front right and back left forward
frontLeft.write(45); // Lift front left leg
backRight.write(45); // Lift back right leg
frontRight.write(135); // Move front right leg forward
backLeft.write(135); // Move back left leg forward
delay(300);
// Lower front left and back right legs while moving front right and back left to neutral
frontLeft.write(90); // Lower front left leg to neutral
backRight.write(90); // Lower back right leg to neutral
frontRight.write(90); // Move front right leg to neutral
backLeft.write(90); // Move back left leg to neutral
delay(300);
// Lift front right and back left legs while moving front left and back right forward
frontRight.write(45); // Lift front right leg
backLeft.write(45); // Lift back left leg
frontLeft.write(135); // Move front left leg forward
backRight.write(135); // Move back right leg forward
delay(300);
// Lower front right and back left legs while moving front left and back right to neutral
frontRight.write(90); // Lower front right leg to neutral
backLeft.write(90); // Lower back left leg to neutral
frontLeft.write(90); // Move front left leg to neutral
backRight.write(90); // Move back right leg to neutral
delay(300);
}
}
// Function to move the robot backward
void moveBackward() {
for (int i = 0; i < 3; i++) { // Repeat for continuous motion
// Similar pattern as moveForward but in reverse
frontLeft.write(135); // Lift front left leg backward
backRight.write(135); // Lift back right leg backward
frontRight.write(45); // Move front right leg backward
backLeft.write(45); // Move back left leg backward
delay(500);
frontLeft.write(90);
backRight.write(90);
frontRight.write(90);
backLeft.write(90);
delay(500);
frontRight.write(135); // Lift front right leg backward
backLeft.write(135); // Lift back left leg backward
frontLeft.write(45); // Move front left leg backward
backRight.write(45); // Move back right leg backward
delay(500);
frontRight.write(90);
backLeft.write(90);
frontLeft.write(90);
backRight.write(90);
delay(500);
}
}
// Function to run forward
void moveRun() {
for (int i = 0; i < 3; i++) { // Repeat for faster motion
// Similar to moveForward but with faster and slightly exaggerated movements
frontLeft.write(45);
backRight.write(45);
frontRight.write(135);
backLeft.write(135);
delay(250); // Reduced delay for faster movement
frontLeft.write(90);
backRight.write(90);
frontRight.write(90);
backLeft.write(90);
delay(250);
frontRight.write(45);
backLeft.write(45);
frontLeft.write(135);
backRight.write(135);
delay(250);
frontRight.write(90);
backLeft.write(90);
frontLeft.write(90);
backRight.write(90);
delay(250);
}
}
// Function to make the robot sit
void sit() {
frontLeft.write(120); // Adjust front legs to sitting position
frontRight.write(120);
backLeft.write(60); // Adjust back legs to sitting position
backRight.write(60);
delay(1000); // Hold the position
}
// Function to make the robot stand
void stand() {
frontLeft.write(90); // Return legs to standing position
frontRight.write(90);
backLeft.write(90);
backRight.write(90);
delay(500); // Stabilize in standing position
}
// Function to wag the tail
void wagTail() {
for (int i = 0; i < 5; i++) { // Wag 5 times
tail.write(45); // Move tail to the left
delay(200);
tail.write(135); // Move tail to the right
delay(200);
}
tail.write(90); // Return tail to neutral position
}
// Function to simulate barking
void bark() {
for (int i = 0; i < 3; i++) { // Bark 3 times
mouth.write(45); // Open mouth
delay(500);
mouth.write(90); // Close mouth
delay(500);
}
}