#define JOY_X A0 // Joystick X-axis
#define JOY_Y A1 // Joystick Y-axis
#define JOY_SW 2 // Joystick button
#define BTN_SELECT 5 // Button Select
#define BTN_PAUSE 3 // Button Pause
#define LED_SELECT 6 // LED Select
#define LED_PAUSE 4 // LED Pause
// Fungsi untuk debounce tombol
bool debounce(int pin) {
bool state = digitalRead(pin); // Baca status tombol
delay(50); // Tunggu sebentar untuk debouncing
return digitalRead(pin) == state;
}
void setup() {
// Inisialisasi Joystick
pinMode(JOY_X, INPUT);
pinMode(JOY_Y, INPUT);
pinMode(JOY_SW, INPUT_PULLUP); // Pull-up internal untuk joystick switch
// Inisialisasi Button
pinMode(BTN_SELECT, INPUT_PULLUP); // Button Select
pinMode(BTN_PAUSE, INPUT_PULLUP); // Button Pause
// Inisialisasi LED
pinMode(LED_SELECT, OUTPUT); // LED Select
pinMode(LED_PAUSE, OUTPUT); // LED Pause
// Mulai komunikasi serial
Serial.begin(9600);
}
void loop() {
// Baca Joystick
int x = analogRead(JOY_X);
int y = analogRead(JOY_Y);
bool joystickPressed = !digitalRead(JOY_SW); // Tombol Joystick
// Output posisi joystick
Serial.print("Joystick X: ");
Serial.print(x);
Serial.print(" | Y: ");
Serial.print(y);
Serial.print(" | Pressed: ");
Serial.println(joystickPressed);
// Cek status tombol Select
if (debounce(BTN_SELECT) == LOW) {
digitalWrite(LED_SELECT, HIGH); // Nyalakan LED Select
Serial.println("Select button pressed");
} else {
digitalWrite(LED_SELECT, LOW); // Matikan LED Select
}
// Cek status tombol Pause
if (debounce(BTN_PAUSE) == LOW) {
digitalWrite(LED_PAUSE, HIGH); // Nyalakan LED Pause
Serial.println("Pause button pressed");
} else {
digitalWrite(LED_PAUSE, LOW); // Matikan LED Pause
}
delay(100); // Delay untuk memberi waktu pembacaan tombol dan joystick
}