Help with project

I recently bought an arduino uno, adafruit motorshield v1.1. I built a robot and since i dont know anything about programing, I managed to make it follow a single path.

I am here asking for a code you could give so I can use a WiiChuck to control it, and how to connect the wiichuck adapter. I use a tank like rotation. Thank you.

Don't plug your adapter in upside down.

Above is a photo of the receiver of a wireless Nunchuk. I wondered what smelt strange and it was the receiver melting from being connected upside down.

I haven't been able to find a good third party PlayStation 2 controller but Nyko makes some nice wireless Nunchuks. Here's a review of sorts of the Nyko Nunchuk.

Thanks for the link groundfungus. I've been wanting to use a Nunchuk with with an Arduino myself.

@CarlosCampa, Please use a better subject titles. "Help with project" doesn't tell us much.

I got this proyect to control 2 DC motors with the wiichuck and since I dont know anything about programming, I was wondering if someone could help me out with a code.

CarlosCampa: I got this proyect to control 2 DC motors with the wiichuck and since I dont know anything about programming, I was wondering if someone could help me out with a code.

No one will write it for you. Start by working out how you want to control the motors, perhaps a library and a driver board/shield, look at examples on running motors, then write your own code to do that. If you strike problems, you can ask for help along the way.

Then, when you are competent at controlling the motors, add the WiiChuck code to use the WiiChuck library. The library will come with examples.

Since you are apparently still working on this project, I'll point out the Instructable tutorial suggests using the 5V supply with the Nunchuk. IMO, this is a bad idea. The Nunchuk is a 3.3V device. I suggest powering it with 3.3V.

As OldSteve pointed out in your other thread on this same topic, you should break your project into parts and learn to control one thing at a time and then join it together.

Here's a thread which might help learn to use your Adafruit shield.

Do not cross-post. Threads merged.

I managed to control the Motorshiel with no problem. Talking about the WiiChuck, i know to read and control Z and C buttons. Can someone help me out with the joystick? Just X and Y axis.

CarlosCampa: I managed to control the Motorshiel with no problem. Talking about the WiiChuck, i know to read and control Z and C buttons. Can someone help me out with the joystick? Just X and Y axis.

What program are you using to read the buttons?

The code I've looked at doesn't seem complete.

My first attempt using code I could actually get to compile output all max values "255" and "1023".

I've used Nunchucks with a Propeller microcontroller many times so I'm attempting to see why the Propeller code works but the Arduino code doesn't.

So far I don't have any success to report. In fact after I made some changes, the code now appears to freeze.

I'm still working on this.

If you have code that works, I hope you post it.

I used the code from this Instructable as a starting point. As I mentioned, the code originally didn’t output meaningful data but the code below works as expected with my controller.

Here’s the code.

/*
 * 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()
{
  Serial.begin (115200);
  Wire.begin(0x52);		// join i2c bus with address 0x52
  nunchuck_init(); // send the initialization handshake
  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(" X\t Y\taX\taY\taZ\tC\tZ");
}

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);
  Wire.endTransmission(); // stop transmitting
  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 with my Nunchuck looks like this.

Nunchuck ID = 0 0 A4 20 0 0 

 X	 Y	aX	aY	aZ	C	Z
129	126	269	541	559	1	1	
129	126	269	541	559	1	1	
129	126	269	541	559	1	1	
129	126	269	541	559	1	1	
129	126	269	541	559	1	1	
129	126	269	541	559	1	1	
129	126	269	541	559	1	1	
129	126	269	541	559	1	1	
128	126	269	541	559	1	1	
129	126	269	541	559	1	1	
129	126	269	541	559	1	1

If you don’t want serial output, set the constant DEBUG_FLAG to zero.

There are a couple delays in the initialization section but once the program is running aren’t any calls to “delay”. The code should not block other parts of the code.

The constant “NUNCHUCK_READ_INTERVAL” determines how often the Nunchuck is read. I have it set to 20 ms (I’m not sure the serial output can keep up with this rate).