I've recently started with building a car which I control remotely with an App I created on the MIT App Inventor website. If I were to use it just to control it remotely I wouldn't have any kind of problem, for I tried it like that and it worked perfectly.
This is how I programmed my app and how it looks on the phone:
Adelante= Forward
Atras= Backwards
Deretcha=Right
Izquierda= Left
However, I tried to make some changes on it and sort of implement "emergency measures". Basically, I added a front sensor to the robot and gave it new orders:
-
If the Robot felt an obstacle from 40 to 20 centimeters away it just would be able to back up through the controls, that way he wouldn't be able to crash into something.
-
If the Robot felt an obstacle less than 20 centimeters away it would start backing up automatically; which means that if something like this happened it would mean that the unknown object was advancing towards the Robot.
-
If the Robot didn't sense anything on more than 40 cm away, then the person controlling the robot would be able to use the controls.
And this is how I did it:
int state=0;
const int trigPin1 = 10;
const int echoPin1 = 7;
// defines variables
long duration;
int distance;
int angle = 0;
void setup()
{
//Setup Channels
pinMode(12, OUTPUT); //Motor Channel A
pinMode(9, OUTPUT); //Brake Channel A
pinMode(13, OUTPUT); //Motor Channel B
pinMode(8, OUTPUT); //Brake Channel B
pinMode(trigPin1, OUTPUT);
pinMode(echoPin1, INPUT);
digitalWrite(12, HIGH);
digitalWrite(13, LOW); // Connection is inverted.
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
void loop()
{
// Clears the trigPin1
digitalWrite(trigPin1, LOW);
delayMicroseconds(200);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin1, HIGH);
delayMicroseconds(100);
digitalWrite(trigPin1, LOW);
// Reads the echoPin1, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin1, HIGH);
// Calculating the distance
distance= duration*0.034/2;
Serial.println(distance);
//if some data is sent, reads it and saves in state
if(Serial.available() > 0)
{
state = Serial.read();
}
if (distance > 40)
{
switch (state)
{
case 'F':
delayMicroseconds(100);
digitalWrite(12, HIGH);
digitalWrite(9, LOW); //Brake is off
digitalWrite(8, LOW); // Brake is off
digitalWrite(13, LOW);
analogWrite(3, 255); //Spins the motor on Channel A at full speed
analogWrite(11, 255); //Spins the motor on Channel B at full speed
break;
case 'B':
delayMicroseconds(100);
digitalWrite(12, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(13, HIGH);
analogWrite(3, 255); //Spins the motor on Channel A at full speed
analogWrite(11, 255); //Spins the motor on Channel B at full speed
break;
case 'R':
delayMicroseconds(100);
digitalWrite(12, HIGH);
digitalWrite(9, LOW); //A
digitalWrite(8, LOW);//B
digitalWrite(13, LOW);//B
analogWrite(11, 90); //Spins the motor on Channel B at full speed
analogWrite(3, 255); //Spins the motor on Channel A at low speed
break;
case 'L':
delayMicroseconds(100);
digitalWrite(12, HIGH); //A
digitalWrite(9, LOW); //A
digitalWrite(8, LOW);//B
digitalWrite(13, LOW);//B
analogWrite(3, 90); //Spins the motor on Channel A at low speed
analogWrite(11, 255); //Spins the motor on Channel B at full speed
break;
case '3':
delayMicroseconds(100);
digitalWrite(12, HIGH); //A
digitalWrite(9, LOW); //A
digitalWrite(8, LOW);//B
digitalWrite(13, HIGH);//B
analogWrite(3, 255); //Spins the motor on Channel A at full speed
analogWrite(11, 255); //Spins the motor on Channel B at full speed
break;
case '2':
delayMicroseconds(100);
digitalWrite(12, LOW); //A
digitalWrite(9, LOW); //A
digitalWrite(8, LOW);//B
digitalWrite(13, LOW);//B
analogWrite(3, 255); //Spins the motor on Channel A at full speed
analogWrite(11, 255); //Spins the motor on Channel B at full speed
break;
case 'S':
delayMicroseconds(100);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
break;
case '0':
delayMicroseconds(100);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
break;
}
}
if (distance < 40 && distance > 20) {
switch (state) {
case 'B':
delayMicroseconds(100);
digitalWrite(12, LOW);
digitalWrite(9, LOW);
digitalWrite(8, LOW);
digitalWrite(13, HIGH);
analogWrite(3, 255); //Spins the motor on Channel A at full speed
analogWrite(11, 255); //Spins the motor on Channel B at full speed
break;
default:
delayMicroseconds(100);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
break;
}
}
if (distance < 20) {
delayMicroseconds(100);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
}
}
When I implement the last code I have a problem, at first if there aren't any objects nearby then the robot works.
Then, when it finds an object ranging from 40 to 20 cm it just stops, which is supposed to do, and if I try to make it back up with the phone it responds and does as I told it to. However, I can just use that action once because when I stop pressing the button everything stops working, I cannot make it back up or go forward, nor do anything else. The app also seems to stop working.
Moreover, if I decide to make another object advance towards it so it backs up automatically it seems to follow code, but then doesn't stop even when it is too further away from the object.
I tried to see if maybe it was the distance sensor was the one that wasn't working, and I plugged the robot into the computer to see if it correctly read the distance. And it did, I even decided to change it and try it with a newer one I had, just in case. I've also tried changing the code but it doesn't seem to work no matter what I do. Any ideas would be helpful.
Thanks