Hi everyone!
For a while now, I'm trying to get my Wii nunchuck to work as a gamepad for VR purposes using the 32U4 based Arduino pro Micro.
I am an absolute beginner in programming, but I've managed to write a working sketch, based on 'if' - 'else' statements. The program runs quite well, the only problem is that the 'E' and 'Shift' key do not stay pressed down.
I have the following key configuration in mind:
Joystick right: Press and hold down D
Joystick left: Press and hold down A
Joystick up: Press and hold down W
Joystick down: Press and hold down S
Accelerometer (XYZ): press and hold down left mouse button
C-button: Press and hold down E
Z-button: Press and hold down Shift key
Both C and Z: Press and hold down left mouse button
Who can tell me what I'm doing wrong?
#include <Keyboard.h>
#include <Mouse.h>
#include <Wire.h>
#include "ArduinoNunchuk.h"
ArduinoNunchuk nunchuk = ArduinoNunchuk();
void setup()
{
Serial.begin(19200);
nunchuk.init();
Wire.begin();
Mouse.begin();
Keyboard.begin();
}
void loop()
//Joystick bewegingen lopen------------------------------------------------------
{
if (nunchuk.analogX <= 70 || nunchuk.analogX >= 170 || nunchuk.analogY <= 70 || nunchuk.analogY >= 170)
{
if (nunchuk.analogX <= 70)
{
Keyboard.press('A');
nunchuk.update();
}
else {
Keyboard.release('A');
}
if (nunchuk.analogX >= 170)
{
Keyboard.press('D');
nunchuk.update();
}
else {
Keyboard.release('D');
}
if (nunchuk.analogY <= 70)
{
Keyboard.press('S');
nunchuk.update();
}
else {
Keyboard.release('S');
}
if (nunchuk.analogY >= 170)
{
Keyboard.press('W');
nunchuk.update();
}
else {
Keyboard.release('W');
}
nunchuk.update();
}
//C- en Z-knoppen----------------------------------------------------------------
if (nunchuk.cButton == 1 && nunchuk.zButton == 0 || nunchuk.cButton == 0 && nunchuk.zButton == 1)
{
if (nunchuk.cButton == 1 || nunchuk.zButton == 1)
{
if (nunchuk.cButton == 1)
{
Keyboard.press(129);
nunchuk.update();
}
else {
Keyboard.release(129);
}
if (nunchuk.zButton == 1)
{
Keyboard.press('E');
nunchuk.update();
}
else {
Keyboard.release('E');
}
}
nunchuk.update();
}
//Indrukken van beide C- en Z-knoppen-----------
if (nunchuk.cButton >= 1 && nunchuk.zButton >= 1) //Voert de volgende regels uit als C en Z zijn ingedrukt
{
Mouse.press(MOUSE_LEFT);
nunchuk.update();
}
else {
Mouse.release(MOUSE_LEFT);
}
nunchuk.update();
//Accelerometers Y en Z--------------------------
if (nunchuk.accelY >= 1000)
{
Mouse.press(MOUSE_LEFT);
delay(250);
}
else {
Mouse.release(MOUSE_LEFT);
}
nunchuk.update();
if (nunchuk.accelZ >= 1000)
{
Mouse.press(MOUSE_LEFT);
delay(250);
}
else {
Mouse.release(MOUSE_LEFT);
}
nunchuk.update();
//-------------------------------------------------------------------------------
}