LOGIN
Ekhtiak Efat
6
Collaborator
Student (Project Leader)
 · PabnaBangladesh
Share
Report
Get Link
TASK
Open
Reward › Contributions per Helpful post
Estimated Duration › 2 week

Check for errors and bugs, also How can this be improved more

This is the code for the Voice Controlled Robot Car. This code is for continuous motion. Also it works with two sonar sensors, one on front and another one on back. it checks for any obstacles . and stops the cor if needed. but the working is not smooth enough, what can we do

//DISCONNECT THE BLUETOOTH MODULE BEFORE UPLOADING THE CODE TO ARDUINO
//COPYWRIGHT OF DIRTY PROJECTS - YOUTUBE CHANNEL
//ASSIGNING MOTOR DRIVER PINS TO ARDUINO PINS
//INTERCHANGE 5 & 6 IF MOTOR A OR LEFT MOTOR ROTATES WRONGLY
//INTERCHANGE 7 & 8 IF MOTOR B OR RIGHT MOTOR ROTATES WRONGLY
const int IN1 = 5; //FORWARD PIN FOR MOTOR A OR LEFT MOTOR
const int IN2 = 6; //BACKWARD PIN FOR MOTOR A OR LEFT MOTOR
const int IN3 = 7; //FORWARD PIN FOR MOTOR B OR RIGHT MOTOR
const int IN4 = 8; //BACKWARD PIN FOR MOTOR B OR RIGHT MOTOR
const int ENA = 9; //MUST BE A PWM PIN TO CONTROLL SPEED OF MOTOR A
const int ENB = 10; //MUST BE A PWM PIN TO CONTROLL SPEED OF MOTOR B
const int TM = 450; //DELAY TIME INTERVAL
const int sP = 100 ; //SPEED OF MOTOR IN THE RANGE OF 0 TO 255
const int sN1=3; //ECHO PIN OF FRONT SONAR
const int sN2=4; //TRIGGER PIN OF FRONT SONAR
const int sN3=12; //ECHO PIN OF BACK SONAR
const int sN4=13; //TRIGGER PIN OF BACK SONAR
String rS; //WORD OR DIRECTION VARIABLE TO BE RECEIVED FROM BLUETOOTH MODULE
int rR = 1; //NEEDED FOR ROTATION DIRECTION IN THE CASE OF ROUND COMMAND
int sT = 0; //FOR MAKING DOUBLE BACK AS STOP AND KEEPING A DATA OF PREVIOUS COMAND


void setup() {
 Serial.begin(9600);
 pinMode(IN1, OUTPUT);
 pinMode(IN2, OUTPUT);
 pinMode(IN3, OUTPUT);
 pinMode(IN4, OUTPUT);
 pinMode(ENA, OUTPUT);
 pinMode(ENB, OUTPUT);
 pinMode(sN1, INPUT);
 pinMode(sN2, OUTPUT);
 pinMode(sN3, INPUT);
 pinMode(sN4, OUTPUT);

}

