0

Need help with Nunchuck code
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Need help with Nunchuck code

by brianzbl on Tue Feb 17, 2009 12:13 am

Hey everyone,

I recently started my first project and I picked up an adapter from the todbot blog to control 4 servos. I cant seem to figure out how to write the code :( I saw a thread here and the code for his project seems very simple. What I would like to start of doing is by geting my servos to all go in one direction when I push the joystick forward, I can do the code for the servo no problem, but I cant figure out the nunchuck part. Obviously I'd like t do other things with them, but I can figure that out on my own. My question is:

Can anyone help me get the code started? I have all the libraries in the thread linked in my explanation. Thanks guys :?
brianzbl
 
Posts: 5
Joined: Tue Feb 17, 2009 12:03 am
Location: Chicago

Re: Need help with Nunchuck code

by Tcepsa on Tue Feb 17, 2009 2:07 pm

Okay, I still haven't gotten around to actually doing anything, so this is all "this seems like it should work to me" but it might give you a place to start ~wry grin~

So, you should probably set up your initial code and setup() similar to chrille's code, except I would recommend that you also uncomment the chuck.calibrateJoy() method since you want to be using the joystick for this.

I don't know anything about servos, so I'm going to use LEDs instead to illustrate the concept; basically just replace the code that turns the LEDs on/off with code to turn the motors on/off and you should be all set. (Note this won't work by itself; you'll need to write the setup() method and other #includes and initializations. Also note, I may have up/down flip-flopped.)
Code: Select all | TOGGLE FULL SIZE
int upLed = 4;
int downLed = 5;
int leftLed = 6;
int rightLed = 7;

int joyThreshold = 10; // So it hopefully won't pick up little movements of the joystick

void loop(){
   chuck.update(); // Read the data from the nunchuck
   if( chuck.readJoyY() < joyThreshold ){ // Stick is being pushed down
      pinMode(downLed, OUTPUT);
      digitalWrite(downLed, HIGH);
   }
   else { // Stick is not being pushed down
      pinMode(downLed, OUTPUT);
      digitalWrite(downLed, LOW);
   }

   if( chuck.readJoyY() > joyThreshold ){ // Stick is being pushed up
      // CODE TO TURN ON ALL SERVOS GOES HERE!
      pinMode(upLed, OUTPUT);
      digitalWrite(upLed, HIGH);
   }
   else { // Stick is not being pushed up
      // CODE TO TURN OFF ALL SERVOS GOES HERE!
      pinMode(upLed, OUTPUT);
      digitalWrite(upLed, LOW);
   }

   if( chuck.readJoyX() < joyThreshold ){ // Stick is being pushed left
      pinMode(leftLed, OUTPUT);
      digitalWrite(leftLed, HIGH);
   }
   else {  // Stick is not being pushed down
      pinMode(leftLed, OUTPUT);
      digitalWrite(leftLed, LOW);
   }

   if( chuck.readJoyX() > joyThreshold ){ // Stick is being pushed right
      pinMode(rightLed, OUTPUT);
      digitalWrite(rightLed, HIGH);
   }
   else {  // Stick is not being pushed right
      pinMode(rightLed, OUTPUT);
      digitalWrite(rightLed, LOW);
   }
}

I hope that helps ^_^
"If you compare yourself with others, you may become vain or bitter; for always there will be greater and lesser persons than yourself. Enjoy your achievements as well as your plans." --Desiderata, by Max Ehrmann
Tcepsa
 
Posts: 26
Joined: Tue Jan 13, 2009 7:14 pm
Location: Mid-Atlantic Region of the USA

Re: Need help with Nunchuck code

by brianzbl on Tue Feb 17, 2009 11:17 pm

Thank you, I have modified my code to get it mostly done, but I still get errors and this seems to be where I have trouble. Thanks to your code though I now understand it better :) Here is my updated code:
Code: Select all | TOGGLE FULL SIZE
#include <math.h>
#include <ServoTimer2.h>
#include "Wire.h"
#include "WiiChuck.h"
//#include "nunchuck_funcs.h"

ServoTimer2 a;    // defins the servo names
ServoTimer2 b;
ServoTimer2 c;
ServoTimer2 d;

int joyThreshold = 10; // So it hopefully won't pick up little movements of the joystick

void setup() {
  //nunchuck_init();
  chuck.begin();
  chuck.update();
  //chuck.calibrateJoy();
 
  a.attach(12);    // attaches all the servos to their proper pins
  b.attach(11);
  c.attach(10);
  d.attach(9);
}
 
void loop() {
  chuck.begin();
  chuck.update(); // Read the data from the nunchuck
   if( chuck.readJoyY() < joyThreshold ){ // Stick is being pushed down
      a.write(750);     // makes all servos go backward
      b.write(2250);
      c.write(750);
      d.write(2250);
     
   }
   else { // Stick is not being pushed down
      a.write(1500);   // all servos stop
      b.write(1500);
      c.write(1500);
      d.write(1500);
   }

   if( chuck.readJoyY() > joyThreshold ){ // Stick is being pushed up
      a.write(2250);    // all servos go forward
      b.write(750);
      c.write(2250);
      d.write(750);
   }
   else { // Stick is not being pushed up
      a.write(1500);   // all servos stop
      b.write(1500);
      c.write(1500);
      d.write(1500);
   }

   if( chuck.readJoyX() < joyThreshold ){ // Stick is being pushed left
      a.write(750);     // values to make the left servos go backward, and the right servos go forward
      b.write(2250);
      c.write(750);
      d.write(2250);
   }
   else {  // Stick is not being pushed down
      a.write(1500);   // servos are stopped
      b.write(1500);
      c.write(1500);
      d.write(1500);
   }

   if( chuck.readJoyX() > joyThreshold ){ // Stick is being pushed right
      a.write(2250);     // values to make the left servos go forward, and the right servos go backward
      b.write(750);
      c.write(750);
      d.write(2250);
   }
   else {  // Stick is not being pushed right
      a.write(1500);   //servos are stopped
      b.write(1500);
      c.write(1500);
      d.write(1500);
   }
}

The error I get is:
In function 'void loop()':
error: 'chuck' was not declared in this scope
All I have done so far was take the code you gave me, put in the oter things from my original servo code, and that other thread. Does anybody else know how else to fix it? I think its close :)
brianzbl
 
