// Kayla Frost 300390878
// Sprint 1 Project
// Oct. 28, 2025
// Include servo library for the Servo motor
#include <Servo.h>
// ---------------- Variables ----------------
// LED
const int tled = 11; // turquoise LED connected to pin 11
// Servo
const int ServoPin = 12; // Servo is attached to pin 12
Servo myservo;
int angle = 0; // Servo position (0–180)
// Joystick pins
const int xPin = A0; // horizontal (blue wire)
const int yPin = A1; // vertical (green wire)
const int buttonPin = 9; // joystick button (magenta wire)
// Joystick readings
int xValue;
int yValue;
int buttonState;
// Joystick offsets
int xOffset = 0;
int yOffset = 0;
// Dead zone
const int threshold = 200; // threshold for X movement
// ---------------- Setup ----------------
void setup() {
// LED setup
pinMode(tled, OUTPUT);
digitalWrite(tled, HIGH); // LED off initially
// Servo setup
myservo.attach(ServoPin);
myservo.write(0); // start position
// Joystick pins
pinMode(xPin, INPUT);
pinMode(yPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
// Serial for debugging
Serial.begin(9600);
}
// ---------------- Loop ----------------
void loop() {
// Read joystick
xValue = analogRead(xPin);
yValue = analogRead(yPin);
buttonState = digitalRead(buttonPin);
// LED control (active-low)
if (buttonState == LOW) {
digitalWrite(tled, LOW); // LED ON
} else {
digitalWrite(tled, HIGH); // LED OFF
}
// Center joystick around 0
xOffset = xValue - 512;
yOffset = yValue - 512;
// Servo control (discrete directions)
if (yOffset > 0) { // only move forward
if (xOffset > threshold) {
angle = 90; // Forward + Right
} else if (xOffset < -threshold) {
angle = 0; // Forward + Left
} else {
angle = 45; // Forward straight (center)
}
} else {
angle = 0; // do not move backward
}
myservo.write(angle);
// Debugging info
Serial.print("X: "); Serial.print(xValue);
Serial.print(" | Y: "); Serial.print(yValue);
Serial.print(" | Xoffset: "); Serial.print(xOffset);
Serial.print(" | Yoffset: "); Serial.print(yOffset);
Serial.print(" | Servo: "); Serial.println(angle);
delay(20);
}