Arduino, Android and Lego

Using Arduino (and an Android app)
to control Lego motors

aka: androdino

Who is this?

Cause I had to

  • Web Developer since before there was Google
  • Barely got an Electrical Engineering degree
  • Google I/O is sorta my thing (love conferences!)

The project!

Make a remote control Lego car. Add motors, gears, etc, to a this Lego set and connect it to an Arudino card outfitted with bluetooth and control it with an app on an Android Phone.

Why Lego?

  • Lego Technic makes it easy to prototype ideas
  • Lego Motors are already geared down and powerful
  • Lego Gears all work with each other
  • Wanted to use this cool steering idea from Sariel
  • Lego is fun! Age isn't a factor!
  • A reason to buy more Lego

Why Arduino?

  • Wanted a reason to explore microcontrollers
  • Committed to give a presentation about Arduino and Android
  • Arduino is inexpensive compared to a Mindstorms set
  • Mindstorms "brain" is very limited (wireless and programming options)

Why Android?

  • Lego remotes don't give control over speed (mostly . . . just ignore that train remote)
  • Lego remotes are IR (light based) so you have to point the remote at the car
  • Lego remotes perform poorly outside (the Sun's IR interfers)
  • Did I mention that commitment about giving a talk about Google stuff?

What is Arduino?

Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.

Demo "punch it"

More on Arduino

Official Arduino

ATmega328, 16Mhz

Arduino MEGA

ATmega2560, 16Mhz (more pins, basically)


Arduino MEGA ADK

Arduino Due

ARM Cortex M3, 32-bit


ADK 2011

Basically, it's an Arduino MEGA ADK (ATmega2560, 16Mhz)


ADK 2012

Basically, it's an Arduino Due (ARM Cortex M3, 32-bit)

With a TON of sensors and LEDs

The Clones!

Plus dozens of others (ruggedunio and iteaduino)!

Shields n' Stuff!

Here are a few I have here


Wanna see them? (maybe after the demo, which is next)

Arduino Hardware details

  • Arduino Mega (or Uno)
  • iTead "Motomama" Shield with BTBee module


Lego details

Notice

  • my custom Lego PF to Arduino wires
  • cool Lego seering mechanism
  • TWO 9V battery compartments (one only delivers 3V)

Hardware Software Details

Hardware Software Details

#include string.h

#define right1 5		//define I1 interface
#define speedPinRight 6  	//enable right motor (bridge A)
#define right2 7      		//define I2 interface 

#define left1 8      		//define I3 interface 
#define speedPinLeft 9  	//enable motor B
#define left2 10     		//define I4 interface 

//Motor Pins
int dir1B = right1;
int speedB = speedPinRight;
int dir2B = right2;

int dir1A = left1;
int speedA = speedPinLeft; 
int dir2A = left2;


//Bluetooth Pins
const int rxPin = 0; // pin used to receive, though, on the itead card, PIN 0 is TX
const int txPin = 1; // pin used to send to, though, on the itead card, PIN 1 is RX

//Other Bluetooth related globals
int availBytes;
const int stringSize = 64;
char stringarr[stringSize];

//Used for the ledPin test
const int ledPin = 2;
char sval; 
char bval;

//Other Globals . . . love GLOBALS
int loopDelay = 50;

const float speedFactor = 28.3;
String CTRLStr = '\0';
int frBool = 0;
int speedVal = 0;
int lrBool = 0;
int dirVal = 0;
int headBool = 0;
int brakeLeftBool = 0;
int brakeRightBool = 0;

//This is a valid string
//API: s:foward/reverse:speed:left/right:direction:headlights:brakeleft:brakeright
//ex   s:1:5:1:5:0:0:0

int apiStringSize = 15;
char apiStartChar = 's';

//needed for splitString
const int MAX_ARGS = 9;
char byteRead;

void setup()
{
  //Serial is used for bluetooth
  Serial.begin(9600);

  //Testing Pin
  pinMode(ledPin, OUTPUT);
  
  //Motor Pins  
  pinMode (dir1A, OUTPUT);
  pinMode (dir2A, OUTPUT);
  pinMode (dir1B, OUTPUT);
  pinMode (dir2B, OUTPUT);
  pinMode (speedA, OUTPUT);
  pinMode (speedB, OUTPUT);
  
}

void loop()
{
  //Blank out the string array that will hold the data from Bluetooth
  for(int i=0; i= apiStringSize) { 
    //Every Serial.print/println/write is just a debug message sent to Bluetooth device
    Serial.print("Bluetooth Serial Available: ");
    Serial.println(availBytes);
    
    //Read in everything . . . we only need the first apiStringSize but we want to 
    //clear the buffer of everything else, one char at a time
    for(int i=0; i=parserCnt){
    rFromIndex = rToIndex+1;
    rToIndex = s.indexOf(parser,rFromIndex);

    if(index == parserCnt){
      if(rToIndex == 0 || rToIndex == -1){
        return '\0';
      }
      return s.substring(rFromIndex,rToIndex);
    }
    else{
      parserCnt++;
    }

  }
  return rs;
}

Had to show something at a CODE camp.

Android Details

Psy

Android Details

Android Details

Challenges

  • RX to TX and TX to RX??
  • Can't program when the bluetooth is connected and using UART pins
  • Software Serial on Mega/ADK
  • SoftwareSerial library (or any other normal library) not in ADK 2012 custom IDE
  • Lego batter pack is 9V. L298d chip on motor shield is inefficient

Demo Shieldbot

Hardware

  • Arduino Uno
  • Shieldbot (motor shield with motors attached!)
  • Bluetooth Shield

Software

  • Tank Mode button

What's next?

What else?

Use that extra usb port!

Put a phone in as the windshield

LIGHTS!

Other options?

i.e. Why didn't you use?

Why not Javascript?

i.e. Why didn't you use?

Resources

Books!!

Honorable mention to electronics and lego stackexchanges

Want to laugh? Check out my panic post from this morning.

Where did you get all that stuff?

Vendors

Amazon
Maker Shed
Sparkfun
Adafruit
itead
seeed
Lego Technic and Power Functions

How much was it?

Where is the code??

Github!

androdino

Insert the french word for "end" here

@tooshel

Questions?