Android control car

hi , i done a basic android control programme, i use ultrasonic sensor for stop the car to be tiggered if the distance is below 50cm. the android control working well but the problem at the car not stop at the trigger distance. pls help where the mistake

char t;
const int R1PWM = 9, L1PWM = 10, R2PWM = 7, L2PWM = 8, PWM = 11;
const int trigPin = 4, echoPin = 3;

float duration, distance = 300.0;

void setup() {
  pinMode(R1PWM, OUTPUT);  //right motors forward
  pinMode(L1PWM, OUTPUT);  //right motors reverse
  pinMode(R2PWM, OUTPUT);  //left motors forward
  pinMode(L2PWM, OUTPUT);  //left motors reverse
  pinMode (PWM, OUTPUT);   //SPEED CONTROL
  Serial.begin(9600);

}

void loop() {
  if (Serial.available()) {
    t = Serial.read();
    Serial.println(t);

    sense();   // ultrasonic detection

    if (distance < 50) // if ultrasonic detection the 50 cm automtically all motors stops

    {
      stops();
    }
    else {

      if (t == 'F') {          //move forward(all motors rotate in forward direction)
        digitalWrite(R1PWM, HIGH);
        digitalWrite(L1PWM, LOW);
        digitalWrite(R2PWM, LOW);
        digitalWrite(L2PWM, HIGH);
        analogWrite (PWM, 50);
        Serial.println ("MOTOR RUNS FOERWARD");
      }

      else if (t == 'G') {    //move reverse (all motors rotate in reverse direction)
        digitalWrite(R1PWM, LOW);
        digitalWrite(L1PWM, HIGH);
        digitalWrite(R2PWM, HIGH);
        digitalWrite(L2PWM, LOW);
        analogWrite (PWM, 50);
        Serial.println ("MOTOR RUNS REVERSE");
      }

      else if (t == 'L') {    //turn right (left side motors rotate in forward direction, right side motors doesn't rotate)
        digitalWrite(R1PWM, LOW);
        digitalWrite(L1PWM, HIGH);
        digitalWrite(R2PWM, LOW);
        digitalWrite(L2PWM, HIGH);
        analogWrite (PWM, 50);
        Serial.println ("MOTOR RUNS RIGHT");
      }

      else if (t == 'R') {    //turn left (right side motors rotate in forward direction, left side motors doesn't rotate)
        digitalWrite(R1PWM, HIGH);
        digitalWrite(L1PWM, LOW);
        digitalWrite(R2PWM, HIGH);
        digitalWrite(L2PWM, LOW);
        analogWrite (PWM, 50);
        Serial.println ("MOTOR RUNS LEFT");
      }


      else if (t == 'S') {    //STOP (all motors stop)
        digitalWrite(R1PWM, LOW);
        digitalWrite(L1PWM, LOW);
        digitalWrite(R2PWM, LOW);
        digitalWrite(L2PWM, LOW);
        Serial.println ("STOP");
      }
      delay(100);
    }
  }
}

void sense() // ultrasonic
{
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration * .0343) / 2;
  Serial.print("Distance: ");
  Serial.println(distance);
  delay(100);
}

void stops() // STOP motors
{
  digitalWrite(R1PWM, LOW);
  digitalWrite(L1PWM, LOW);
  digitalWrite(R2PWM, LOW);
  digitalWrite(L2PWM, LOW);
  Serial.println ("STOP OF OBS");
}

Do you see the "STOP OF OBS" message when the distance printed is less than 50 ?

1 Like

What do your Serial.prints show, particularly for distance?

It might be a good idea to set pinMode(trigPin, OUTPUT).

Steve

1 Like

You're only checking the distance if you've received something.
Shouldn't you be checking all the time?

Why is the sense function void?
Shouldn't it return the range?

1 Like

Your code is entirely inside of the if(Serial.available()) conditional and distance will not be read unless there is something in the serial input buffer.

Add one bracket to isolate the Serial read from the rest of the program, and remove on bracket from the end which will now be extral.

if (Serial.available()) {
    t = Serial.read();
    Serial.println(t);
  } //add bracket here but  there will be one extra at the end after you do this
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.