Bluetooth controllable car

Hi everyone, so for my schoolproject I am making an "rc-car" which you can control on your smartphone with an app I've made. The only problem I'm having is that i can only drive in small shocks. This is because my delay is set to '10'. But does anyone have a better approach on how to control the vehicle so it keeps driving. I could also add a stop button, but i'd rather do it in another way

#include <SoftwareSerial.h>
char t = '\0';

int trigPin = 6;  // TRIG pin
int echoPin = 7;  // ECHO pin
int input1 = 9;
int input2 = 8;
int input3 = 2;
int input4 = 3;

const byte rxPin = 5;
const byte txPin = 4;
SoftwareSerial mySerial(rxPin, txPin);

float duration_us, distance_cm;
bool distance = false;

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(2, OUTPUT);   //left motors forward
  pinMode(3, OUTPUT);   //left motors reverse
  pinMode(8, OUTPUT);   //right motors forward
  pinMode(9, OUTPUT);   //right motors reverse
  pinMode(10, OUTPUT);
  pinMode(11, OUTPUT); //Led
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  mySerial.begin(9600);
  Serial.begin(9600);

  pinMode(trigPin, OUTPUT);  // configure the trigger pin to output mode
  pinMode(echoPin, INPUT);   // configure the echo pin to input mode
}

void loop() {
  // If any data is available at the Bluetooth Serial Port
  if (mySerial.available()) {
    t = mySerial.read();
    Serial.write(t);
  }

  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration_us = pulseIn(echoPin, HIGH);  // measure duration of pulse from ECHO pin

  distance_cm = 0.017218 * duration_us;  // calculate the distance

  if (distance_cm >= 10)
    distance = true;
  else
    distance = false;

  if (t == 'F') {  //move forward
      do{
     Serial.println("Fout");
      digitalWrite(input3, HIGH);
      digitalWrite(input4, LOW);
      }while(t == 'F');
  }
  else if (t == 'B') {  //move reverse
    do{
    digitalWrite(input3, LOW);
    digitalWrite(input4, HIGH);
    t = mySerial.read();
    }while(t == 'B');
  }
  else if (t == 'R') {  //turn right (left side motors rotate in forward direction, right side motors doesn't rotate)
    digitalWrite(input1, HIGH);
    digitalWrite(input2, LOW);
  }
  else if (t == 'L') {  //turn left (right side motors rotate in forward direction, left side motors doesn't rotate)
    digitalWrite(input1, LOW);
    digitalWrite(input2, HIGH);
  }
  else if (t == '1') {  //turn led on or off)
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    digitalWrite(12, HIGH);
    digitalWrite(13, HIGH);
  }
  else if (t == '0') {
    digitalWrite(10, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, LOW);
    digitalWrite(13, LOW);
  }
  else if (t == 'S' || distance == true) { //STOP (all motors stop)
    digitalWrite(input1, LOW);
    digitalWrite(input2, LOW);
    digitalWrite(input3, LOW);
    digitalWrite(input4, LOW);
  }
  delay(10);
}

Yes, lose the delay..
maybe replace it with t = '\0'; to clear out the last command..
take the do whiles out of forward and reverse and lose the mySerial.read in reverse..
then retest, put it up on blocks so it don't fly away.. :slight_smile:

~q

1 Like

I have done this, but I do have to use a stop button now to turn off all motors.

When I did my remote..
OnMouseDown, I sent the drive signal and OnMouseUp, I sent a stop signal..
Means you have to press and hold to drive and to stop just lift you finger..
Not sure what your control app is created with but maybe this helps..

good luck.. ~q

Hey, in the code you have used Sofwareserial.h, to define virtual serial pins. You could use the hardware serial pins for the Bluetooth module right?
P.S : I am new to arduino code, and I just asking this for a little clarity.

Yes you can, but there are two consequences you need to be aware of.

  1. You will need to disconnect the BT module from the Hardware serial pins when downloading code.

  2. Serial input to the sketch can only come from the phone over BT and not from the serial monitor. Serial output from the sketch will go the the phone over BT and also to the monitor. Sometimes for debugging and development you want to have input from the monitor, particularly if you have a custom app on the phone and it is not working correctly.

If you have additional questions or issues it is best to start your own new thread and not tag on to a completed posting which is not specific to your situation.

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