In this video, we'll use the Arduino Leonardo-compatible Adafruit ATmega32u4 Featherboard to build a joystick-style mouse that can be used with any USB-equipped computer. This will be built into Project Xyberpunk to replace the Trackpoint-style mouse module in the original design.
Here's the code, for those of you following along at home:
//***************************************************** //Project Xyberpunk v1.0 //Joystick mouse module //Feb 15, 2019 // //CC-BY-SA //Includes some public domain code by David A Mellis & Tom Igoe //(I know I don't have to give credit, but I like to when I can) // //Matthew Eargle //Element14 Presents //element14.com/presents //AirborneSurfer Productions //airbornesurfer.com // //***************************************************** #include <Mouse.h> const int leftButton = 10; const int rightButton = 11; const int xPin = A0; const int yPin = A1; int xValue = 0; // the sensor value int xMin = 1023; // minimum sensor value int xMax = 0; // maximum sensor value int yValue = 0; int yMin = 1023; int yMax = 0; //Refresh rate between polling intervals, lower is smoother movement #define REFRESH_RATE 1 static unsigned long lastRefresh = 0; void setup() { Serial.begin(115200); pinMode(leftButton, INPUT); pinMode(rightButton, INPUT); // turn on LED to signal the start of the calibration period: pinMode(13, OUTPUT); digitalWrite(13, HIGH); // calibrate during the first five seconds while (millis() < 5000) { xValue = analogRead(xPin); yValue = analogRead(yPin); // record the maximum sensor value if (xValue > xMax) { xMax = xValue; } if (yValue > yMax) { yMax = yValue; } // record the minimum sensor value if (xValue < xMin) { xMin = xValue; } if (yValue < yMin) { yMin = yValue; } } // signal the end of the calibration period digitalWrite(13, LOW); Mouse.begin(); } void loop() { digitalWrite(leftButton, HIGH); digitalWrite(rightButton, HIGH); int leftClick = digitalRead(leftButton); int rightClick = digitalRead(rightButton); // left click: if (leftClick == LOW) { // if the mouse is not pressed, press it: if (!Mouse.isPressed(MOUSE_LEFT)) { Mouse.press(MOUSE_LEFT); } } // else the mouse button is not pressed: else { // if the mouse is pressed, release it: if (Mouse.isPressed(MOUSE_LEFT)) { Mouse.release(MOUSE_LEFT); } } // right click: if (rightClick == LOW) { // if the mouse is not pressed, press it: if (!Mouse.isPressed(MOUSE_RIGHT)) { Mouse.press(MOUSE_RIGHT); } } // else the mouse button is not pressed: else { // if the mouse is pressed, release it: if (Mouse.isPressed(MOUSE_RIGHT)) { Mouse.release(MOUSE_RIGHT); } } if ((lastRefresh + REFRESH_RATE) < millis()) { moveMouse(); lastRefresh = millis(); } } void moveMouse() { int xValue = analogRead(A1); int yValue = analogRead(A0); int xMove = (((xValue) - 512) / 50); int yMove = ((-1*((yValue) - 512) / 50)); Serial.print("X: "); Serial.print(xMove); Serial.print(", Y: "); Serial.print(yMove); Mouse.move(xMove, yMove, 0); Serial.println(); }
Top Comments