// Include the necessary libraries
#include <Joystick.h>
// Define the pin numbers for the joystick axes
const int xAxisPin = A0;
const int yAxisPin = A1;
// Define the pin numbers for the joystick buttons
const int buttonPin1 = 2;
const int buttonPin2 = 3;
// Create a Joystick object
Joystick_ Joystick;
void setup() {
// Initialize the Joystick library
Joystick.begin();
// Set the joystick axis pins as INPUT
pinMode(xAxisPin, INPUT);
pinMode(yAxisPin, INPUT);
// Set the joystick button pins as INPUT_PULLUP
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
}
void loop() {
// Read the analog values from the joystick axes
int xAxisValue = analogRead(xAxisPin);
int yAxisValue = analogRead(yAxisPin);
// Map the analog values to the range expected by the Joystick library
int mappedX = map(xAxisValue, 0, 1023, 0, 127);
int mappedY = map(yAxisValue, 0, 1023, 0, 127);
// Send the joystick values to the computer
Joystick.setXAxis(mappedX);
Joystick.setYAxis(mappedY);
// Read the state of the joystick buttons
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
// Send the button states to the computer
Joystick.setButton(0, !buttonState1);
Joystick.setButton(1, !buttonState2);
// Add a small delay to avoid sending data too quickly
delay(10);
}