I spent a good amount of time trying to make a remote controlled RC car using a phone’s gyro as a Remote here is all the code I also attached a zipped file at the bottom.

I used MIT’s App inventor for making the Android portion of the project. Any Questions leave them in the comments and ill try to help as best I can. Have Fun.

 Arduino Code

#include <SoftwareSerial.h>
#include <Servo.h>

SoftwareSerial mySerial(10, 11); // RX, TX

Servo xservo;
Servo yservo;
String inData;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
xservo.attach(9); // attaches the servo on pin 9 to the servo object
yservo.attach(6); // attaches the servo on pin 6 to the servo object
}
void loop() // run over and over
{
while (mySerial.available() > 0)
{
char recieved = mySerial.read();
inData += recieved;
// Process message when new line character is recieved
if (recieved == ‘\n’)
{
//Serial.print(“Arduino Received: “);
//Serial.print(inData);
String xval = getValue(inData, ‘:’, 0);
String yval = getValue(inData, ‘:’, 1);
inData = “”; // Clear recieved buffer
Serial.print(“Y:” + yval);
Serial.println(“X:” + xval);

//Convert String to int
int xvalue = stringToNumber(xval);
int yvalue = stringToNumber(yval);
//map the values to the servo max output 0-180 90 is full stop.
//Servo ouptuts
xservo.write(map(xvalue, -50, 50, 0, 180));
yservo.write(map(yvalue, -50, 50, 0, 180));
delay(20);
}
}
}
String getValue(String data, char separator, int index)
{
int found = 0;
int strIndex[] = {
0, -1 };
int maxIndex = data.length()-1;
for(int i=0; i<=maxIndex && found<=index; i++){
if(data.charAt(i)==separator || i==maxIndex){
found++;
strIndex[0] = strIndex[1]+1;
strIndex[1] = (i == maxIndex) ? i+1 : i;
}
}
return found>index ? data.substring(strIndex[0], strIndex[1]) : “”;
}
int stringToNumber(String thisString) {
int i, value, length;
length = thisString.length();
char character[(length+1)];
for(i=0; i<length; i++) {
character[i] = thisString.charAt(i);
}
character[i]=0;
value = atoi(character);
return value;
}

MIT App project file and source code for Arduino RC Android

Leave a comment