// Joystick Pin Definitions
const int JOY1_X = A0;
const int JOY1_Y = A1;
const int JOY1_BTN = 2;
const int JOY2_X = A2;
const int JOY2_Y = A3;
const int JOY2_BTN = 3;
// Standalone Buttons Pin Definitions
const int buttonPins[8] = {4, 5, 6, 7, 8, 9, 10, 11};
void setup() {
// Initialize Serial Communication for debugging
Serial.begin(115200);
// Configure Joystick button pins with internal pull-up resistors
pinMode(JOY1_BTN, INPUT_PULLUP);
pinMode(JOY2_BTN, INPUT_PULLUP);
// Configure the 8 standalone controller buttons
for (int i = 0; i < 8; i++) {
pinMode(buttonPins[i], INPUT_PULLUP);
}
}
void loop() {
// 1. Read Joystick Analog Values (0 to 1023)
int joy1XVal = analogRead(JOY1_X);
int joy1YVal = analogRead(JOY1_Y);
int joy2XVal = analogRead(JOY2_X);
int joy2YVal = analogRead(JOY2_Y);
// 2. Read Joystick Buttons (LOW means pressed because of INPUT_PULLUP)
bool joy1BtnPressed = (digitalRead(JOY1_BTN) == LOW);
bool joy2BtnPressed = (digitalRead(JOY2_BTN) == LOW);
// 3. Print Joystick Data to Serial Monitor
Serial.print("Joy1 X/Y: ");
Serial.print(joy1XVal);
Serial.print(",");
Serial.print(joy1YVal);
Serial.print(" [Btn: ");
Serial.print(joy1BtnPressed ? "PRESSED" : "IDLE");
Serial.print("] | ");
Serial.print("Joy2 X/Y: ");
Serial.print(joy2XVal);
Serial.print(",");
Serial.print(joy2YVal);
Serial.print(" [Btn: ");
Serial.print(joy2BtnPressed ? "PRESSED" : "IDLE");
Serial.print("] | Buttons: ");
// 4. Read and Print Standalone Buttons
for (int i = 0; i < 8; i++) {
bool btnPressed = (digitalRead(buttonPins[i]) == LOW);
Serial.print(btnPressed ? "1" : "0");
if (i < 7) Serial.print(",");
}
// Newline for the next frame of data
Serial.println();
// Small delay to make the serial monitor readable
delay(100);
}