code

All posts tagged code

Here is the code I used on the receive side (Arduino USB board with Adafruit XBee adapter) connected to the steering servo and speed control on an old RC-10T truck:

#include "Servo.h"
#include "NewSoftSerial.h"

int joyx,joyy = 135;
int accx,accy = 125;
int zbut,cbut = 0;
int ledpin = 6;
int wheelServo = 9;
int motorServo = 10;
int lightvalue = 255;
char nunchuckData;
int val = 0;
int tempval = 0;
int wheelPos;
int motorPos;
int buttonState = 0;
int lightMode = 0;

Servo steer;
Servo move;

NewSoftSerial xbee(2,3);  //NewSoftSerial mySerial(int rx, int tx);

void setup()
{
//    Serial.begin(57600);
//    Serial.print("Nunchuck ready\n");
    xbee.begin(57600);

    steer.attach(9);
    move.attach(10);
    pinMode(ledpin, OUTPUT);
}

void loop() {
  if (zbut) {
    if (joyy < 130) {
      motorPos = 92+((135-joyy)/5);
      if (motorPos > 135)
        motorPos = 135;
      move.write(motorPos);
    }
    if (joyy > 140) {
      motorPos = 92-((joyy-135)/5);
      if (motorPos < 45)
        motorPos = 45;
      move.write(motorPos);
    }
  }
  else {
    if ((joyy > 130) && (joyy < 140)) {
      move.write(92);
    }
  }
  if (accx > 125) {
    wheelPos = 90-(accx-125);
    if (wheelPos < 45)
      wheelPos = 45;
    steer.write(wheelPos);
  }
  if (accx < 125) {
    wheelPos = 90+(125-accx);
    if (wheelPos > 135)
      wheelPos = 135;
    steer.write(wheelPos);
  }

  if (cbut != buttonState) {          // the button state has changed!
    if (cbut == 1) {                // check if the button is pressed
      if (lightMode == 0) {          // light is off
        lightMode = 1;               // light is on!
        digitalWrite(ledpin, HIGH);
      } else {
        lightMode = 0;               // light is on!
        digitalWrite(ledpin, LOW);
      }
    }
  }
  buttonState = cbut;               // save the new state in our variable

  while(xbee.available()) {
    tempval = xbee.read();
    if (tempval=='x')    {
     nunchuckData='x';
     val=0;
    }
    else if (tempval=='y')    {
      nunchuckData='y';
      val=0;
    }
    else if (tempval=='X')    {
      nunchuckData='X';
      val=0;
    }
    else if (tempval=='Y')    {
      nunchuckData='Y';
      val=0;
    }
    else if (tempval=='Z')    {
      nunchuckData='Z';
      val=0;
    }
    else if (tempval=='C')    {
      nunchuckData='C';
      val=0;
    }
    else if (tempval >='0' && tempval < = '9')    {
      val=val * 10;
      val=val + (tempval - '0');

      if (nunchuckData=='x'){
        joyx=val;
      }
      else if (nunchuckData=='y'){
        joyy=val;
      }
      else if (nunchuckData=='X'){
        accx=val;
      }
      else if (nunchuckData=='Y'){
        accy=val;
      }
      else if (nunchuckData=='Z'){
        zbut=val;
      }
      else if (nunchuckData=='C'){
        cbut=val;
      }
    }
  }  //end of input in serial buffer
}

The NewSoftSerial library is a great way to use any two Arduino pins as another serial port.

Here is the code that I used on the Arduino Pro Mini in the Wireless Wii Nunchuck:

#include "Wire.h"
#include "nunchuck_funcs.h"

int loop_cnt=0;

byte accx,accy,zbut,cbut,joyy,joyx;
int ledPin = 13;

void setup()
{
    Serial.begin(57600);
    nunchuck_setpowerpins();
    nunchuck_init(); // send the initilization handshake
    pinMode(ledPin, OUTPUT);
}

void loop()
{
  digitalWrite(ledPin, HIGH);   // set the LED on   

    if( loop_cnt > 50 ) { // every 100 msecs get new data
        accx = 120;
        accy = 120;

        loop_cnt = 0;

        nunchuck_get_data();

        accx  = nunchuck_accelx(); // ranges from approx 70 - 182
        accy  = nunchuck_accely(); // ranges from approx 65 - 173
        zbut = nunchuck_zbutton();
        cbut = nunchuck_cbutton();
        joyy = nunchuck_joyy();
        joyx = nunchuck_joyx();

        Serial.print("x");
        Serial.print((byte)joyx,DEC);
        Serial.print("y");
        Serial.print((byte)joyy,DEC);
        Serial.print("X");
        Serial.print((byte)accx,DEC);
        Serial.print("Y");
        Serial.print((byte)accy,DEC);
        Serial.print("Z");
        Serial.print((byte)zbut,DEC);
        Serial.print("C");
        Serial.println((byte)cbut,DEC);
    }
    loop_cnt++;
    delay(1);
}

And the nunchuck functions code can be found here.