Posts: 5
Joined: Tue Feb 17, 2009 12:03 am
Location: Chicago

Re: Need help with Nunchuck code

by Tcepsa on Wed Feb 18, 2009 12:51 am

You're getting that "chuck undefined" error because there's no actual declaration of what chuck is anywhere that the loop() function can "see" it. To fix that, include the line
Code: Select all | TOGGLE FULL SIZE
WiiChuck chuck = WiiChuck();
before the part where you declare the ServoTimers (or immediately afterwards).

Also, as a heads-up, it looks like one of your cases (forward, backward, left, or right) probably won't do what you're expecting it to, but I'll let you see whether you can puzzle it out first. (I usually feel like I've learned more when I figure things out for myself, or at least when I try to figure them out first--but it's also good to be able to get help from others when I'm really stuck or frustrated.)

Aside from the WiiChuck declaration, though, I don't see anything else that jumps out at me as something that will stop your code from compiling and loading and running ^_^
"If you compare yourself with others, you may become vain or bitter; for always there will be greater and lesser persons than yourself. Enjoy your achievements as well as your plans." --Desiderata, by Max Ehrmann
Tcepsa
 
Posts: 26
Joined: Tue Jan 13, 2009 7:14 pm
Location: Mid-Atlantic Region of the USA

Re: Need help with Nunchuck code

by brianzbl on Wed Feb 18, 2009 6:24 pm

I have looked over my code and changed some of the Servo values that I thought were wrong. The code now compiles which is good :) but it does not actually work. I have been looking at other code using the ServoTimer2 library and it looks like I have set it up right. Here is code I have wrote to drive the servos without the Nunchuck (which works fine):
Code: Select all | TOGGLE FULL SIZE
// ServoTimer2 library

#include <ServoTimer2.h>

ServoTimer2 a;   // Sets up the first servo
ServoTimer2 b;   // Sets up the second servo
ServoTimer2 c;   // Sets up the third servo
ServoTimer2 d;   // Sets up a fourth servo

void setup() {   // these codes assign all the pin numbers
  a.attach(12);
  b.attach(11);
  c.attach(10);
  d.attach(9);
}

void loop() {    // these setup the speed and direction of the servos
  a.write(750);
 
  b.write(750);
 
  c.write(2250);
 
  d.write(2250);
}


I can't seem to figure out what else I need to do.
brianzbl
 
Posts: 5
Joined: Tue Feb 17, 2009 12:03 am
Location: Chicago

Re: Need help with Nunchuck code

by nemesisrobot on Wed Feb 18, 2009 6:33 pm

Have you verified that your nunchuk is connected/working properly? Have you tried reading values from the nunchuk and printing them?

Also, I could be wrong but you could try removing
Code: Select all | TOGGLE FULL SIZE
chuck.begin();

from your main loop. I believe it only needs to be called once.
nemesisrobot
 
Posts: 25
Joined: Tue Jan 01, 2008 3:27 am

Re: Need help with Nunchuck code

by Tcepsa on Wed Feb 18, 2009 6:39 pm

I'll second nemesisrobot's comment; it sounds like there's probably something screwy with the nunchuck-related part of the code.

One thing you might try (in addition to the other suggestions) would be to uncomment the line
Code: Select all | TOGGLE FULL SIZE
//chuck.calibrateJoy();
in the setup() function.
"If you compare yourself with others, you may become vain or bitter; for always there will be greater and lesser persons than yourself. Enjoy your achievements as well as your plans." --Desiderata, by Max Ehrmann
Tcepsa
 
