Created
April 26, 2023 18:55
-
-
Save squirelo/82fc8ce6271b8e55ccb1cdf17eaf7d5a to your computer and use it in GitHub Desktop.
wheelchair bluetooth demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <BleMouse.h> // Include the bleMouse library | |
BleMouse bleMouse; | |
const int joystickXPin = 26; // Pin for the X-axis of the joystick | |
const int joystickYPin = 36; // Pin for the Y-axis of the joystick | |
const int toggleButtonPin = 27; // Pin for the toggle button | |
const int sensitivity = 10; // Sensitivity for mouse movements | |
int xValue = 0; // Value for the X-axis of the joystick | |
int yValue = 0; // Value for the Y-axis of the joystick | |
bool mouseClickState = false; // State of the mouse click | |
bool toggleButtonState = false; // State of the toggle button | |
bool lastToggleButtonState = false; // Last state of the toggle button | |
void setup() { | |
bleMouse.begin(); // Initialize the bleMouse library | |
Serial.begin(115200); // Initialize the serial communication | |
while (! Serial) delay(1); // Wait for the serial communication to be established | |
pinMode(joystickXPin, INPUT); // Set the X-axis pin of the joystick as an input | |
pinMode(joystickYPin, INPUT); // Set the Y-axis pin of the joystick as an input | |
pinMode(toggleButtonPin, INPUT_PULLUP); // Set the toggle button pin as an input with a pull-up resistor | |
} | |
void loop() { | |
xValue = analogRead(joystickXPin); // Read the analog value from the X-axis pin of the joystick | |
Serial.print(xValue); | |
yValue = analogRead(joystickYPin); // Read the analog value from the Y-axis pin of the joystick | |
Serial.print(yValue); | |
toggleButtonState = !digitalRead(toggleButtonPin); // Read the digital value from the toggle button pin | |
const int deadzone = 6; // Deadzone range, centered around the middle value | |
int xMouse = map(xValue, 0, 4095, sensitivity, -sensitivity); // Map the X-axis value to the range of mouse movement from -sensitivity to +sensitivity | |
int yMouse = map(yValue, 0, 4095, sensitivity, -sensitivity); // Map the Y-axis value to the range of mouse movement from -sensitivity to +sensitivity | |
xMouse = constrain(xMouse, -sensitivity, sensitivity); | |
yMouse = constrain(yMouse, -sensitivity, sensitivity); | |
// Ignore joystick input within the deadzone range | |
if (abs(xMouse) < deadzone / 2) xMouse = 0; | |
if (abs(yMouse) < deadzone / 2) yMouse = 0; | |
if (abs(xMouse) > 0 || abs(yMouse) > 0) { | |
bleMouse.move(xMouse, yMouse); // If the mapped values are outside the deadzone, move the mouse by xMouse and yMouse | |
} | |
if (toggleButtonState && !lastToggleButtonState) { | |
mouseClickState = !mouseClickState; | |
} | |
if (mouseClickState) { | |
bleMouse.press(MOUSE_LEFT); // If the mouseClickState is true, press the left button of the mouse | |
} else { | |
bleMouse.release(MOUSE_LEFT); // If the mouseClickState is false, release the left button of the mouse | |
} | |
lastToggleButtonState = toggleButtonState; // Update the last toggle button state | |
delay(10); // Add a small delay to avoid overwhelming the loop | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment