Project showcase
Smartphone Controlled Arduino Car with Auto-Mode

Smartphone Controlled Arduino Car with Auto-Mode

In this project I've built a smartphone controlled car where the car either can be steered by user or moves automatically.

  • 7 views
  • 0 comments
  • 0 respects

Components and supplies

Necessary tools and machines

09507 01
Soldering iron (generic)
Hy gluegun
Hot glue gun (generic)
One opportunity to fix the components.

Apps and online services

About this project

I, as a student in the fields of engineering, wanted to learn the interplay of sensors and actuators controlled by a code. Here several arduino projects give the perfect setup of learning by doing closely on either hardware and software matters.

I've decided to build a car controlled by a smartphone app via bluetooth. The ideas for different approaches were found on project hubs like this website. Here it was important for me to not simply copy a project and the according code. Using the information on project hubs I've decided which optional functions I want to include in my car. The app (ArduinoBlue) I used was already made and is available on IOS or Android.

My car can be turned on with the switch and has two different modes:

  • Steering mode: Switching on the system the car is always in steering mode. Here the user can steer the car via stick on the "ArduinoBlue" app. By the way you can adjust the moving and turning speed with sliders (see picture: "ArduinoBlue"). Additionally the ultrasonic is meassuring if there are obstacles in the direction straight on (the servo engine is turned off in this mode). If you are about the crash at a wall the car will turn to one side. But you have to be careful with driving too fast.
  • Auto mode: This second mode gets turned on and off by the "Auto On"/ "Auto Off" Buttons. In this mode the car drives on its own using a turning ultrasonic sensor to avoid obstacles.

Obviously there are still things to optimise but I think such a project is never completely finished.

Code

CodeArduino
// Libraries
#include <ArduinoBlue.h>
#include <Servo.h>

// Ultrasonic Sensor
#define TRIGGER 23
#define ECHO 25
int distance;

// Bluetooth data
int prevThrottle = 49;
int prevSteering = 49;
int throttle, steering, sliderVal, sliderId, button;
int automotive = 3; // starting in steering mode

ArduinoBlue phone(Serial1); // pass reference of bluetooth object to ArduinoBlue constructor.

// Engines
int ENA = 3;
int IN1 = 2;
int IN2 = 4;
int IN3 = 7; 
int IN4 = 8; 
int ENB = 9;

// Velocities
int acc = 80 + 175/2;
int spin = 115;
int turn;

// Servo
Servo servo;
int angle = 35;

void setup() {    
  Serial.begin(9600);
  Serial1.begin(9600);
  // delay just in case bluetooth module needs time to "get ready".
  delay(100);
  Serial.println("setup complete");
  pinMode(ENA, OUTPUT);
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(ENB, OUTPUT);
  
  // Ultrasonic
  pinMode(ECHO, INPUT);
  pinMode(TRIGGER, OUTPUT);
  digitalWrite(TRIGGER, LOW);

  // Servo
  servo.attach(27);
  servo.write(angle);
  
}


void loop() {
  
  // get bluetooth data
  throttle = phone.getThrottle();
  steering = phone.getSteering();
  button = phone.getButton(); 
  // ID of the slider moved. In this timegap sliderVal accessible.
  sliderId = phone.getSliderId();
  // Slider value goes from 0 to 200.
  sliderVal = phone.getSliderVal();

  // changing the velocity depending on throttle slider position
  if (sliderId == 1) {
    Serial.print("Slider ID: ");
    Serial.print(sliderId);
    Serial.print("\tValue: ");
    Serial.println(sliderVal);
    acc = map(sliderVal, 0, 200, 80, 255);
  }
  // changing between steering and auto mode
  if (button != -1) {
    automotive = button;
  }
  // changing turn speed depending on steering slider position
  if (sliderId == 0) {
    Serial.print("Slider ID: ");
    Serial.print(sliderId);
    Serial.print("\tValue: ");
    Serial.println(sliderVal);
    spin = map(sliderVal, 0, 200, 80, 150);
  }
  
  // Display throttle and steering data if steering or throttle value is changed (only if usb is attached)
  if (prevThrottle != throttle || prevSteering != steering) {
    Serial.print("Throttle: "); Serial.print(throttle); Serial.print("\tSteering: "); Serial.println(steering);
    prevThrottle = throttle;
    prevSteering = steering;
  }

  if (automotive == 3) {
    drivemode(); // steering mode
  }
  if (automotive == 2) {
    selfdrivemode();  // auto mode
  }

}

