LEGO - Lego Electronic GizmO

 
Justin Kaput
Kris Mobley
Domenic Policicchio III
 
We made a tank out of LEGOs and our Arduino board that is controlled by a Wiimote nunchuck.
 
We borrowed a library from http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/ that allows us to read the Wiichuck using our Analog In pins on the Arduino. We used switch-case statements to help us steer. It uses the joystick to control speed and steering and the z button to help us decide when it moves.
 
 Arduino Code
/* Adapted from
 * WiiChuckDemo -- 
 *
 * 2008 Tod E. Kurt, http://thingm.com/
 *
 */

#include <Wire.h>
#include "nunchuck_funcs.h"  // You must download this library 

int loop_cnt=0;

byte joyx,joyy,zbut,cbut;
int motor1Pin1 = 5;                  // declare our variables
int motor1Pin2 = 6;
int motor2Pin1 = 11;
int motor2Pin2 = 10;
int enable1 = 3;
int enable2 = 9;


void setup()
{
  Serial.begin(19200);            // begin serial communication at 19200 bps
  nunchuck_setpowerpins();
  nunchuck_init();                // send the initilization handshake
  pinMode(motor1Pin1, OUTPUT);    // set motor pins to output 
  pinMode(motor1Pin2, OUTPUT);
  pinMode(motor2Pin1, OUTPUT);
  pinMode(motor2Pin2, OUTPUT);

  Serial.print("WiiChuckDemo ready\n");        
  // print ("WiiChuckDemo ready\n") to know that it is starting
}

