Total Noob trying to learn Arduino starting with a Nunchuk project

Hi all,

I am trying to make a project where I use the Nunchuk data from the joystick and the accelerometer, at the moment I don't know what for but I am just trying to get the data feeding into the serial data in the IDE and am following this Youtube Tutorial and have fallen at the first hurdle pretty much. I have the adaptor, the nunchuk, the Mega and the code and files which I have extracted from https://github.com/infusion/Fritzing/tree/master/Nunchuk

Most likely the reason is I am using an Arduino Mega2560 instead of an Uno as is used on the tutorial, so the 4 pins from the Nunchuk to the Mega are a little awkward.

When compiling and uploading the code I got from the video, I get this error:

And the overall code is as follows:

#include <Wire.h>
#include "Nunchuk.h"

void setup() {

Serial.begin(9600);
Wire.begin();
    nunchuk_init();
    nunchuk_init_power(); // A1 and A2 is power supply

Serial.println("Starting");

}

void loop() {

if (nunchuk_read()) {
    Serial.print("joyX: ");
    Serial.print(nunchuk_joystickX());
    Serial.print(", joyY: ");
    Serial.print(nunchuk_joystickY());
    Serial.print(", accX: ");
    Serial.print(nunchuk_accelX());
    Serial.print(", accY: ");
    Serial.print(nunchuk_accelY());
    Serial.print(", buttonZ: ");
    Serial.print(nunchuk_buttonZ());
    Serial.print(", buttonC: ");
    Serial.println(nunchuk_buttonC());
} else {
	Serial.println("_");
}
delay(10);

}

I've got a couple of wires going from GND and 5v on the Mega but I assume the power while it is plugged into a PC will come from the PC.

If anybody is willing to help, please explain to me in most simple terms as I'm very new and just starting to pick up technical terms bit by bit.

Many thanks in advance
Ben

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

When posting code or error messages please use code tags and do not post screenshots as they are almost always unreadable

OK thanks for the tips.

as a newcomer trying this project as the first step is very ambitious.

This is like you are able to ride a normal bike on the street and have quite some years of experience with that and some training for a 50 miles bike-trip.

And now you change to a BMX trying to do backflips in the half-pipe.
In the bike-example it is very clear: you gonna hurt yourself seriously.

You start with small hops over a two feet high ramp to get a "feeling" for:

  • how does the BMX-bike jump?
  • how do I have to position my body for a smooth landing.
  • how do I have to move my body and bike as a unit to gain some extra height when jumping over the ramp?

etc.
with programming an arduino there is danger to hurt yourself as long as you stay away from mains-voltage but you might not be able to analyse what is the bug?

So as some first steps
Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

Hi Stefan,

I am trying to be ambitious :slight_smile: I'm aware it's a tricky step but thought if I manage to do that then it will be a solid start to help future projects.

Thanks for the link and I am aware I need to learn the basics and I am by reading books and trying to understand all the functions, will read through your link now.

Thanks
Ben

In Nunchuk.h:

#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__) // Only Arduino UNO
/**
 * Use normal analog ports as power supply, which is useful if you want to have all pins in a row
 * Like for the famous WiiChuck adapter
 * @see https://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
 */
static void nunchuk_init_power() {
    // Add power supply for port C2 (GND) and C3 (PWR)
    PORTC &= ~_BV(PORTC2);
    PORTC |= _BV(PORTC3);
    DDRC |= _BV(PORTC2) | _BV(PORTC3);
    delay(100);
}
#endif

It looks like nunchuk_init_power() should be called only if you're using the adapter mentioned in the video, and I'm guessing that it is expected that the adapter is used with an Uno. You're using a mega, so if you want to use the adapter (and the pinout matches the Uno for the A4 and A5 pins, which I'm quite sure it does) you should be able to uncomment the
#if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328P__)
and
#endif lines around static void nunchuk_init_power. If you don't want to use the adapter, I'd guess that you can just power the nunchuk from the 5v pin on the mega, though this isn't a great idea for a long-term setup.

OK great thank you so much for that breakdown, I'll get an Uno and give it another whirl.

Many thanks
Ben

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.