Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Suyog Gunjal
Published © GPL3+

Voice Command - Arduino Smart Car

An Arduino smart car with voice command.

IntermediateWork in progressOver 1 day935
Voice Command - Arduino Smart Car

Things used in this project

Schematics

Grove Speech Recognizer

Arduino UNO 3

Circuit

Code

arduino_smart_car.ino

C/C++
#include "./IRremote.h"
#include <Servo.h>
#include <SoftwareSerial.h>
#include "SPI.h"
#include "Phpoc.h"

#define SOFTSERIAL_RX_PIN  0
#define SOFTSERIAL_TX_PIN  1


// Arduino web server 
PhpocServer server(80);

char slideName;
int slideValue;

SoftwareSerial softSerial(SOFTSERIAL_RX_PIN,SOFTSERIAL_TX_PIN);

const char *voiceBuffer[] =
{
    "Turn on the light",
    "Turn off the light",
    "Play music",
    "Pause",
    "Next",
    "Previous",
    "Up",
    "Down",
    "Turn on the TV",
    "Turn off the TV",
    "Increase temperature",
    "Decrease temperature",
    "What's the time",
    "Open the door",
    "Close the door",
    "Left",
    "Right",
    "Stop",
    "Start",
    "Mode 1",
    "Mode 2",
    "Go",
}
;

//==============================
//==============================
int Left_motor_back=9;       
int Left_motor_go=8;         
int Right_motor_go=6;        
int Right_motor_back=7;    
int Right_motor_en=5;     
int Left_motor_en=10;  
//==============================
/*Set BUZZER port*/
int BUZZER = 12;             

/*Set Button port*/
int KEY = 13;
/*Set right &left LED port*/
int right_led=4;
int left_led=3;

/*Set Ultrasonic Sensor*/
int Echo = A1;  // Set Echo port
int Trig = A0; // Set Trig port
int Distance = 0;
/*Line Walking*/
const int SensorRight = A2;   	// Set Right Line Walking Infrared sensor port
const int SensorLeft = A3;     	// Set Left Line Walking Infrared sensor port
int SL;    // State of Left Line Walking Infrared sensor
int SR;    // State of Right Line Walking Infrared sensor
/*follow*/
const int SensorRight_2 = A4;     // Right Tracking Infrared sensor
const int SensorLeft_2 = A5;     // Left Tracking Infrared sensor
int SL_2;    // State of Left Tracking Infrared sensor
int SR_2;    // State of Right Tracking Infrared sensor

void setup()
{
  
  softSerial.begin(9600);
  softSerial.listen();
  Serial.begin(9600);  // Set Bluetooth baud rate 9600
  
 //Initialize motor drive for output mode
  pinMode(Left_motor_go,OUTPUT); 
  pinMode(Left_motor_back,OUTPUT);
  pinMode(Right_motor_go,OUTPUT);
  pinMode(Right_motor_back,OUTPUT);
  pinMode(Right_motor_en,OUTPUT);
  pinMode(Left_motor_en,OUTPUT);
  pinMode(BUZZER, OUTPUT);// Set buzzer as output
  pinMode(left_led, OUTPUT);//Set led as output
  pinMode(right_led, OUTPUT);//Set led as output
  pinMode(KEY, INPUT_PULLUP);// Set button as input and internal pull-up mode

  digitalWrite(BUZZER, LOW);   //Set beep mute
  digitalWrite(Left_motor_en, HIGH); // set left motor enble
  digitalWrite(Right_motor_en, HIGH); // set right motor enble


while(!Serial)
    ;

  /*Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
  //Phpoc.begin();

  server.beginWebSocket("car_control");

  Serial.print("WebSocket server address : ");
  Serial.println(Phpoc.localIP()); 
  */
}
void run1()     // Advance
{
  digitalWrite(Left_motor_en,HIGH);  // Left motor enable
  analogWrite(Left_motor_en,157);
  digitalWrite(Right_motor_en,HIGH);  // Right motor enable
  analogWrite(Right_motor_en,157);
  digitalWrite(Right_motor_go,HIGH);  // right motor go ahead
  digitalWrite(Right_motor_back,LOW);   
  analogWrite(Right_motor_go,255);//PWM--Pulse Width Modulation(0~255). right motor go speed is 255.
  analogWrite(Right_motor_back,0); 
  digitalWrite(Left_motor_go,HIGH);  // set left motor go ahead
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,135);//PWM--Pulse Width Modulation(0~255).left motor go speed is 135.
  analogWrite(Left_motor_back,0);
  //delay(time * 100);   //Running time can be adjusted 
  digitalWrite(right_led,LOW);//rihgt led OFF
  digitalWrite(left_led,LOW);//left led OFF
}

void brake()         //Stop
{
  digitalWrite(Left_motor_back, LOW);
  digitalWrite(Left_motor_go, LOW);
  digitalWrite(Right_motor_go, LOW);
  digitalWrite(Right_motor_back, LOW);
   digitalWrite(left_led, HIGH);
  digitalWrite(right_led, HIGH);
  
}

