Introduction: Reverse Engineering =Uzzors2k= Bluecar Android App. Amarino Stuff Too.

About: I like cheezy music and cheezy movies.

This instructable will mostly help people trying to drive/james bond (tomorrow never dies of course) rc cars.  If your just trying to turn on lights, or control your garage with bluetooth from an android phone, there are tons of other, better, easier, faster methods. 


Hello to whoever finds this helpful. This is also my first instructable!

A little bit of my background: I'm a bored teenaged kid that spends way too much time on Facebook and watched way too much James Bond as a kid. Right now i'm listening to a song called Stolen Dog by Burial, it's pretty sick.  ANYWAYS, I'm not in college and I don't have a professional degree in anything. I've been taking C++ classes at a community college/teaching myself C for AVR and have found Arduino to be super easy and super fun and super powerful. The bottom line: If I can do this, anybody can. 

I didn't document my project as well as I should have.

But what I did was modify a RC james bond aston martin car to be controlled by my Android phone. -I was going for a james bond kind of deal. 

I've seen a lot of development between Android and embedded systems lately, which is really cool, cause it's really cool stuff.  

The important part of this instructable is to find the best platform 
    by platform I mean android app, do I want to use amarino, which can only send sensor-accelerometer  data, or do I want to create         my own android app that incorporates buttons and accelerometer data?
But, creating your own app is pretty hard if you don't know a thing about java or android. 
But, using amarino limits you to only using accelerometer data, which is cool, but for me got lame pretty fast. 
Wait, the multicolor lamp uses sliders, I bet somebody could use buttons. I just don't know how to do it, so I might as well use somebody else's work. : )

A while back I came across a GREAT GREAT FANTASTIC website: http://uzzors2k.4hv.org/index.php?page=blucar
Some of the stuff this guy did kind of blew my mind. 
It was hard to understand his website because he compiled his avr microcontroller code with micro C, and for me, I was used to the avr gcc compiler. But I was also not as experienced with setting up a serial port with an attiny. Uzzors also created an Android app to control his attiny. 
I really wanted to either modify his app or reverse engineer it for my own purposes because it incorporates both accelerometer functionality for steering his car and buttons to control the forward and reverse functions. 
I wanted to use Uzzors app with Arduino, because its Arduino, super easy to use, super powerful at the same time.
======================================================================================
I originally wanted to use Uzzors App but I honestly couldn't understand it. I remember thinking, "Oh man, I'm in over my head man!". So I built my car which was all hardware, but I was using amarino to control all the functions. I'm sure you can find a lot about amarino, its pretty cool, but also pretty lame after a while, because you can't create buttons, you can only use sensors. But don't get me wrong, amarino is SICK when you start using it.
I'm just trying to shed some light that I thought was previously out of reach, and I think that Uzzors app seemed out of reach for folks like me!

So what am I saying? You can use Uzzors cool App with Arduino, and not only the Attiny. Check it out on the android app store, search Blucar and your set.

I know nothing about Java. Sucks.  

This is all the hardware stuff: 

Step 1: Amarino

Here's my amarino code if you wanted to take a look. Don't laugh, please just give me some constructive criticism. 
I'm using a rooted HTC inspire. 
go to http://www.amarino-toolkit.net/index.php/download.html for more info.

I was experimenting with the switch case to try and get faster response times. do what ever you want. there used to be an if statement there.

I actually modified somebody's code off the internet, so some things seem arbitrary. (I hope I didn't use that word wrong..)
Your going to have to download the Amarino library for the Arduino IDE off their website. 

#include <MeetAndroid.h>
#include <Servo.h>

MeetAndroid meetAndroid;
Servo myservo;       //controls the steering
Servo motor;            //controls the for/rev motor

void setup()
{
Serial.begin(9600);     //default baud rate for bt module
meetAndroid.registerFunction(phoneorient, 'A');   //use event A in amarino
myservo.attach(9); //servo
motor.attach(8); //forward/backwards motor
}

void loop()
{
meetAndroid.receive(); // you need to keep this in your loop() to receive events
}

