At first: thanks for the help!
I tried your code, unfortunately it didn't work. I suspect that the code stops when it reaches Wire.endTransmission(); Here is the adjusted code I used to debug it: (it prints text on the serial line when it reaches a certain point in the script), (it prints in setup() and in nunchuck_init())
/*
* Nunchuck or Nunchuk Demo Code
* by Duane Degn
* November 14, 2015
*
* Based on code found found:
* http://www.instructables.com/id/Wii-Nunchuck-as-general-purpose-controller-via-Ard/
* The original code has lots of problems.
*/
// 3.3V output on Arduino to Nunchuck
// GND
// SDA to analog pin 4
// SCK to analog pin 5
// the middle upper and middle lower contacts of nunchuck plug are not used
//Derived from code developed by Chad Phillips, see: http://windmeadow.com/node/42
// For info on wiring up the chuck to the Arduino, download Bionic Arduino Class 4 from TodBot blog:
// http://todbot.com/blog/2007/11/24/bionic-arduino-class-notes-3-4/
// Notes on accelerometer output in six positions. When a value is listed as "500" this aproximate.
// Other values are not exact and will likely vary from Nunchuck to Nunchuck.
// output with Nunchuck in normal position = 500, 500, 734
// output with Nunchuck with port (left) side down = 260, 500, 500
// output with Nunchuck with starboard (right) side down = 755, 526, 488
// output with Nunchuck with front end down = 500, 762, 500
// output with Nunchuck with front end up = 500, 279, 500
// output with Nunchuck upside down = 500, 500, 255
#include <Wire.h>
const byte EXPECTED_BYTES = 6;
byte outbuf[EXPECTED_BYTES]; // array to store arduino output
int joy_x_axis = outbuf[0];
int joy_y_axis = outbuf[1];
int accel_x_axis = outbuf[2] << 2 | ((outbuf[5] >> 2) & 3);
int accel_y_axis = outbuf[3] << 2 | ((outbuf[5] >> 4) & 3);
int accel_z_axis = outbuf[4] << 2 | ((outbuf[5] >> 6) & 3);
const unsigned long NUNCHUCK_READ_INTERVAL = 20;
unsigned long lastNunchuckRead;
const boolean DEBUG_FLAG = 1;
byte c_button = (outbuf[5] >> 1) & 1;
byte z_button = outbuf[5] & 1;
void setup()
{
delay(5000);
Serial.begin (115200);
Wire.begin(0x52); // join i2c bus with address 0x52
Serial.println("step1");
nunchuck_init(); // send the initialization handshake
Serial.println("step4");
if (DEBUG_FLAG)
{
getId();
printHeading();
}
lastNunchuckRead = millis();
}
void loop()
{
checkNunchuckTime(DEBUG_FLAG);
// Control robot based on Nunchuck data.
}
void checkNunchuckTime(boolean debugFlag)
{
if (millis() - lastNunchuckRead > NUNCHUCK_READ_INTERVAL)
{
lastNunchuckRead += NUNCHUCK_READ_INTERVAL;
byte i = readNunchuck();
if (i < EXPECTED_BYTES && debugFlag)
{
printError(EXPECTED_BYTES, i);
}
else if (debugFlag)
{
printData();
}
}
}
byte readNunchuck()
{
byte i = 0;
Wire.requestFrom (0x52, EXPECTED_BYTES); // request data from nunchuck
while (Wire.available())
{
outbuf[i++] = Wire.read();
}
if (i >= EXPECTED_BYTES)
{
joy_x_axis = outbuf[0];
joy_y_axis = outbuf[1];
accel_x_axis = outbuf[2] << 2 | ((outbuf[5] >> 2) & 3);
accel_y_axis = outbuf[3] << 2 | ((outbuf[5] >> 4) & 3);
accel_z_axis = outbuf[4] << 2 | ((outbuf[5] >> 6) & 3);
c_button = (outbuf[5] >> 1) & 1;
z_button = outbuf[5] & 1;
}
send_zero(); // This appears to be required.
return i;
}
void printData ()
{
/* Include these commands if you want to watch the data values streaming from chuck in the serial viewer window on your PC*/
Serial.print(joy_x_axis, DEC);
Serial.print("t");
Serial.print(joy_y_axis, DEC);
Serial.print("t");
Serial.print(accel_x_axis, DEC);
Serial.print("t");
Serial.print(accel_y_axis, DEC);
Serial.print("t");
Serial.print(accel_z_axis, DEC);
Serial.print("t");
Serial.print(c_button, DEC);
Serial.print("t");
Serial.print(z_button, DEC);
Serial.print("t");
/* Uncomment this section for zero paded binary output of outbuf[5].
byte testBit = 128;
Serial.print("outbuf[5] = ");
while (outbuf[5] < testBit && testBit > 1) // zero pad binary output
{
Serial.print("0");
testBit = testBit >> 1;
}
Serial.print(outbuf[5], BIN); */
Serial.println();
}
void printHeading()
{
Serial.println();
Serial.println(" Xt YtaXtaYtaZtCtZ");
}
void printError(byte bytesExpected, byte bytesReceived)
{
Serial.print("Error. The program expected to receive ");
Serial.print(bytesExpected, DEC);
Serial.print(" bytes but instead received ");
Serial.print(bytesExpected, DEC);
Serial.println(" bytes.");
}
void nunchuck_init()
{
Wire.beginTransmission(0x52); // transmit to device 0x52
//Wire.write (0x40); // sends memory address
//Wire.write (0x00); // sends sent a zero.
Wire.write (0xF0);
Wire.write (0x55);
Serial.println("step2");
Wire.endTransmission(); // stop transmitting
Serial.println("step3");
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write (0xFB);
Wire.write (0x00);
Wire.endTransmission(); // stop transmitting
delay(10);
}
void getId()
{
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write(0xFA);
Wire.endTransmission(); // stop transmitting
Serial.print("Nunchuck ID = ");
Wire.requestFrom (0x52, EXPECTED_BYTES);
while (Wire.available())
{
byte nunchuckByte = Wire.read();
Serial.print(nunchuckByte, HEX);
Serial.print(" ");
}
Serial.println();
delay(10);
}
void send_zero()
{
Wire.beginTransmission(0x52); // transmit to device 0x52
Wire.write (0x00); // sends one byte
Wire.endTransmission(); // stop transmitting
}
The output is the following:
step1
step2
//it stops here, it starts printing again when the nunchuck is removed
step3
step4
Nunchuck ID =
X Y aX aY aZ C Z
Error. The program expected to receive 6 bytes but instead received 6 bytes.
Error. The program expected to receive 6 bytes but instead received 6 bytes.
Error. The program expected to receive 6 bytes but instead received 6 bytes.
Error. The program expected to receive 6 bytes but instead received 6 bytes.
Error. The program expected to receive 6 bytes but instead received 6 bytes.
Error. The program expected to receive 6 bytes but instead received 6 bytes.
Error. The program expected to receive 6 bytes but instead received 6 bytes.
Error. The program expected to receive 6 bytes but instead received 6 bytes.
//When the nunchuck is plugged in again, the program stops to print again
This page warns for the 3.3v on i2c with a arduino micro:
Even the Arduino Leonardo and Arduino Micro (both of which use the ATmega32U4) require 0.7 * Vcc for 'high' level of the I2C bus, although their normal digital inputs are LVTTL and require only 1.9V. For 5V powered micros, the I2C high level should be at least 3.5V to meet specification; using the 3.3V I2C-bus for a 5V Arduino is living on the edge, hoping that the 3.3V will be recognized as a digital high.
Is the arduino micro on this point different form the arduino uno?