// Analog input pins for X and Y axes of the joystick
#define JOYSTICK_X_PIN 34
#define JOYSTICK_Y_PIN 35
// Digital input pin for the joystick button
#define JOYSTICK_BUTTON_PIN 32
// Timing variables to control the reading interval
unsigned long lastReadTime = 0;
const unsigned long readInterval = 1000; // Interval set to 1 seconds
void setup() {
Serial.begin(115200);
// The button uses an internal pull-up resistor.
// When pressed, it connects to GND (reads LOW).
pinMode(JOYSTICK_BUTTON_PIN, INPUT_PULLUP);
}
void loop() {
unsigned long currentTime = millis();
// This block ensures that readings are taken every 1 seconds
if (currentTime - lastReadTime >= readInterval) {
lastReadTime = currentTime;
// Read analog values from the joystick's X and Y axes
int xValue = analogRead(JOYSTICK_X_PIN);
int yValue = analogRead(JOYSTICK_Y_PIN);
// The button is active LOW, so we check for a LOW signal to detect a press
bool buttonPressed = digitalRead(JOYSTICK_BUTTON_PIN) == LOW;
// Output the values to the Serial Monitor for analysis
Serial.println("=== Joystick ===");
Serial.print("X: ");
Serial.print(xValue);
Serial.print(" | Y: ");
Serial.print(yValue);
Serial.print(" | Button: ");
Serial.println(buttonPressed ? "Pressed" : "Released");
Serial.println("================\n");
}
}