Created
March 26, 2023 13:48
-
-
Save carneym/a5564309750b9c87468c13dba8dc19f3 to your computer and use it in GitHub Desktop.
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 "Mouse.h" | |
// Joystick variables | |
const int xAxis = A0; // joystick X axis | |
const int yAxis = A1; // joystick Y axis | |
// parameters for reading the joystick: | |
int range = 12; // output range of X or Y movement | |
int responseDelay = 5; // response delay of the mouse, in ms | |
int threshold = range / 4; // resting threshold | |
int center = range / 2; // resting position value | |
// mouse button variables | |
const int mouseButton = 6; | |
unsigned long previousMillis = 0; // will store last time buttonCycleChecked was updated | |
const long interval = 200; // interval between button checks | |
void setup() { | |
// take control of the mouse: | |
pinMode(mouseButton, INPUT_PULLUP); | |
Serial.begin(9600); // open the serial port at 9600 bps: | |
Serial.println("Button test"); | |
Mouse.begin(); | |
} | |
void loop(){ | |
/*********************************** | |
Joystick direction on a 5ms delay | |
***********************************/ | |
// read and scale the two axes: | |
int xReading = readAxis(A0); | |
int yReading = readAxis(A1); | |
// if the mouse control state is active, move the mouse: | |
Mouse.move(xReading, yReading, 0); | |
/*********************************** | |
Left button click on a 200ms loop. | |
***********************************/ | |
unsigned long currentMillis = millis(); // current time | |
if (currentMillis - previousMillis >= interval) { | |
// save the last time you blinked the LED | |
previousMillis = currentMillis; | |
int pushButtonValue = digitalRead(6); | |
if(pushButtonValue == HIGH) { | |
Serial.println("Button NOT pressed"); | |
//if (Mouse.isPressed(MOUSE_LEFT)) { | |
Mouse.release(MOUSE_LEFT); | |
//} | |
} else { | |
Serial.println("Button pressed"); | |
// if the mouse is not pressed, press it: | |
//if (!Mouse.isPressed(MOUSE_LEFT)) { | |
Mouse.press(MOUSE_LEFT); | |
//} | |
} | |
} | |
delay(responseDelay); | |
} | |
/* | |
reads an axis (0 or 1 for x or y) and scales the analog input range to a range | |
from 0 to <range> | |
*/ | |
int readAxis(int thisAxis) { | |
// read the analog input: | |
int reading = analogRead(thisAxis); | |
// map the reading from the analog input range to the output range: | |
reading = map(reading, 0, 1023, 0, range); | |
// if the output reading is outside from the rest position threshold, use it: | |
int distance = reading - center; | |
if (abs(distance) < threshold) { | |
distance = 0; | |
} | |
// return the distance for this axis: | |
return distance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment