December 22, 2024

#include <Servo.h>
Servo servo;
// Arduino pin numbers
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
int servorotate = 0;
int xvalue = 0;
int yvalue = 0;
void setup() {
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
Serial.begin(9600);
servo.attach(4);
servo.write(90);
}

void loop() {
Serial.print(“Switch: “);
Serial.print(digitalRead(SW_pin));
Serial.print(“\n”);
Serial.print(“X-axis: “);
Serial.print(analogRead(X_pin));
servorotate = (xvalue * 100) / 1023;
xvalue = analogRead(X_pin);
yvalue = analogRead(Y_pin);
int svalue = digitalRead(SW_pin);

if (xvalue > 530)
{
Serial.print(“\n”);

Serial.print(“rotate 160\n”);

servo.write(160);
}
if (xvalue < 510)
{

Serial.print(“\n”);

Serial.print(“rotate -150 \n”);

servo.write(-150);
}
if (svalue == 0)
{
Serial.print(“\n”);

Serial.print(“rotate 90\n”);

servo.write(90);
}

Serial.print(“\n”);
Serial.print(“Y-axis: “);
Serial.println(analogRead(Y_pin));
Serial.print(“\n\n”);
delay(500);
}