void left()         //Turn left
{
  digitalWrite(Right_motor_go,HIGH);	// right motor go ahead
  digitalWrite(Right_motor_back,LOW);
  analogWrite(Right_motor_go,250); // PWM--Pulse Width Modulation(0~255) control speedright motor go speed is 255.
  analogWrite(Right_motor_back,0);
  digitalWrite(Left_motor_go,LOW);   // left motor stop
  digitalWrite(Left_motor_back,LOW); 
  analogWrite(Left_motor_go,0); 
  analogWrite(Left_motor_back,0); 
  digitalWrite(left_led, HIGH);
  digitalWrite(right_led, LOW);
}

void spin_left()         //Left rotation
{
  digitalWrite(Right_motor_go,HIGH);// right motor go ahead
  digitalWrite(Right_motor_back,LOW);
  analogWrite(Right_motor_go,200);// PWM--Pulse Width Modulation(0~255) control speed ,right motor go speed is 200.
  analogWrite(Right_motor_back,0);
  digitalWrite(Left_motor_go,LOW);   // left motor back off
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0); 
  analogWrite(Left_motor_back,200);// PWM--Pulse Width Modulation(0~255) control speed,left motor back speed is 200.
   digitalWrite(left_led, HIGH);
  digitalWrite(right_led, LOW);
}

void right()        //turn right
{
  digitalWrite(Right_motor_go,LOW);   // right motor stop
  digitalWrite(Right_motor_back,LOW);
  analogWrite(Right_motor_go,0); 
  analogWrite(Right_motor_back,0);
  digitalWrite(Left_motor_go,HIGH);// left motor go ahead
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,230);// PWM--Pulse Width Modulation(0~255) control speed ,left motor go speed is 255.
  analogWrite(Left_motor_back,0);
   digitalWrite(left_led, LOW);
  digitalWrite(right_led, HIGH);
}

void spin_right()        //Right rotation
{
    digitalWrite(Right_motor_go,LOW);  // right motor back off
  digitalWrite(Right_motor_back,HIGH);
  analogWrite(Right_motor_go,0); 
  analogWrite(Right_motor_back,200);// PWM--Pulse Width Modulation(0~255) control speed
  digitalWrite(Left_motor_go,HIGH);// left motor go ahead
  digitalWrite(Left_motor_back,LOW);
  analogWrite(Left_motor_go,200);// PWM--Pulse Width Modulation(0~255) control speed 
  analogWrite(Left_motor_back,0);
   digitalWrite(left_led, LOW);
  digitalWrite(right_led, HIGH);
}

void back()          //back off
{
   digitalWrite(Right_motor_go,LOW); //right motor back off
  digitalWrite(Right_motor_back,HIGH);
  analogWrite(Right_motor_go,0);
  analogWrite(Right_motor_back,150);// PWM--Pulse Width Modulation(0~255) control speed
  analogWrite(Right_motor_en,165);
  digitalWrite(Left_motor_go,LOW);  //left motor back off
  digitalWrite(Left_motor_back,HIGH);
  analogWrite(Left_motor_go,0);
  analogWrite(Left_motor_back,140);// PWM--Pulse Width Modulation(0~255) control speed
  digitalWrite(left_led, LOW);
  digitalWrite(right_led, HIGH);
  //led();
}
void whistle()   //beep sounds
{
    digitalWrite(BUZZER, HIGH); //sounds
    delay(2000);//delay 2s
    digitalWrite(BUZZER, LOW); //mute
   // delay(2);//delay 2s
  //}
}

/*main loop*/
void loop()
{
 voiceControl();
 //webCarControl();
}


void webCarControl()
{
   // wait for a new client:
  PhpocClient client = server.available();

  if (client) {
    String slideStr = client.readLine();
    Serial.println("client command is =>"+slideStr);
    if(slideStr)
    {
      slideName = slideStr.charAt(0);
      slideValue = slideStr.substring(1).toInt();


      if(slideName == 'D')//stop
      {
        Serial.println("stop car");
           brake();
      }

       if(slideName == 'B')//left
      {
         Serial.println("move left");
           left();
      }

       if(slideName == 'C')//right
      {
         Serial.println("move right");
           right();
      }

       if(slideName == 'A')//whistle
      {
        Serial.println("play music");
           whistle();
      }
    }
  }
}

void voiceControl()
{
  int cmd;

    if(softSerial.available())
    {

        Serial.println("Inside voiceControl() - command available");
      
        cmd = softSerial.read();
            switch(cmd)
        {
           case 3://play music
           Serial.println("play music");
           whistle();
           break;
          
           case 16://left
           Serial.println("move left");
           left();
           break;
           
           case 17://right
           Serial.println("move right");
           right();
           break;
           
           case 18://stop
           Serial.println("stop car");
           brake();
           break;
           
           case 19://start
           Serial.println("start car");
           run1();
           break;

           case 8://back
           Serial.println("back car");
           back();
           break;
        }
    }
}

Credits

Suyog Gunjal

Suyog Gunjal

5 projects • 12 followers
I am a technologist by education, profession and most importantly passion. Email : gunjalsuyog@gmail.com Mobile : +91 8275522404

Comments