Author Options:

Why can't I compile the following Arduino code? Answered

This always stops at: nunchuck_get_data(); and the error on the bottom says: 'nunchuck_get_data' was not declared in this scope.  I do not understand that message.  I got the code from Arduino Cook Book Edition 2 and I have the Arduino Uno.  It's so discouraging some times.  I am trying to get going on this stuff and I can't seem to get a lot of code to work;

Here is the code fornd on page 131-132 of the book:

* WiichuckSerial
* Uses Nunchuck Library discussed in recipe 16.5
* sends comma-seperated values for data
* Label string separated by commas can be used by receiving program
* to identify fields

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

// values to add to the sensor to get zero reading when centered
int offsetX, offsetY, offsetZ;

#include <Wire.h>
#include "Nunchuck.h"
void setup()
  nunchuckInit(); // send the intitialization handshake
  nunchuckRead; // ignore the first time
void loop()
  boolean btnC = nunchuckGetValue(wii_btnC);
  boolean btnZ = nunchuckGetValue(wii_btnZ);
  if(btnC) {
    offsetX = 127 nunchuckGetValue(wii_accelX);
    offsetY = 127 nunchuckGetValue(wii_accelY);

void printAccel(int value,int offset)
  Serial.print(adjReading(value, 127-50, 127+50, offset));

void printJoy(int value)
  Serial.print(adjReading(value,0, 255, 0));

void printButton(int value)
  if( value != 0)
     value = 127;

int adjReading( int value, int min, int max, int offset)
  value = constrain(value + offset, min, max);
  value = map(value, min, max, -127, 127);
  return value;




Best Answer 5 years ago

I found an excerpt from the Arduino Cookbook (16.5 Creating a Library That Uses Other Libraries) online. It lists the contents of Nunchuck.h, but no function called nunchuck_get_data() exists in the include file. Here are the function prototypes in the include:

enum nunchuckItems { wii_joyX, wii_joyY, wii_accelX, wii_accelY, wii_accelZ, wii_btnC, wii_btnZ, wii_ItemCount };
void nunchuckSetPowerpins();
void nunchuckInit();
void nunchuckRequest();
bool nunchuckRead();
char nunchuckDecode (uint8_t x);
int nunchuckGetValue(int item);

I don't have access to the book. Is it possible you've lifted function names from another chapter, and another .cpp file? The nunchuck_get_data() function might be part of a higher-level program, and not in the library itself...

Do you have the nunchuck library in the correct folder?

I'm not sure! It doesn't really tell me where to put it.

It's so discouraging some times.  I am trying to get going on this stuff and I can't seem to get a lot of code to work;

Part of the art (and it is an art) of programming is developing the skills to be TRULY methodical, and rigorous. It is a mental discipline. Start at the top and ALWAYS question your assumptions. 


5 years ago

amongst other possibilities, this:


will definitely stop things after you get your function declarations fixed. You're missing a ')' at the end of the line.

Always check for missing parenthesis and semicolons.

Also, you do it in a few spots.

and this:
nunchuckRead; // ignore the first time

will not call the function without (). That'll give a strange error I suspect.

Not sure what happened here, operator seems to be missing:
offsetX = 127 nunchuckGetValue(wii_accelX);

open nunchuck.h and see if that function is defined...

a quick google found this function

int nunchuck_get_data()
int cnt=0;
Wire.requestFrom (0x52, 6); // request data from nunchuck
while (Wire.available ()) {
// receive byte as an integer
nunchuck_buf[cnt] = nunchuk_decode_byte(Wire.receive());
nunchuck_send_request(); // send request for next data payload
// If we recieved the 6 bytes, then go print them
if (cnt >= 5) {
return 1; // success
return 0; //failure

It looks like you're trying to include those libraries twice as well... something doesnt add up.

Make sure you have the library in the right place, and that it has that function