#include <Arduino.h>
int joyX, joyY; // Variables to store joystick values
int buttonState = HIGH; // Initialize to HIGH
bool isMouseMode = true;
void setup() {
Serial.begin(9600);
// Initialize the switch pin
pinMode(3, INPUT_PULLUP);
// Initialize joyX and joyY to the center position
joyX = 0; // Adjust to your joystick's center value
joyY = 0; // Adjust to your joystick's center value
}
void loop() {
// Read the joystick values
joyX = analogRead(A0);
joyY = analogRead(A1);
// Read the switch state
buttonState = digitalRead(3);
// Toggle between mouse and directional mode when the switch is LOW
if (buttonState == LOW) {
isMouseMode = !isMouseMode;
delay(200); // Debounce the switch
}
// Adjusted threshold values for better detection
int threshold = 100; // Adjust this value as needed
// If the switch is in mouse mode
if (isMouseMode) {
// Use joystick values for mouse movement
int mouseX = map(joyX, 0, 1023, -512, 512);
int mouseY = map(joyY, 0, 1023, -512, 512);
Serial.print("Mouse X: ");
Serial.print(mouseX);
Serial.print("\tMouse Y: ");
Serial.println(mouseY);
} else {
// In directional mode
if (joyX > (512 + threshold)) {
Serial.println("KEY LEFT ARROW");
} else if (joyX < (512 - threshold)) {
Serial.println("KEY RIGHT ARROW");
}
if (joyY > (512 + threshold)) {
Serial.println("KEY UP ARROW");
} else if (joyY < (512 - threshold)) {
Serial.println("KEY DOWN ARROW");
}
}
}