wiichuck buttons

Dear all,

I want to use my Wii Nunchuck to control a zumo in future, but first i’m trying to program the arduino to just light a led by pressing the cButton. Here is my code:

#include <ArduinoNunchuk.h>

Servo myservo;  // create servo object to control a servo
 
int pos = 0;    // variable to store the servo position
int analogPin = 4 ; //variable to store cbutton value
int val = 0;
int led = 13;

ArduinoNunchuk nunchuk = ArduinoNunchuk();

#define BAUDRATE 19200

void setup()
{
  Serial.begin(BAUDRATE);
  nunchuk.init();
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(led, OUTPUT);
} 
 
void loop(){
nunchuk.update();
Serial.println(nunchuk.cButton, DEC);
Serial.print(' ');
val = analogRead(analogPin);
if (val==1)
{
  digitalWrite(led, HIGH);
}
}

There is something i do wrong in reading the input from de nunchuck. It is connected on A4 and A5, i don’t know which analog input reads the buttons. I thought my if statement is correct but do I have to define what the arduino must do if val == 0?

And how do i get this code working?

greetz wizard

analogRead() return a value from 0 to 1023 (digitalRead() return only 0 or 1).
Try to print on serial monitor the value of val to check if is really 1

And how do i get this code working?

By writing it correctly, read:-
http://www.gabrielbianconi.com/arduinonunchuk/

Take what that page says with a grain of salt, though.

The library is very simple to use. First you instantiate it:

ArduinoNunchuk nunchuk = ArduinoNunchuk();

Wrong.

ArduinoNunchuk nunchuk;

is the proper way.

There may be other crap on that page. I didn't bother reading the rest of it.

got it:

#include <Wire.h>
#include <ArduinoNunchuk.h>

int led = 13;

ArduinoNunchuk nunchuk;

void setup(){
  Serial.begin(19200);
  nunchuk.init();
  pinMode(led, OUTPUT);
} 
 
void loop(){
nunchuk.update();
int var = nunchuk.cButton;
Serial.println(var);

if (var==1)
{digitalWrite(led, HIGH);
}
else if (var==0)
{digitalWrite(led, LOW);
}
}

Thanks for help all. :smiley:

else if (var==0)

You don’t need that if statement if you get to the else you already know the value of var is equal to zero.

oke I'll change it tonight. And what about the debounce function, do I need to use that? Cause eventually i'm going to use the button for controlling a servo. If I press the button once the servo positions to a specific angle and if i press the button once again it needs to position back to the first angle.

greetz wizard

And what about the debounce function, do I need to use that?

That would depend on how you move the servo. If you just tell it to go to the new position, and go back to reading the switch, then, yes you need debouncing.

If you use a for loop to slowly move the servo, while ignoring the switch, it will have stopped bouncing long before you get around to reading it again.

hi and Happy New Year!!!

I try to use the code but a have a problem. I use the arduino 1.5.4 and give me this back :

In file included from sketch_jan01b.ino:2:
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:9: error: stray ‘\302’ in program
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:9: error: stray ‘\267’ in program
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:9: error: stray ‘\302’ in program
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:9: error: stray ‘\267’ in program
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:407: error: missing terminating " character
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:409: error: missing terminating " character
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:500: error: stray ‘#’ in program
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:500: error: stray ‘#’ in program
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:500: error: stray ‘#’ in program
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:500: error: stray ‘#’ in program
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:4: error: expected unqualified-id before ‘<’ token
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:500: error: expected constructor, destructor, or type conversion before ‘.’ token
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:500: error: expected unqualified-id before ‘<’ token
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:500: error: expected unqualified-id before ‘<’ token
C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:544: error: expected unqualified-id before numeric constant

please help me!!!

i try to download the last wizard code in uno arduino board.... :cold_sweat:

C:\Program Files\Arduino\libraries\ArduinoNunchuk/ArduinoNunchuk.h:9: error: stray '\302' in program

I don't have that file on my computer. You need to post your copy of it.

Did you copy that file from a website, by any chance? If so, there is a possibility that some characters got mangled. For instance, - signs get changes to ems. The compiler knows what a - sign is. It has no idea what an em is.

Single quotes often get mangled, too.

i find the library from internet

Yoy have right. I try download again the zip file and work perfect!!! Thanks for your help.