// Kayla Frost 300390878
// Sprint 1 Project
// Oct. 28, 2025
// Include servo library for the Servo motor
#include <Servo.h>
// Define pin number to LED
const int tled = 11; // turquoise led connected to pin 11
const int ServoPin = 12; // Servo is attached to pin 12
Servo myservo; // The servo motor is called myservo
int angle = 0; // Variable to store servo motor's angle position (in degrees)
const int xPin = A0; // horiz (blue wire)
const int yPin = A1; // vert (green wire)
const int buttonPin = 9; // joystick button (magenta wire)
int xValue;
int yValue;
int buttonState; // is button pressed or unpressed?
int xOffset = 0;
int yOffset = 0;
float angleRad = 0.0;
float angleDeg = 0.0;
void setup() {
// put your setup code here, to run once:
//Define LED as an output
pinMode(tled, OUTPUT);
//Initially, turn off LED
digitalWrite(tled, HIGH);
//Set up servo motor
myservo.attach(ServoPin); // attach servo motor to its pin (pin 12)
myservo.write(0); // set initial servo position to be 0 degrees
Serial.begin(9600);
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
// put your main code here, to run repeatedly:
// read joystick
xValue = analogRead(xPin);
yValue = analogRead(yPin);
buttonState = digitalRead(buttonPin);
// LED control
if (buttonState == LOW) //if the button is pressed
{
digitalWrite(tled, LOW); // LED ON when pressed
}
else
{
digitalWrite(tled, HIGH); // LED OFF when not pressed
}
// servo motor control
if (yValue==0 && xValue>= 500 && xValue<= 520)
{
myservo.write(180);
}
if (yValue==1023 && xValue>= 500 && xValue<= 520)
{
myservo.write(90);
}
if (xValue==0 && yValue>= 500 && yValue<= 520)
{
myservo.write(135);
}
if (xValue==1023 && yValue>= 500 && yValue<= 520)
{
myservo.write(45);
}
Serial.print("X: "); Serial.print(xValue);
Serial.print(" | Y: "); Serial.print(yValue);
Serial.print('\n');
delay(20);
}