void loop() {
 analogWrite(ENA, sP);
 analogWrite(ENB, sP);
 rS = ""; //CLEARING THE VARIABLE FOR NEW INPUT
    digitalWrite(IN2, LOW);
   digitalWrite(IN4, LOW);
   digitalWrite(IN1, LOW);
   digitalWrite(IN3, LOW); 
 while (Serial.available()) {
   delay(10);  //small delay to allow input buffer to fill

   char c = Serial.read();  //gets one byte from serial buffer
   rS += c;
 } //makes the string rS
 rS.toLowerCase(); //MAKING rS LOWER CASE
 if (rS.length() > 0) {
   Serial.println(rS);
   //FOR FORWARD
   if (rS == "1" || rS == "so do i" || rS == "rod" || rS == "for what" || rS == "pride" || rS == "roar" || rS == "fluoride" || rS == "or large" || rS == "call world" || rS == "call rod" || rS == "ordered" ||  rS == "forward" || rS == "call art" || rS == "photo id" || rS == "for work" || rS == "what do i do" || rS == "how do i" || rS == "water wired" || rS == "not tired" || rS == "call reward" || rS == "call o.r" || rS == "for a large" || rS == "so tired" || rS == "for a wired" || rS == "call wired" || rS == "for a white" || rS == "for the wired" || rS == "call dad" || rS == "out of liar" || rS == "call dart" || rS == "call reward" || rS == "photo on" || rS == "how to wire" || rS == "what tired" || rS == "auto art" || rS == "but tired" || rS == "auto wash" || rS == "set alarm" || rS == "for required" || rS == "400 on" || rS == "walmart" || rS == "for a while" || rS == "call award" || rS == "paul tired" || rS == "call iron" || rS == "auto world" || rS == "auto on" || rS == "call ward"  || rS == "for word" || rS == "call yard" || rS == "call o.r" || rS == "for a large" || rS == "so tired" || rS == "for a wired" || rS == "call wired" || rS == "for a white" || rS == "for the wired" || rS == "call dad" || rS == "out of liar" || rS == "call dart" || rS == "call reward" || rS == "photo on" || rS == "how to wire" || rS == "what tired" || rS == "auto art" || rS == "but tired" || rS == "auto wash" || rS == "set alarm" || rS == "for required" || rS == "400 on" || rS == "walmart" || rS == "for a while" || rS == "call award" || rS == "paul tired" || rS == "call iron" || rS == "auto world" || rS == "auto on" || rS == "call ward"  || rS == "for word" || rS == "or wire"   || rS == "call o.r" || rS == "for a large" || rS == "so tired" || rS == "for a wired" || rS == "call wired" || rS == "for a white" || rS == "for the wired" || rS == "call dad" || rS == "out of liar" || rS == "call dart" || rS == "call reward" || rS == "photo on" || rS == "how to wire" || rS == "what tired" || rS == "auto art" || rS == "but tired" || rS == "auto wash" || rS == "set alarm" || rS == "for required" || rS == "400 on" || rS == "walmart" || rS == "for a while" || rS == "call award" || rS == "paul tired" || rS == "call iron" || rS == "auto world" || rS == "auto on" || rS == "call ward"  || rS == "for word" || rS == "for ward" )
   {
     sT = 1; //SAVING STAT OF THE ROBOT
     forward(); //CALLING FUNCTION FOR FORWARD MOTION
   }
   //FOR BACKWARD
   else if (rS == "2" || rS == "back" || rS == "bag" || rS == "beck" || rS == "mac" || rS == "a" || rS == "heck" || rS == "hack")
   {  
     //MAKING DOUBBLE BACK AS STOP AND SINGLE BACK IS BACKWARD MOTION
     if (sT != 2) {
       sT = 2;
       backward(); //CALLING FUNCTION FOR BACKWARD MOTION
             }
     else if(sT==2) {
               sT = 0;
       stoprobo();  //CALLING FUNCTION TO STOP THE ROBOT      
     }
     
       }
       
      //FOR LEFT
   else if (rS == "4" || rS == "left" || rS == "dave" || rS == "yeah" || rS == "latest" || rS == "hey" || rS == "if")
   {
     sT = 4;
     digitalWrite(IN3, HIGH);
     delay(TM);
     forward();
   }
   
   //FOR RIGHT
   else if (rS == "3" || rS == "diet" || rS == "tight" || rS == "rice" || rS == "would i" || rS == "tonight" || rS == "write" || rS == "wright" || rS == "right" || rS == "at" || rS == "night" || rS == "alright" || rS == "light" || rS == "bright" || rS == "aight" || rS == "livr" || rS == "died" || rS == "that" || rS == "rides" || rS == "drives")
   {
      sT = 3;
     digitalWrite(IN1, HIGH);
     delay(TM);
     forward();
   }
   
   //FOR DOING A ROUND CIRCLE
   else if ( rS == "round" || rS == "sound" || rS == "brown" || rS == "don\'t" || rS == "robin" || rS == "around" || rS == "ground" || rS == "hi" || rS == "silent" || rS == "violent" || rS == "down" || rS == "island" || rS == "land" || rS == "and" || rS == "all" || rS == "found" || rS == "bound" || rS == "band")
   { sT = 7;
     if (rR == 1)
     {
       digitalWrite(IN1, HIGH);
       rR = 2;
       while (!Serial.available());
     }
     else
     {
       digitalWrite(IN3, HIGH);
       rR = 1;
       while (!Serial.available());
     }
   }
   
   //FOR MAKING THE FRONTSIDE TO TURN BACK
   else if (rS == "5" || rS == "turn back" || rS == "darn back" || rS == "i\'m back" || rS == "don back" || rS == "i\'m")
   {
     sT = 5;
     digitalWrite(IN1, HIGH);
     digitalWrite(IN4, HIGH);
     delay(TM);
     forward();
         }
   
         //TO STOP THE CAR
   else if (rS == "6" || rS == "stop" || rS == "you stop" || rS == "he stopped" || rS == "stopped" || rS == "what\'s up" || rS == "the" || rS == "is top" || rS == "now" || rS == "how" || rS == "step")
   {
     sT = 6;
     stoprobo();
   }
 }
}