void loop()
{
  if( loop_cnt > 100 ) { // every 100 msecs get new data
    loop_cnt = 0;

    nunchuck_get_data();

    joyx  = nunchuck_joyx();        // get data from wii chuck 
    joyy  = nunchuck_joyy();
    zbut = nunchuck_zbutton();
    cbut = nunchuck_cbutton(); 

    Serial.print("joyx: "); 
    Serial.print((byte)joyx,DEC);           // print variable values
    Serial.print("\tjoyy: "); 
    Serial.print((byte)joyy,DEC);
    Serial.print("\tzbut: "); 
    Serial.print((byte)zbut,DEC);
    Serial.print("\tcbut: "); 
    Serial.println((byte)cbut,DEC);
    if (zbut == 1){            // if z button is pressed 
      if (joyy >= 135) {      // if joystick is above half   
        joyy = map(joyy, 135, 229, 0, 255);      // map joy y and joy x
        int joyx2 = map(joyx, 32, 228, 1, 9);    
        switch(joyx2) {     // when variable matches case run case
        case 1:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);      // print joy x2 value
          break;                                 // break statements end one case and start the next
        case 2:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);
          break;
        case 3:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);
          analogWrite(enable1, 0);     // set anable pins to control speed
          analogWrite(enable2, joyy);  
          digitalWrite(motor1Pin1, HIGH);     // set motor pins to control direction
          digitalWrite(motor1Pin2, LOW);      // in this case turn tank full left 
          digitalWrite(motor2Pin1, LOW);
          digitalWrite(motor2Pin2, HIGH);
          break;
        case 4:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);
          analogWrite(enable1, joyy/2);
          analogWrite(enable2, joyy);
          digitalWrite(motor1Pin1, HIGH);     // in this case turn tank slight left
          digitalWrite(motor1Pin2, LOW);
          digitalWrite(motor2Pin1, LOW);
          digitalWrite(motor2Pin2, HIGH);
          break;
        case 5:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);
          analogWrite(enable1, joyy);
          analogWrite(enable2, joyy);
          digitalWrite(motor1Pin1, HIGH);    // in this case tank goes foward
          digitalWrite(motor1Pin2, LOW);
          digitalWrite(motor2Pin1, LOW);
          digitalWrite(motor2Pin2, HIGH);
          break;
        case 6:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);
          analogWrite(enable2, joyy/2);
          analogWrite(enable1, joyy);
          digitalWrite(motor1Pin1, HIGH);   // in this case turn slight right
          digitalWrite(motor1Pin2, LOW);
          digitalWrite(motor2Pin1, LOW);
          digitalWrite(motor2Pin2, HIGH);
          break;
        case 7:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);
          analogWrite(enable2, 0);
          analogWrite(enable1, joyy);
          digitalWrite(motor1Pin1, HIGH);       //in this case turn full right
          digitalWrite(motor1Pin2, LOW);
          digitalWrite(motor2Pin1, LOW);
          digitalWrite(motor2Pin2, HIGH);
          break;
        case 8:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);     
          break;
        case 9:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);
        }
      }

      if (joyy <= 125) {                                // if joy y is below half 
        joyy = map(joyy, 29, 125, 255, 0);              // map joy y and joy x variables 
        int joyx2 = map(joyx, 32, 228, 1, 9);
        Serial.print("joyx2: "); 
        Serial.println((byte)joyx2, DEC);
        switch(joyx2) {
        case 1:
          break;
        case 2:
          break;
        case 3:
          analogWrite(enable1, 0);         // in this case tank goes backwards full left
          analogWrite(enable2, joyy);
          digitalWrite(motor1Pin1, LOW);
          digitalWrite(motor1Pin2, HIGH);
          digitalWrite(motor2Pin1, LOW);
          digitalWrite(motor2Pin2, HIGH);
          break;
        case 4:
          analogWrite(enable1, joyy);    // in this case tank goes back slight left
          analogWrite(enable2, joyy/2);
          digitalWrite(motor1Pin1, LOW);
          digitalWrite(motor1Pin2, HIGH);
          digitalWrite(motor2Pin1, HIGH);
          digitalWrite(motor2Pin2, LOW);
          break;
        case 5:
          analogWrite(enable1, joyy);  // in this case tank goes straight back
          analogWrite(enable2, joyy);
          digitalWrite(motor1Pin1, LOW);
          digitalWrite(motor1Pin2, HIGH);
          digitalWrite(motor2Pin1, HIGH);
          digitalWrite(motor2Pin2, LOW);
          break;
        case 6:
          analogWrite(enable2, joyy/2);     // in this case tank goes slight backward right
          analogWrite(enable1, joyy);
          digitalWrite(motor1Pin1, LOW);
          digitalWrite(motor1Pin2, HIGH);
          digitalWrite(motor2Pin1, HIGH);
          digitalWrite(motor2Pin2, LOW);
          break;
        case 7:
          analogWrite(enable2, 0);        // in this case tank goes back full right
          analogWrite(enable1, joyy);
          digitalWrite(motor1Pin1, LOW);
          digitalWrite(motor1Pin2, HIGH);
          digitalWrite(motor2Pin1, HIGH);
          digitalWrite(motor2Pin2, LOW);
          break;
        case 8:
          break;
        case 9:
          Serial.print("joyx2: "); 
          Serial.println((byte)joyx2, DEC);
        }
      }   
    }
    if (zbut == 0) {                    // if z button is not pressed turn off the motors
      analogWrite(enable1, 0);
      analogWrite(enable2, 0);
    }
  }
  loop_cnt++;        // increase loop count by one
  delay(1);          // delay one millisecond
}


 
We had a couple problems including one that made cases 1, 8, and 9 not work, so we had to get rid of them. I don't know what made them not work, but it might have been how the Wiichuck communicated with the Arduino board. We also took out case 2 so that the tank would turn equally both directions. We also could not use a default statemet in our code because it made the tank only go forward and backwards and not be able to turn.
 
If we had more time / money we would add a full body kit and a video camera so we could operate the tank from a distance. We would also like to add more powerful motors and Kris wants hydraulics.
ċ
electronicsfinal.fz
(315k)
Unknown user,
May 13, 2011, 11:36 AM
Comments