void phoneorient(byte flag, byte numOfValues)
{
//Phone Orientation Controller
int values[]={0,0,0};
meetAndroid.getIntValues(values);

// You must hold phone in LANDSCAPE for following orientation.
double x=values[1];     //store x as a double (long decimal number)
int y=values[2];
int z=values[3];
x = (-x);           //my steering was reversed, quick fix
if (x>2)
{
myservo.write(105 + (x-2)*2.5);  //values determined by trial and error for my car
}
else if (x< -2)
{
myservo.write(105 + (x+2)*3.571);  //unique for my car

}
else
{
myservo.write(105);        //unique center steering position
}
/* if (y>=7)
{
//motor.write(180);
motor.write(90 + 25*(y-7));
} */

switch (y)       //tried to use a switch statement so that the motor was faster, reverse is a little glitchy I recommend using the if statement                           //anyways. If you do use the if, change y to a float/double instead of int. 
{
case 9:
motor.write(180);
break;
case 8:
motor.write(150);
break;
case 7:
motor.write(120);
break;
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
motor.write(90);
break;
case -1:
case -2:
case -3:
case -4:
case -5:
case -6:
case -7:
case -8:
case -9:
motor.write(30);
break;
}
/*else if (y<1)
{
motor.write(90 - (y-1)*);
//motor.write(30);
}
else
{
motor.write(90);
}*/

Step 2: BluCar

When I first started trying to use Uzzor's app, I tried to read the bluetooth data through my computer and display it on a terminal. It didn't work. And I didn't understand why it didn't work for some time. But what was going on was, my terminal was only set up to read ASCII characters. And I kind of understood why it wasn't working a while back, because I figured out that Uzzor's app only sent an 8 bit binary number that held all the data for left/right forward/reverse brakelights/headlights. But even with that figured out, I couldn't really check my theory because I couldn't read the data coming out of my phone, to my computer it was gibberish and the code that I was writing to read the data wasn't working. And a while back, I wasn't as comfortable as I am now with using bit masks. 

I also thought that the arduino by default, using the Serial.read() only read ASCII characters. (I was wrong of course)

But this is why Uzzor is cool: Amarino uses a 3 character array to send accelerometer data, that's 3 bytes. Those three bytes only correspond to one variable. One byte for forward, the second for steering, the third for whatever you want.
Uzzor sends one byte, which contains all the instructions for forward, reverse, left, right, headlights, brakelights. pretty cool.

This is the arrangement that I found:

| 7: Headlights | 6: BrakeLights | 5: Reverse | 4: Forward | 3:Right | 2: Left | 1: Unused | 0: Unused |

You store the Serial.read() as an int and create bit masks in order to read the bits that you need to control. 

Here's an example: 
I get this binary number from Serial.read()
10010100     //= 148
The headlights are on, the car is going forward, and turning left. 
If I want to read the status of forward/reverse:
create a bit mask: 00110000 & 10010100  
which results in 00010000
And I can write code to understand that number and perform a task. 
I might be doing a wrong method or something, but the way I did it worked for me, if you have any other methods, post them!
==============================================================
Kind of off topic
but the way Uzzor uses his serial data is pretty fricken classy. Like when I finally understood what was going on I was like woah!, that is pretty smooth!

All he does is straight up write his 8 bit number to DDRB (ATTINY, which has 8 ports, so each port corresponds directly to each function, the headlight LED is connected to the seventh (eighth pin)  most significant port). So when he writes a high in the brakelight position, it corresponds directly to the port which connects to the brakelight led.
8bit serial number --> DDRB --> hardware

He also creates a bit mask to ignore the last four bits (0 and 1 are unused 2 and 3 are steering) and he does this because steering is controlled by a servo and made another function to convert the data into a servo PPM signal.

Cool huh? Me thinks so.
--------------------------------------------------------------------------------------------------------------------------------
When I first started trying to test/read the serial data from Uzzor's android app, i was using the equity operator, which wasn't working. But i guess that is bad programming... I haven't refined my code, but what I'm doing is looking for ball park ranges. I clear the bits around the data I want to read. If I wanted to read the motor state, I read the forward and reverse states
0001 0000       //serial real time data
0011 0000 &   //mask so that I only read the forward/reverse data
--------------------
0001 0000       //forward

0010 0000       //same but for reverse
0011 0000 &
-------------------
0010 0000

So I can either get values 32 or 16. (0010 0000 or 0001 0000)
and I & with the number 48 because 48 in binary is 0011 0000.
So the way I wrote my code was 
int x;
x = Serial.read();              //Being safe could try int x = Serial.read(); 
//reverse:
x &= 48;         //x = Serial & 0011 0000
if (x > 24)  servo.write(30);                //ball park range the target reverse command which should be 32
else if (x < 24 && x > 15)   servo.write(180);                       //ball park range the target forward command which should be 16
else servo.write(90);
--------------------------------------------------------------------------------------------------------------
So that's it, here's some code to use Uzzor's app with arduino:

#include <Servo.h>
Servo servo;
Servo motor;
void setup()
{
  servo.attach(9);
  motor.attach(8);
  Serial.begin(9600);
  pinMode(13, OUTPUT); //This is my "headlight" 
}

void loop()
{ //start main
int x;
int y;
int z;

if (Serial.available() > 0)
{ //start if 1
x = Serial.read(); //x has to be declared a Serial.read() here for some reason
y = x;
z = x;
y &= 48; //bit masks y looks for forward/revers
x &= 12; //looks for left/right
z &= 128; //looks for headlight no brake light yet
if (x<6 && x>3) 
{
  servo.write(80); //left unique to my project
  }
else if (x>6)
{
servo.write(120); //right unique to my project
}
else servo.write(105); //Center unique to my project

if (y > 24) 
{
  motor.write(30); //reverse
 }
 else if (y<24 && y>15)
 {
   motor.write(150); //forward
  }
  else motor.write(90); //don't move

if (z > 0) digitalWrite(13, HIGH); //headlights
else digitalWrite(13, LOW);
} //end if 1
} //end main

------------------------------------------------------------------------------------------------------
let me know if you have any advice or improvements.


Step 3: Conclusion

The ideal Android app would have buttons, and accelerometer data. Amarino does a great job at providing relatively glitch free accelerometer data but lacks the buttons. BluCar is kind of glitchy and the accelerometer functionality in the app for steering only goes extreme right or extreme left, so you can't get any of the in-between steering positions which is lame too. But, it still can center. Idk, Amarino and BluCar need to have kids or something. In the future I might figure out Uzzor's program and use the first 4 bits in the serial data for a smoother steering system. I'd have 16 positions, 8 for each side. The accelerometer can have a max 9 integer. increment 8/9.

The main point of this instructable is to provide another Android interface for Arduino, one that is a little more tailored to driving stuff instead of just on/off switch functions.

Here's a video of both programs in action:
http://www.youtube.com/watch?v=Wsil_w2pt40&feature=player_embedded

Make It Move Challenge

Participated in the
Make It Move Challenge