#include <Arduino.h>
int joyX, joyY; // Variables to store joystick values
int buttonState = 0;
int lastButtonState = 0;
bool isMouseMode = true;
void setup() {
Serial.begin(9600);
// Initialize the switch pins
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
}
void loop() {
// Read the joystick values
joyX = analogRead(26);
joyY = analogRead(27);
// Read the switch state
buttonState = digitalRead(14);
// If the switch is in mouse mode
if (isMouseMode) {
// Use joystick values for mouse movement
int mouseX = map(joyX, 0, 65535, -10, 10);
int mouseY = map(joyY, 0, 65535, -10, 10);
Serial.print("Mouse X: ");
Serial.print(mouseX);
Serial.print("\tMouse Y: ");
Serial.println(mouseY);
// Check if the switch is toggled to directional mode
if (buttonState == LOW && lastButtonState == HIGH) {
isMouseMode = false;
}
} else {
// In directional mode
if (buttonState == HIGH && lastButtonState == LOW) {
// Toggle back to mouse mode
isMouseMode = true;
}
// Check joystick direction
if (joyX < 10000) {
Serial.print("KEY LEFT ARROW");
} else if (joyX > 55000) {
Serial.print("KEY RIGHT ARROW");
}
if (joyY < 10000) {
Serial.print("KEY UP ARROW");
} else if (joyY > 55000) {
Serial.print("KEY DOWN ARROW");
}
}
// Store the last switch state
lastButtonState = buttonState;
}
Loading
pi-pico-w
pi-pico-w