From d0e08458f49f843317121f83dd4ce8af1ad0aa96 Mon Sep 17 00:00:00 2001 From: thomasballantine Date: Fri, 19 May 2023 21:17:49 +0000 Subject: [PATCH] Added .ino file --- nunchuck_mouse.ino | 125 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 nunchuck_mouse.ino diff --git a/nunchuck_mouse.ino b/nunchuck_mouse.ino new file mode 100644 index 0000000..d7b9640 --- /dev/null +++ b/nunchuck_mouse.ino @@ -0,0 +1,125 @@ +/* + * AUTHOR: Thomas Ballantine + * Website: thomasballantine.com + * Licence: GNU Lesser General Public License v3.0 + * Created: 19/05/2023 + * + * A program to enable usage of a Nintendo Wii Nunchuk as a computer mouse + * All design and test carried out using an MKR1000 and 2 nunchuks, one genuine, + * one fake, on Arduino IDE 1.8.19 + * For SAMD usage, roll back to Arduino SAMD boards 1.6.21 in board manager as per + * https://github.com/arduino/ArduinoCore-samd/issues/423 + * + * Inspiration: + * https://www.raphnet.net/electronique/wusbmote/index_en.php + * + * Please get in touch if you make this spaghetti work better + * At present all values are hard coded, adjust as necessary for your needs + * + * TODO: Implement proper scrolling. Perhaps use accelerometer? + */ + + + +#include //Can be found in Library manager +#include //Arduino builtin library +int mouse_x_axis = 0; +int mouse_y_axis = 0; +int flag_1 = 0; +int flag_z = 0; +int flag_c = 0; +//https://github.com/arduino/ArduinoCore-samd/issues/423 Must use Arduino SAMD boards 1.6.21 to compile +Accessory nunchuck; + +void setup() { + Mouse.begin(); + nunchuck.begin(); + if (nunchuck.type == Unknown) { + nunchuck.type = NUNCHUCK; + } + +} + +void loop() { + nunchuck.readData(); // Read inputs + +// Determining mouse movement amounts from joystick values +//Only 2 levels of speed here, nunchuk doesn't have great sensitivity + if (nunchuck.getJoyX() > 140) mouse_x_axis = 1; + if (nunchuck.getJoyX() > 170) mouse_x_axis = 3; + if (nunchuck.getJoyX() < 116) mouse_x_axis = -3; + if (nunchuck.getJoyX() < 88) mouse_x_axis = -1; + if (nunchuck.getJoyY() > 140) mouse_y_axis = -1; + if (nunchuck.getJoyY() > 170) mouse_y_axis = -3; + if (nunchuck.getJoyX() < 116) mouse_y_axis = 1; + if (nunchuck.getJoyY() < 88) mouse_y_axis = 3; + + + + +//Lots of ifs here, to set and unset the press/release flags +//Works much smoother than when it was using the mouse.click() method + if (nunchuck.getButtonZ() == 1 && nunchuck.getButtonC() == 0){ + Mouse.press(MOUSE_RIGHT); + flag_z = 1; + if (flag_1 == 1){ + Mouse.release(MOUSE_MIDDLE); + flag_1 = 0; + } + if (flag_c == 1){ + Mouse.release(); + flag_c = 0; + } + } + + + if (nunchuck.getButtonC() == 1 && nunchuck.getButtonZ() == 0){ + Mouse.press(); + flag_c = 1; + if (flag_1 == 1){ + Mouse.release(MOUSE_MIDDLE); + flag_1 = 0; + } + if (flag_z == 1){ + Mouse.release(MOUSE_RIGHT); + flag_z = 0; + } + } + + + if (nunchuck.getButtonC() == 1 && nunchuck.getButtonZ() == 1){ + Mouse.press(MOUSE_MIDDLE); + flag_1 = 1; + if (flag_z == 1){ + Mouse.release(MOUSE_RIGHT); + flag_z = 0; + } + if (flag_c == 1){ + Mouse.release(); + flag_c = 0; + } + + } + + if (nunchuck.getButtonC() == 0 && nunchuck.getButtonZ() == 0){ + if (flag_1 == 1){ + Mouse.release(MOUSE_MIDDLE); + flag_1 = 0; + } + if (flag_c == 1){ + Mouse.release(); + flag_c = 0; + } + if (flag_z == 1){ + Mouse.release(MOUSE_RIGHT); + flag_z = 0; + } + } + + + + Mouse.move(mouse_x_axis, mouse_y_axis,0); //sending mouse movement data + mouse_x_axis = 0; //resetting the mouse movements + mouse_y_axis = 0; + delay(5); // polling at about 200Hz, plenty fast enough for anyone +}