#include <Servo.h> // Include Servo library
#include <BetterJoystick.h> // Include Joystick library (ensure it's installed)
// Declare Servo object
Servo myServo;
// Declare Joystick object with X, Y, and Switch pins
Joystick joystick(A0, A1, 8);
int mappedX, mappedY; // Variables for mapped joystick positions
int analogpin = A0;
int ledPin = 6; // Use a PWM-compatible pin for the LED
void setup() {
// Enable serial communication for debugging
Serial.begin(9600);
Serial.println("Begin!");
// Set up LED pin
pinMode(ledPin, OUTPUT);
// Attach servo to pin 9
myServo.attach(9);
// Initialize servo position to 0 degrees
myServo.write(0);
// Allow servo to initialize
delay(15);
}
void loop() {
// Retrieve and map joystick positions
retrievePositions();
// Control servo with constrained input
myServo.write(mappedX - 37);
// Adjust LED brightness based on joystick Y-axis input
analogWrite(ledPin, 255-mappedY);
delay(10); // Small delay to stabilize readings
}
void retrievePositions() {
// Read raw joystick inputs and map to desired ranges
mappedX = joystick.x(0, 255); // Map X-axis to 0–255
mappedY = joystick.y(0, 255); // Map Y-axis to 0–255
// Serial.println(mappedY); // Debugging puroses
}