#include <Servo.h>
// Servo definitions
Servo servo1; // Create servo object for servo 1
int joyY = A1; // Analog input pin for joystick Y-axis
int centerValue = 512; // Assuming 10 bit ADC (0-1023), the center is around 512
int lastServoAngle = 90; // Start position of servo
int threshold = 10; // Ignore minor movements
void setup()
{
servo1.attach(9); // Attach servo 1 to digital pin 9
servo1.write(lastServoAngle); // Set servo to initial position
}
void loop()
{
int valY = analogRead(joyY); // Read joystick Y-axis
// Check if joystick movement is significant
if (abs(valY - centerValue) > threshold)
{
// Map joystick movement directly to a small range of servo movement
int servoChange = map(valY, 0, 1023, -5, 5); // Small incremental change
int newServoAngle = lastServoAngle + servoChange;
// Constrain the servo position to avoid overstepping its bounds
newServoAngle = constrain(newServoAngle, 0, 180);
// Write the new angle to the servo
servo1.write(newServoAngle);
// Update the last known servo position
lastServoAngle = newServoAngle;
}
delay(50); // Small delay for stability
}