// Kayla Frost 300390878
// Sprint 1 Project
// Oct. 28, 2025
//GOAL: check that servo moves and turn on LED
// Include servo library for the Servo motor
#include <Servo.h>
// Define pin number to LED
int tled = 11; // turquoise led connected to pin 11
int ServoPin = 12; // Servo is attached to pin 12
Servo myservo; // The servo motor is called myservo
int pos = 0; // Variable to store servo motor's position (in degrees)
const int up = 0;
const int down = 180;
int xPin = A0; // horiz (blue wire)
//int yPin = A1; // vert (green wire)
int buttonPin = 9; // joystick button (magenta wire)
int xValue;
//int yValue;
int buttonState; // is button pressed or unpressed?
int angle;
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:
/*
//turn on LED for 2 seconds
digitalWrite(tled, LOW);
delay(2000);
// turn off LED for 5 seconds
digitalWrite(tled, HIGH);
delay(5000);
//point servo up and down
myservo.write(up);
delay(500);
myservo.write(down);
delay(500);
*/
xValue = analogRead(xPin);
//yValue = analogRead(yPin);
buttonState = digitalRead(buttonPin); // digital is between pushed and unpushed
//unpushed = high = 1, pushed is opposite
if(buttonState == 0)
{
digitalWrite(tled, LOW);
delay(2000);
}
// Map joystick range to servo angle (0–180 degrees)
angle = map(xValue, 0, 1023, 0, 180);
myservo.write(angle); // Move servo to that angle
// Optional: print for debugging
Serial.print("Joystick: ");
Serial.print(xValue);
Serial.print(" | Servo Angle: ");
Serial.println(angle);
delay(15); // Small delay for stability
//NOTE: this servo moves 180 degrees, not 360 degrees
}