Wii Nunchuck and Arduino Mega 2560

I thought it would be cool to hook up a Wii Nunchuck to my Arduino.  Who knows how I could use this – a servo rotating, an RC car, RC Submarine, almost anything I guess.

Well, I tried multiple different programs…and only one that worked correctly.  I’m a simple developer, I like to plug in code and have it work – so what did I do?  I tried multiple programs in order to get it to work with minimal work from me… and I found the answer.

http://modelrail.otenko.com/arduino/wii-nunchuck-arduino-mega-model-railway-fun

In case the code from that post goes away, here it is in all its glory:

#include 
#include "nunchuck_funcs.h"
#define PWM_INPUT_PIN_1 2
#define PWM_INPUT_PIN_2 3
#define PWM_ENABLE_PIN_1 7
int throttle = 0;
int loop_cnt=0;
void setup() {
  Serial.begin(19200);
  nunchuck_setpowerpins();
  nunchuck_init(); // send the initilization handshake
}
void loop() {
  if( loop_cnt > 100 ) { // every 100 msecs get new data
    loop_cnt = 0;
    nunchuck_get_data();
  }
  loop_cnt++;
  throttle = nunchuck_joyx() - 127;
  if (throttle -10) throttle = 0;
  UpdateTrackPower();

  //I added the following line
  nunchuck_print_data();
}
void UpdateTrackPower() {
  if (throttle == 0) {
    digitalWrite(PWM_INPUT_PIN_1, LOW);
    digitalWrite(PWM_INPUT_PIN_2, LOW);
  } else {
    if (throttle > 0) {
      digitalWrite(PWM_INPUT_PIN_1, HIGH);
      digitalWrite(PWM_INPUT_PIN_2, LOW);
    } else if (throttle < 0) {
      digitalWrite(PWM_INPUT_PIN_1, LOW);
      digitalWrite(PWM_INPUT_PIN_2, HIGH);
    }
  }
  analogWrite(PWM_ENABLE_PIN_1, abs(throttle));
}


I did add one line in order to display the values.  Now, I can do multiple other projects...press the 'C' or 'Z' button and turn on or off blinking of the LED... who knows tomorrow may be world peace.

And as I was putting this blog entry together I found that the original code was posted here to make a Wii Nunchuck Controlled Model Train: http://www.instructables.com/id/Wii-Nunchuk-Controlled-Model-Train/step3/Software-or-Firmware/

Well, now you know and knowing is half the battle.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.