#include <Servo.h>
// Define the servo pins
int servo1Pin = 9;
int servo2Pin = 10;
int servo3Pin = 11;
// Define the servo objects
Servo servo1;
Servo servo2;
Servo servo3;
// Define the angles of each servo motor in the default position (90 degrees)
int theta1_0 = 90;
int theta2_0 = 90;
int theta3_0 = 90;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Attach the servo objects to the pins
servo1.attach(servo1Pin);
servo2.attach(servo2Pin);
servo3.attach(servo3Pin);
// Move the servos to the default position
servo1.write(theta1_0);
servo2.write(theta2_0);
servo3.write(theta3_0);
// Wait for the servos to reach the default position
delay(1000);
}
void loop() {
// Read the incoming data from the serial communication
if (Serial.available() > 0) {
String data = Serial.readStringUntil('\n');
data.trim();
// Split the data into three parts separated by commas
int commaIndex1 = data.indexOf(',');
int commaIndex2 = data.indexOf(',', commaIndex1+1);
String theta1String = data.substring(0, commaIndex1);
String theta2String = data.substring(commaIndex1+1, commaIndex2);
String theta3String = data.substring(commaIndex2+1);
// Convert the strings to integers
int theta1 = theta1String.toInt();
int theta2 = theta2String.toInt();
int theta3 = theta3String.toInt();
// Move the servos to the specified angles
servo1.write(theta1);
servo2.write(theta2);
servo3.write(theta3);
// Wait for the servos to reach the specified angles
delay(1000);
}
}