I wrote a code in App inventor 2 for controling this car with bluetooth, but I did not find the way to do it with the android acceleretor.
This is my arduino code:
#include <SoftwareSerial.h>
#include <AFMotor.h>
//creates two objects to control the terminal 1 and 2 of motor shield
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
SoftwareSerial BT(0, 1); //TX, RX respetively
String readdata;
int delayVal = 250;
void setup() {
BT.begin(9600);
Serial.begin(9600);
}
//-----------------------------------------------------------------------//
void loop() {
while (BT.available()){ //Check if there is an available byte to read
delay(10); //Delay added to make thing stable
char c = BT.read(); //Conduct a serial read
readdata += c; //build the string- "forward", "reverse", "left" and "right"
}
if (readdata.length() > 0) {
Serial.println(readdata);
if(readdata == "forward")
{
motor1.setSpeed(255); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
}
else if(readdata == "reverse")
{
motor1.setSpeed(255);
motor1.run(BACKWARD); //rotate the motor counterclockwise
motor2.setSpeed(255);
motor2.run(BACKWARD); //rotate the motor counterclockwise
}
else if (readdata == "right")
{
motor1.setSpeed(255); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(0);
motor2.run(RELEASE); //turn motor2 off
}
else if ( readdata == "left")
{
motor1.setSpeed(0);
motor1.run(RELEASE); //turn motor1 off
motor2.setSpeed(255); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
}
else if (readdata == "stop")
{
motor1.setSpeed(0);
motor1.run(RELEASE); //turn motor1 off
motor2.setSpeed(0);
motor2.run(RELEASE); //turn motor2 off
}
readdata="";}} //Reset the variable
Tnx
Raanan