//FUNCTION FOR DISTANCE OF FRONT SONAR
long dS1()
 {
 long duration=0;
  digitalWrite(sN2, LOW);
 delayMicroseconds(2);
 digitalWrite(sN2, HIGH);
 delayMicroseconds(10); 
 digitalWrite(sN2, LOW);
 duration = pulseIn(sN1, HIGH, 2000);  //IT WILL WAIT HIGHEST 2000 MICROSECONDS AND IF NOTHING IT WILL RETURN 0
 return duration;
 } 
 
//FUNCTION FOR DISTANCE OF FRONT SONAR
  long dS2()
 {
 long duration=0;
 digitalWrite(sN4, LOW);
 delayMicroseconds(2);
 digitalWrite(sN4, HIGH);
 delayMicroseconds(10); 
 digitalWrite(sN4, LOW);
 duration = pulseIn(sN3, HIGH, 2000); //IT WILL WAIT HIGHEST 2000 MICROSECONDS AND IF NOTHING IT WILL RETURN 0
 return duration;
 }

  
//FUNCTION FOR FORWARD MOTION
void forward()
{
 Serial.println("Forward");
 digitalWrite(IN2, LOW);
 digitalWrite(IN4, LOW);
     digitalWrite(IN1, HIGH);
     digitalWrite(IN3, HIGH);
     while (!Serial.available())
     {
       long Value=0;
        Value=dS1(); //CALLING FRONT SONAR DISTANCE
   Serial.println(Value);
   if(Value>0 && Value<2000)
   {
   stoprobo(); //CALLING STOP FUNCTION TO STOP THE ROBOT
   }
  
       }
}

//FUNCTION FOR BACKWARD MOTION
void backward()
{
 Serial.println("Backward");
   digitalWrite(IN1, LOW);
   digitalWrite(IN3, LOW);
      digitalWrite(IN2, HIGH);
      digitalWrite(IN4, HIGH);
       while (!Serial.available())
     {
       long Value=0;
        Value=dS2(); //CALLING BACK SONAR DISTANCE
   Serial.println(Value);
   if(Value>0 && Value<2000)
   {
    stoprobo(); //CALLING STOP FUNCTION TO STOP THE ROBOT
   }
     }
}

//FUNCTION TO STOP THE ROBOT
void stoprobo(){
Serial.println("Stop");
   digitalWrite(IN2, LOW);
   digitalWrite(IN4, LOW);
   digitalWrite(IN1, LOW);
   digitalWrite(IN3, LOW);
     return;}
Arduino
Robotics
Microcontrollers
5 likes 
Like
Award Contribution
Load 3 previous comments
Suraj Parasuram
10
Top Contributor
Image Processing and Machine Learning enthusiast
 · StanfordU.S.
Share
Report
Get Link

what is sP??

Like
1 like 
Award Contribution
Ekhtiak Efat
6
Collaborator
Student (Project Leader)
 · PabnaBangladesh
Share
Report
Get Link

sP is speed of the motors. the range is 0-255, it is for motor driver

Like
0 like 
Award Contribution
Suraj Parasuram
10
Top Contributor
Image Processing and Machine Learning enthusiast
 · StanfordU.S.
Share
Report
Get Link

Oh sorry…i didnt see it.

Like
1 like 
Award Contribution
Ekhtiak Efat
6
Collaborator
Student (Project Leader)
 · PabnaBangladesh
Share
Report
Get Link

Its ok, no problem 😊😊😊

Like
0 like 
Award Contribution
Mohammed Shahi
6
Collaborator
Engineer
 · NagercoilIndia
Share
Report
Get Link

Hello @Ekhtiak…Your code looks fine and i believe it can run smoothly on your hardware unit… There may be errors in only two sections… 1)when you receive the serialcommand for backward direction(cross check) … Or 2)in your ds2() function… Can you post a video of your current output so that i can rectify errors perfectly… Thank you!!

Like
2 likes 
Award Contribution
Devansh Suri
8
Top Contributor
Collaborizm Robotothon 2017
 · New DelhiIndia
Share
Report
Get Link

awesome again

Like
1 like 
Award Contribution
JOIN TASK
LEAVE A REPLY
DISCOVER
CHAT
HIRE
ACTIVITY
FEED
New Post
Help
Start Project
Online Users
Share Link
Write something before you submit it!
Photo updated
Request Sent!
Updated
Copied to Clipboard