Hi everyone,
For quite a while I have been working on my USB HID Nunchuk project, designed for VR purposes.
Also discussed earlier in this topic
Goal
The aim of the project is to use 2 Wii Nunchuks (left and right hand) as substitute for a keyboard and mouse or game controller while playing Skyrim with my Oculus Rift. Thus creating one serious immersive game experience! Believe me. The choice for the Nunchuk is based on its ergonomic shape, that is has a joystick, physical buttons and an accelerometer.
How it works
At this moment the concept works by using an Arduino Pro Micro (connected directly to the Nunchuk) which reads and interprets the I2C signal that are outputted by the Nunchuk (using this sketch). Because of software limitations, I make use one Arduino for each Nunchuck .
The axis of the joystick emulates w, d, a and s, the buttons emulate the mouse buttons and the accelerometers are used for ie. swinging the sword, casting spells and blocking with your shield
Problem
The biggest issue I am experiencing are the wires. It has been a hassle to route the USB cables from the pc to the Arduino’s, each time I wanted to use them. After many hours of desk research and developing, on how to deal with the wires, I stumbled upon the NRF24L01 module, a nifty little 2.4GHz module , commonly being used in ordinary devices as keyboards and mice. I already have two Arduino’s ‘talking’ to each other using a basic sketch, a LED, and a switch, so I know it works!
My question
The next step is to use the NRF24L01 module to send the readily interpreted I2C signals from the Arduino (connected to the Nunchuck) to another Arduino, connected to the pc via usb. (see chart below).
My skill level is novice, but I am confident this is feasible. I would like to share my findings with the community and reach out for some guidance on how to edit my sketch in order to send the readily interpreted I2C data over the NRF24L01 module to another Arduino.
My code
#include <Keyboard.h>
#include <Mouse.h>
#include <Wire.h>
#include "ArduinoNunchuk.h"
int joyKeyDown = (-1);
int buttonDown = (-1);
ArduinoNunchuk nunchuk = ArduinoNunchuk();
void setup()
{
Serial.begin(19200);
nunchuk.init();
}
void loop()
{
int newJoyKeyDown = (-1);
int newButtonDown = (-1);
nunchuk.update();
//// Joystick
// joystick left
if (nunchuk.analogX <= 70)
{
newJoyKeyDown = 'a';
}
// joystick right
else if (nunchuk.analogX >= 170)
{
newJoyKeyDown = 'd';
}
// joystick up
if (nunchuk.analogY >= 170)
{
newJoyKeyDown = 'w';
}
// joystick down
else if (nunchuk.analogY <= 70)
{
newJoyKeyDown = 's';
}
// Z-Button
if (nunchuk.zButton == 1)
{
newJoyKeyDown = 'q';
}
// C-Button
if (nunchuk.cButton == 1)
{
newJoyKeyDown = 'e';
}
// Both C- and Z-Buttons
if (nunchuk.zButton == 1 && nunchuk.cButton == 1)
{
newButtonDown = MOUSE_RIGHT;
}
// Accelerometers
if (nunchuk.accelY >= 1000 || nunchuk.accelX >= 1000)
{
newButtonDown = MOUSE_RIGHT;
delay(25);
}
// if a key is down and we have a new key to press
if ( (newJoyKeyDown > 0) && (newJoyKeyDown != joyKeyDown) )
{
if ( joyKeyDown > 0 )Keyboard.release(joyKeyDown);
Keyboard.press(newJoyKeyDown); // press new key
joyKeyDown = newJoyKeyDown; // remember this key
}
else if ( (newJoyKeyDown < 0) && (joyKeyDown > 0) ) {
// no keys down, release pressed key
Keyboard.release(joyKeyDown);
joyKeyDown = (-1);
}
//For mouse functions
if ( (newButtonDown > 0) && (newButtonDown != buttonDown) )
{
if ( buttonDown > 0 )Mouse.release(buttonDown);
Mouse.press(newButtonDown); // press mouse
buttonDown = newButtonDown; // remember this key
}
else if ( (newButtonDown < 0) && (buttonDown > 0) ) {
// no keys down, release pressed key
Mouse.release(buttonDown);
buttonDown = (-1);
}
}