Posts: 26
Joined: Tue Jan 13, 2009 7:14 pm
Location: Mid-Atlantic Region of the USA

Re: Need help with Nunchuck code

by brianzbl on Wed Feb 18, 2009 7:01 pm

Thanks guys. I only tested the nunchuck with the serial read a couple days ago. Just plugged it back in to check is I could read the values wit the serial reader, and its not owkring for some reason, so right now I'm going to figure out whats wrong with that.

Edit: Got the nunchuck up and running, code doesn't work however. The servos move for about a second on there own for a little while on and off, but then it just stops. Thanks for all the help though, anymore suggestions would be nice :D
brianzbl
 
Posts: 5
Joined: Tue Feb 17, 2009 12:03 am
Location: Chicago

Re: Need help with Nunchuck code

by Tcepsa on Thu Feb 19, 2009 11:17 am

I don't know specifically what it could be, but the three ways I would break this down for troubleshooting are 1) the nunchuck-related code, 2) the servo-related code, and 3) everything else (i.e. control structures, etc.)

It seems unlikely that it would be 3, because as far as I can tell you don't have any conditions that would cause it to stop executing the loop() function, nor do I see anything that seems like it would cause it to enter an infinite loop within the loop() function. (Though depending on how you've changed your code since you last posted it, that may no longer be the case).

So that leaves 1 and 2 as the likely suspects, and I would probably put in Serial code to get a better idea of what's going on (e.g. Serial.println() calls inside each of the if and each of the else blocks to let you know that's where it is, along with one at the top of the loop to let you know that it is still looping. When the servos stop working, see whether it's still executing the contents of whatever if block had turned them on. If it is, then you know the problem is with the servo code, since the nunchuck is still properly telling the code to execute that loop. If it isn't still executing the loop, then the problem is probably the nunchuck.

Finally, I took a quick look at the ServoTimer2 library and found this comment towards the start of the .h file:
ServoTimer2.h wrote: A servo is activated by creating an instance of the Servo class passing the desired pin to the attach() method.
The servo is pulsed in the background to the value most recently written using the write() method.
If I'm interpreting that correctly, it means that when you want to turn the servos on you write to them once and then they should keep going until you write them with a new value. In other words, it looks like you don't need to be calling a.write(750) every loop, and this might be causing some of the difficulty (it seems like it shouldn't, but anytime I can't find a clear problem immediately off the bat I like to look for ways that I might be using the code that are slightly out of spec and could therefore lead to unintended behavior).

So I would recommend creating a variable to track whether the servos are on or off, and only execute the code to turn them on when they are off, and only execute the code to turn them off when they are on.

Hopefully something in here will prove useful! ^_^
"If you compare yourself with others, you may become vain or bitter; for always there will be greater and lesser persons than yourself. Enjoy your achievements as well as your plans." --Desiderata, by Max Ehrmann
Tcepsa
 
Posts: 26
Joined: Tue Jan 13, 2009 7:14 pm
Location: Mid-Atlantic Region of the USA

Re: Need help with Nunchuck code

by brianzbl on Thu Feb 19, 2009 11:11 pm

Alright, I got some help on the Arduino forums and my new updated code is this (its ony part of it, I will write the rest when I get it working):
Code: Select all | TOGGLE FULL SIZE
#include <math.h>
#include <ServoTimer2.h>
#include "Wire.h"
#include "WiiChuck.h"
//#include "nunchuck_funcs.h"

ServoTimer2 a;    // defines the servo names
ServoTimer2 b;
ServoTimer2 c;
ServoTimer2 d;

WiiChuck chuck = WiiChuck();

int joyThreshold = 10; // So it hopefully won't pick up little movements of the joystick
int x;

void setup() {
  //nunchuck_init();
  chuck.begin();
  chuck.update();
  chuck.calibrateJoy();
 
  a.attach(12);    // attaches all the servos to their proper pins
  b.attach(11);
  c.attach(10);
  d.attach(9);
}
 
void loop() {
  chuck.begin();
  chuck.update(); // Read the data from the nunchuck
  x = chuck.readJoyX(); // need to first assign X
  if (x < 0){ // Move one direction
    a.write(2250);
    b.write(750);
    c.write(750);
    d.write(2250);
   }
   else if (x > 10) {// move another direction
   a.write(750); 
   b.write(2250);
   c.write(2250);
   d.write(750);
   }
   else { // i.e. 100 <= x <= 160
   a.write(1500);
   b.write(1500);
   c.write(1500);
   d.write(1500);
   }
}


I'm going to experiment with the servo commands tomarrow when I have time but I think that is actually the problem now :)
brianzbl
 
Posts: 5
Joined: Tue Feb 17, 2009 12:03 am
Location: Chicago

Please be positive and constructive with your questions and comments.