// C++ code
//
#include <Servo.h>
unsigned long current_time = 0;
unsigned int intervel = 15;
Servo servo_9;
Servo hello;
Servo p11;
int current_angle1;
int current_angle2;
int current_angle3;
int input;
int input2;
int input3;
void move_motor(Servo *ser, int *current_angle, int *target_angle) {
if (*target_angle > *current_angle) {
*current_angle = *current_angle + 1;
ser->write(*current_angle);
} else if (*target_angle < *current_angle) {
*current_angle = *current_angle - 1;
ser->write(*current_angle);
}
}
void setup() {
Serial.begin(9600);
servo_9.attach(9);
hello.attach(10);
p11.attach(11);
Serial.println("Enter a value between 0 and 180 to set the servo angle:");
}
void loop() {
if (Serial.available() > 0) {
// Read the incoming string as an integer
String inputString = Serial.readStringUntil('\n'); // قراءة حتى السطر الجديد
inputString.trim(); // إزالة المسافات الزائدة أو الأحرف غير المرغوب فيها
// تقسيم المدخلات إلى أجزاء
int commaIndex1 = inputString.indexOf(',');
int commaIndex2 = inputString.lastIndexOf(',');
input = inputString.substring(0, commaIndex1).toInt();
input2 = inputString.substring(commaIndex1 + 1, commaIndex2).toInt();
input3 = inputString.substring(commaIndex2 + 1).toInt();
// Check if the input is within the valid range for a servo angle
/* int target1;
int target2;
int target3;
if (input >= 0 && input <= 180 && input2 >= 0 && input2 <= 180 && input3 >= 0 && input3 <= 180) {
target1 = input;
target2 = input2;
target3 = input3;
}*/
Serial.print("Parsed Input1: ");
Serial.println(input);
Serial.print("Parsed Input2: ");
Serial.println(input2);
Serial.print("Parsed Input3: ");
Serial.println(input3);
}
if (input >= 0 && input <= 180 && input2 >= 0 && input2 <= 180 && input3 >= 0 && input3 <= 180) {
//current_angle1 = servo_9.read();
//current_angle2 = hello.read();
//current_angle3 = p11.read();
if (millis() - current_time >= intervel) {
move_motor(&servo_9, ¤t_angle1, &input);
move_motor(&hello, ¤t_angle2, &input2);
move_motor(&p11, ¤t_angle3, &input3);
current_time = millis();
}
} else {
// Print an error message if the input is out of range
Serial.println("Error: Please enter a value between 0 and 180");
}
Serial.print("servo1 position: ");
Serial.println(current_angle1);
Serial.print("Parsed Input2: ");
Serial.println(current_angle2);
Serial.print("Parsed Input3: ");
Serial.println(current_angle3);
}