Good evening everybody!
I am currently working on my project regarding a Wii Nunchuck and an Arduino pro Mini.
The goal of this project is to use the Nunchuck for my Oculus Rift. I want to use the joystick to walk around in Skyrim, so I need the joystick to be translated into W, A, S and D. The C and Z buttons have to 'replace' the left and right mouse buttons.
Of course I have done some searching on Google. But everything I have encountered till now doesn't seem to work, or isn't what I am looking for.
Example 1
Example 2
The problem is that I am new to the wonderful world of Arduino and that I am quite unfamiliar with the code it is programmed with.
I however want this to work, as I have unsuccesfully tried to connect the WiiMote to my computer. Using a keyboard when using the Rift is a real burden and doesn't do the immersion any good.
So my question is if anybody can help me out with writing the sketch for my Nunchuck.
Try this code out It has been a long time since I used it but it worked then. It is hard to find code that works for the WIIChuck I put this class together after many failed attempts
#include <NewWiiChk.h>
NewWiiChk WiiChk;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
WiiChk.get_data(); // Load the data from the WiiChuck
WiiChk.print_data();
int XV = WiiChk.joyx();
int YV = WiiChk.joyy();
int ZV = WiiChk.zbutton();
int CV = WiiChk.zbutton();
}
NewWiiChk.cpp (4.7 KB)
NewWiiChk.h (741 Bytes)
zhomeslice:
Try this code out It has been a long time since I used it but it worked then. It is hard to find code that works for the WIIChuck I put this class together after many failed attempts
I suspect that you forgot to tell the OP that the two attached files need to be installed as a library 
His examples used libraries
some of them are the same ones I had to fight to get my library working. I just assumed
Klavkjir new how to add libraries but he can ask and We will help I'm sure 
I can't quite remember what all I changed but I was able to control a robot with a wireless Wiichuck which had many challenges all on its own.
here's a couple clips of the bot in action
and another one of the disk launcher
Thank you guys for your answers! Nice projects, Zhomeslice.
I have succesfully installed the libraries and uploaded the code on my Pro Mini. I however can't seem to find the Arduino pins that have to be connected with the Nunchuck...
Is it possible to convert this sketch to emulate keyboard inputs?
I have managed to find a working sketch that drives two servo's.
Just to confirm the connections and the Nunchuck itself are correct.
Is there any way this sketch can be adjusted for my keyboard purpose?
#include <math.h>
#include "Wire.h"
#include "WiiChuck.h"
#include "Servo.h"
Servo servoRoll;
Servo servoPitch;
WiiChuck chuck = WiiChuck();
void setup() {
Serial.begin(115200);
servoRoll.attach(10);
servoPitch.attach(11);
chuck.begin();
chuck.update();
}
int countLoop = 0;
float rollTot = 0.0;
float pitchTot = 0.0;
int nToAvg = 5;
int lastRollVal = 0, lastPitchVal = 0;
void loop() {
delay(10);
chuck.update();
rollTot += chuck.readRoll();
pitchTot += chuck.readPitch();
if (countLoop == nToAvg) {
int roll = rollTot/nToAvg;
roll = round(roll/5)*5; // Round to the nearest 5 degrees to eliminate some jitter
roll = max(roll, -90);
roll = min(roll, 90);
roll += 90; // Add 90 degrees to make the roll range match the servo range
int pitch = pitchTot/nToAvg;
pitch = round(pitch/5)*5; // Round to the nearest 5 degrees to eliminate some jitter
pitch = max(pitch, 0);
pitch = min(pitch, 180);
// See if writing the values to the servos only if they change will help eliminate jitter
if (roll != lastRollVal) servoRoll.write(roll);
if (pitch != lastPitchVal) servoPitch.write(pitch);
lastRollVal = roll;
lastPitchVal = pitch;
Serial.print("Roll = ");
Serial.print(roll);
Serial.print(", Pitch = ");
Serial.println(pitch);
pitchTot = 0.0;
rollTot = 0.0;
countLoop = 0;
} else {
rollTot += chuck.readRoll();
pitchTot += chuck.readPitch();
countLoop++;
}
}
Klavkjir:
Thank you guys for your answers! Nice projects, Zhomeslice.
I have succesfully installed the libraries and uploaded the code on my Pro Mini. I however can't seem to find the Arduino pins that have to be connected with the Nunchuck...
Is it possible to convert this sketch to emulate keyboard inputs?
The arduino Uno/Pro mini all use the atmega328p chip so analog pins A4 and A5 are used for i2c connections see:
https://www.arduino.cc/en/Main/ArduinoBoardProMini
The Pro Mini has 8 analog inputs,; two (inputs 4 and 5) on holes in the interior of the board. I2C: A4 (SDA) and A5 (SCL). Support I2C (TWI) communication using the Wire library.
in this diagram the A4 pin is labeled incorrectly should be SDA
Other clone Pro Mini boards have the A4 and A5 pins along the edge
i2c usually always requires pullup resisters but it only needs 1 set if the wiichuck has them built in no extras are required but I can't remember. my brief research this morning suggests that the wiichuck has them
Z
Other clone Pro Mini boards have the A4 and A5 pins along the edge
Found them and connected the SCL to A5 and the SDA to A4, as you said.
However, I am not getting any readings on my serial monitor when using your sketch. Is this normal?
I have the nunchuck on a separate 3.3V power source, but am not using any pull-up resistors.
I2C requires pullup resistors to achieve the "off" state of high voltage.
The master & slave can only pull the signals low to create the clock & data - the resistors are needed for a high.
Klavkjir:
Found them and connected the SCL to A5 and the SDA to A4, as you said.
However, I am not getting any readings on my serial monitor when using your sketch. Is this normal?
I have the nunchuck on a separate 3.3V power source, but am not using any pull-up resistors.
Use this to scan the i2c buss for active devices.
// --------------------------------------
// i2c_scanner
//
// Version 1
// This program (or code that looks like it)
// can be found in many places.
// For example on the Arduino.cc forum.
// The original author is not know.
// Version 2, Juni 2012, Using Arduino 1.0.1
// Adapted to be as simple as possible by Arduino.cc user Krodal
// Version 3, Feb 26 2013
// V3 by louarnold
// Version 4, March 3, 2013, Using Arduino 1.0.3
// by Arduino.cc user Krodal.
// Changes by louarnold removed.
// Scanning addresses changed from 0...127 to 1...119,
// according to the i2c scanner by Nick Gammon
// http://www.gammon.com.au/forum/?id=10896
// Version 5, March 28, 2013
// As version 4, but address scans now to 127.
// A sensor seems to use address 120.
//
//
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
//
bool blinkState = false;
#include <Wire.h>
void setup()
{
Wire.begin();
Serial.begin(115200);
Serial.println("\nI2C Scanner");
pinMode(3, OUTPUT);
}
void loop()
{
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address < 127; address++ )
{
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0)
{
Serial.print("I2C device found at address 0x");
if (address<16)
Serial.print("0");
Serial.print(address,HEX);
Serial.println(" !");
blinkState = !blinkState;
digitalWrite(3, blinkState);
nDevices++;
}
else if (error==4)
{
Serial.print("Unknow error at address 0x");
if (address<16)
Serial.print("0");
Serial.println(address,HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}
Again many thanks for your generous help!
I have uploaded the I2C scanner you've sent me, Zhomeslice. In the monitor it is stuck on 'scanning', so I guess what Crossroads said about the resitiors pulling the signals low is what the problem is.
Would a 10K resistor to VCC (3.3v) on both SDA and SCL do the trick?
I have a new update regarding the project; somebody pointed out that only a 32U4 based Arduino boards can be used for HID purposes like these. Do you guys agree that I should continue with an Atmel 32U4 in stead of a 328P?
Klavkjir:
Again many thanks for your generous help!
I have uploaded the I2C scanner you've sent me, Zhomeslice. In the monitor it is stuck on 'scanning', so I guess what Crossroads said about the resitiors pulling the signals low is what the problem is.
Would a 10K resistor to VCC (3.3v) on both SDA and SCL do the trick?
2K ~ 10K should work I wouldn't go below 2K I last used 4.7k
Atmel 32U4 in stead of a 328P?
I'm unfimiliar with the project but either should work with the Wii Nunchuck