Stuck: Wireless Wii Nunchuck + Sabertooth 2x25

I’m trying to drive/control 2 motors using the wireless wii nunchuck as my joystick and the Sabertooth 2x25 as my motor shield. I’ve been able to get the nunchuck code from this thread: http://forum.arduino.cc/index.php/topic,22056.0.html to work and print actual data to the Serial Monitor but I can’t figure out how to tell the Arduino to use those values for motor control.

My setup is:
Arduino Mega 2560
Sabertooth 2x25 V2
Wireless Wii Nunchuck
(2) DC Motors

The R/C Microcontroller example code from Dimension Engineering loads and does what it’s supposed to but I’m not sure how to merge it with my Wii Nunchuck code. http://www.dimensionengineering.com/info/arduino

Any help is GREATLY appreciated, as I’ve been searching the web and forum for a couple of weeks now and haven’t been able to locate a solution.

My current code is attached. You’ll notice I’ve tried to add in the sample code from DE for the Sabertooth but it doesn’t seem to have any effect on the motors nor serial monitor.

MRFy_Drive_via_Mega_and_wiinunchuck.ino (8.93 KB)

What do you see in the serial monitor and how do you expect to use that data to control the servos?

Paul thank you for replying!

The serial monitor shows the following at "center" with no buttons pressed:

Ident=0 0 A4 20 0 0 
Finished setup
128	127	443	453	717	1	1	
128	127	443	453	717	1	1	
128	127	443	453	717	1	1	
128	127	443	453	717	1	1	
128	127	443	453	717	1	1	
128	127	443	453	717	1	1	
128	127	443	453	717	1	1	
128	127	475	496	719	1	1
...

The first column is the Joystick X (0-127 = Left & 129-255= right) and the second column being Joystick Y (0-126 = Down & 128-255= Up).

I would like to tell the servos to go forward when Y is up, reverse when Y is down and be able to turn Left/Right with Joy X...

The two values of interest are printed by:

  Serial.print (joy_x_axis, DEC);
  Serial.print ("\t");

  Serial.print (joy_y_axis, DEC);

So, why can't you use those variables to control the servos?

That is exactly what I’d like to do, but I don’t know how to do that. When I’ve tried to reference any of those variables after/outside the void print() function I get an error code they weren’t defined in this scope, which is why I resisted them in a “void sabertooth” function which resulted in no response from the servos.

void sabertooth()
{
  //int power;
  int joy_x_axis = outbuf[0];
  int joy_y_axis = outbuf[1];
  int accel_x_axis = outbuf[2] * 2 * 2;
  int accel_y_axis = outbuf[3] * 2 * 2;
  int accel_z_axis = outbuf[4] * 2 * 2;
  int z_button = 0;
  int c_button = 0;
  
  
  if (z_button == 1 && c_button == 1) //if no buttons pressed steer via joystick
  {
   if (joy_y_axis >= 129) //Up postion (center is 128)
   {
    map(joy_y_axis, 0, 255, 0, 180); //Drive forward
    //for (power = 0; power <= 180; power ++)
     ST1.write(joy_y_axis);
     ST2.write(joy_y_axis);
     delay(20);
     Serial.print("Forward");
   }
  }
}
    map(joy_y_axis, 0, 255, 0, 180); //Drive forward

map() returns a value, which you discard. You might as well not bother calling the function.

Forget this complicated sketch. Can you move the servos in a simple sketch with hardcoded values?

This simple sketch from DE works just fine with my setup.

// Sweep Sample
// Copyright (c) 2012 Dimension Engineering LLC
// See license.txt for license details.

#include <Servo.h>

Servo ST1, ST2; // We'll name the Sabertooth servo channel objects ST1 and ST2.
                // For how to configure the Sabertooth, see the DIP Switch Wizard for
                //   http://www.dimensionengineering.com/datasheets/SabertoothDIPWizard/start.htm
                // Be sure to select RC Microcontroller Mode for use with this sample.
                //
                // Connections to make:
                //   Arduino Pin 9  ->  Sabertooth S1
                //   Arduino Pin 10 ->  Sabertooth S2
                //   Arduino GND    ->  Sabertooth 0V
                //   Arduino VIN    ->  Sabertooth 5V (OPTIONAL, if you want the Sabertooth to power the Arduino)
                //
                // Sabertooth accepts servo pulses from 1000 us to 2000 us.
                // We need to specify the pulse widths in attach(). 0 degrees will be full reverse, 180 degrees will be
                // full forward. Sending a servo command of 90 will stop the motor. Whether the servo pulses control
                // the motors individually or control throttle and turning depends on your mixed mode setting.

// Notice these attach() calls. The second and third arguments are important.
// With a single argument, the range is 44 to 141 degrees, with 92 being stopped.
// With all three arguments, we can use 0 to 180 degrees, with 90 being stopped.
void setup()
{
  ST1.attach( 9, 1000, 2000);
  ST2.attach(10, 1000, 2000);
}

void loop()
{
  int power;
  
  // Ramp both servo channels from 0 to 180 (full reverse to full forward),
  // waiting 20 ms (1/50th of a second) per value.
  for (power = 0; power <= 180; power ++)
  {
    ST1.write(power);
    ST2.write(power);
    delay(20);
  }
  
  // Now go back the way we came.
  for (power = 180; power >= 0; power --)
  {
    ST1.write(power);
    ST2.write(power);
    delay(20);
  }
}

After considering your comments yesterday, I removed my sabertooth function and added this code at the end of the “void print ()” and the motors seem to be responding to the joystick now but the responsiveness seems to be delayed between when the joystick is moved and the motors responding, sometimes as much as 5+ seconds.

if (z_button == 1 && c_button == 1) //if no buttons pressed steer via joystick
  {
   if (joy_y_axis >= 150) //Up postion (center is 127)
   {
     ST1.write(150);
     ST2.write(150);
     Serial.print("Forward");
   }
   if (joy_y_axis == 127) //centered
   {
     ST1.write(90);
     ST2.write(90);
     Serial.print("stopped");
   }
   if (joy_y_axis <= 110) //down
   {
     ST1.write(50);
     ST2.write(50);
     Serial.print("Reverse");
   }
   if (joy_x_axis <= 110) //left
   {
     ST1.write(84);
     ST2.write(130);
     Serial.print("left");
   }
   if (joy_x_axis >= 150) //right
   {
    ST1.write(130); 
    ST2.write(84);
    Serial.print("right");
   }