void findDistance() {
  int duration;
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIGGER, LOW);

  duration = pulseIn(ECHO,HIGH);
  distance = duration / 2 / 7.6;
  delay(60);

}


void selfdrivemode() {  
  for (angle=35; angle <= 70; angle += 10){
    servo.write(angle);
    findDistance();
    Serial.println(distance); //value 10 equals 1cm obstacle distance
    if (distance <= 70) {
      analogWrite(ENA, 100);
      analogWrite(ENB, 100);
      right();
      delay(800);
      stopengine();
    }
    delay(15);
  }
  
  servo.write(35);
  
  for (angle=35; angle >= 0; angle -= 10){
    servo.write(angle);
    findDistance();
    Serial.println(distance); //value 10 equals 1cm obstacle distance
    if (distance <= 70) {
      analogWrite(ENA, 100);
      analogWrite(ENB, 100);
      left();
      delay(800);
      stopengine();
    }
    delay(15);
  }
  
  servo.write(35);
  
  findDistance();
  Serial.println(distance); //value 10 equals 1cm obstacle distance
  if (distance > 70) {
    analogWrite(ENA, 100);
    analogWrite(ENB, 100);
    forward();
    delay(400);
    stopengine();
  }
}

void drivemode() {
  findDistance();
  Serial.println(distance); //value 10 equals 1cm obstacle distance
  if (distance >= 100) {
    drivesetup();
  }
  else {
    analogWrite(ENA, 90);
    analogWrite(ENB, 90);
    left();
    delay(1000);
  }          
        
}

void drivesetup() {  
  if (throttle > 60) {
    if (steering > 40 && steering < 60) {
      analogWrite(ENA, acc);
      analogWrite(ENB, acc);
      forward();
    }
    if (steering <= 40 && steering >= 10) {
      turn = map(steering, 10, 40, 0, acc);
      analogWrite(ENA, turn); //decrease
      analogWrite(ENB, acc); //hold
      forward();
    }
    if (steering >= 60 && steering <= 90) {
      turn = map(steering, 60, 90, acc, 0);      
      analogWrite(ENA, acc); //hold
      analogWrite(ENB, turn); //decrease
      forward();
    }
    if (steering < 10) {
      analogWrite(ENA, spin);
      analogWrite(ENB, spin);
      left(); 
    }
    if (steering > 90) {
      analogWrite(ENA, spin);
      analogWrite(ENB, spin);
      right(); 
    }
  }    
  if (throttle < 40) {
    if (steering > 40 && steering < 60) {
      analogWrite(ENA, acc);
      analogWrite(ENB, acc);
      back();
    }
    if (steering <= 40 && steering >= 25) {
      turn = map(steering, 25, 40, 0, acc);
      analogWrite(ENA, turn); //decrease
      analogWrite(ENB, acc); //hold
      back();
    }
    if (steering >= 60 && steering <= 75) {
      turn = map(steering, 60, 75, acc, 0);
      analogWrite(ENA, acc); //hold
      analogWrite(ENB, turn); //decrease
      back();
    }
    if (steering < 10) {
      analogWrite(ENA, spin);
      analogWrite(ENB, spin);
      left(); 
    }
    if (steering > 90) {
      analogWrite(ENA, spin);
      analogWrite(ENB, spin);
      right(); 
    }
  }
   
  if (throttle == 49) {
    stopengine();
  }
}

void forward() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void back() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void left() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, HIGH);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
}

void right() {
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, HIGH);
}

void stopengine() {
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
}

Schematics

Wire circuit
Capture trldi5dcg1

Comments

Similar projects you might like

Smartphone Controlled Arduino 4WD Robot Car

Project in progress by Andriy Baranov

  • 109,514 views
  • 62 comments
  • 199 respects

Bluetooth Controlled Car with Arduino Mega

Project showcase by DANNY003

  • 6,216 views
  • 10 comments
  • 17 respects

Bluetooth Controlled Car

Project tutorial by Aniket Mindhe

  • 4,947 views
  • 2 comments
  • 18 respects

SmartPhone Controlled RGB MOOD Light

Project tutorial by Prajjwal Nag

  • 6,708 views
  • 1 comment
  • 11 respects

Arduino Controlled Car with HC-SR04

Project showcase by VASILEIOS MOUGIOS

  • 14,352 views
  • 3 comments
  • 17 respects

Bluetooth Controlled Car

Project tutorial by JANAK13

  • 36,390 views
  • 31 comments
  • 59 respects
Add projectSign up / Login