#include <Servo.h>
Servo horizontalServo; // Horizontal servo
Servo verticalServo; // Vertical servo
int horizontalPos = 0; // Initial position for horizontal servo
int verticalPos = 0; // Initial position for vertical servo
void setup() {
Serial.begin(9600);
horizontalServo.attach(9); // Pin for horizontal servo
verticalServo.attach(10); // Pin for vertical servo
horizontalServo.write(horizontalPos); // Set initial position
verticalServo.write(verticalPos); // Set initial position
}
void loop() {
if (Serial.available() > 0) {
char command = Serial.read();
// Control horizontal movement (A = left, D = right)
if (command == 'A') {
horizontalPos = horizontalPos - 90; // Move left (90 degrees)
if (horizontalPos < 0) horizontalPos = 0; // Limit to 0 degrees
} else if (command == 'D') {
horizontalPos = horizontalPos + 90; // Move right (90 degrees)
if (horizontalPos > 180) horizontalPos = 180; // Limit to 180 degrees
}
// Control vertical movement (W = up, S = down)
if (command == 'W') {
verticalPos = verticalPos + 90; // Move up (90 degrees)
if (verticalPos > 180) verticalPos = 180; // Limit to 180 degrees
} else if (command == 'S') {
verticalPos = verticalPos - 90; // Move down (90 degrees)
if (verticalPos < 0) verticalPos = 0; // Limit to 0 degrees
}
// Set servo positions
horizontalServo.write(horizontalPos);
verticalServo.write(verticalPos);
// Wait for a short time to let the servo adjust
delay(500);
// Return servos to center position (0, 0)
horizontalPos = 0;
verticalPos = 0;
horizontalServo.write(horizontalPos);
verticalServo.write(verticalPos);
delay(500); // Wait before the next command
}
}