TheTNR
Published © GPL3+

4WD Smart Robot Car

If you have this car, you will be able to avoid obstacles and follow the necessary lines to reach your destination and protect the distances

AdvancedFull instructions provided8,716
4WD Smart Robot Car

Things used in this project

Hardware components

Arduino UNO & Genuino UNO
Arduino UNO & Genuino UNO
×1
Dual H-Bridge motor drivers L298
SparkFun Dual H-Bridge motor drivers L298
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
HC-05 Bluetooth Module
HC-05 Bluetooth Module
×1
Obstacle Avoidance Sensor
×2
Line Tracking Sensor
×2
Custom PCB
Custom PCB
×1
Li-Ion Battery 1000mAh
Li-Ion Battery 1000mAh
×1

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
3D Printer (generic)
3D Printer (generic)

Story

Read more

Schematics

b2ot_(1)_CUyhyycUXq.pdf

Code

btcontrol.ino

Arduino
const int motorA1  = 6;  // L298N'in IN3 Girii
  const int motorA2  = 7;  // L298N'in IN1 Girii
  const int motorB1  = 8; // L298N'in IN2 Girii
  const int motorB2  = 9;  // L298N'in IN4 Girii


  int i=0; //Dngler iin atanan rastgele bir deiken
  int j=0; //Dngler iin atanan rastgele bir deiken
  int state; //Bluetooth cihazndan gelecek sinyalin deikeni
  int vSpeed=255;     // Standart Hz, 0-255 aras bir deer alabilir

void setup() {
    // Pinlerimizi belirleyelim
    pinMode(motorA1, OUTPUT);
    pinMode(motorA2, OUTPUT);
    pinMode(motorB1, OUTPUT);
    pinMode(motorB2, OUTPUT);  
    pinMode(5, OUTPUT); 
    pinMode(10, OUTPUT);  
    pinMode(2, OUTPUT);  
    pinMode(3, OUTPUT);  
    pinMode(4, OUTPUT);   
    // 9600 baud hznda bir seri port aalm
    Serial.begin(9600);
}
 
void loop() {
  digitalWrite(5,HIGH);
  digitalWrite(10,HIGH);
  
  /*Bluetooth balants koptuunda veya kesildiinde arabay durdur.
 (Aktif etmek iin alt satrn "//" larn kaldrn.)*/
//     if(digitalRead(BTState)==LOW) { state='S'; }

  //Gelen veriyi 'state' deikenine kaydet
    if(Serial.available() > 0){     
      state = Serial.read();   
    }
  
  /* Uygulamadan ayarlanabilen 4 hz seviyesi.(Deerler 0-255 arasnda olmal)*/
    if (state == '0'){
      vSpeed=0;}
    else if (state == '1'){
      vSpeed=100;}
    else if (state == '2'){
      vSpeed=180;}
    else if (state == '3'){
      vSpeed=200;}
    else if (state == '4'){
      vSpeed=255;}
     
  /***********************leri****************************/
  //Gelen veri 'F' ise araba ileri gider.
    if (state == 'F') {
      analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
        analogWrite(motorB1, vSpeed);      analogWrite(motorB2, 0); 
        digitalWrite(2,LOW); 
        digitalWrite(3,LOW); 
        digitalWrite(4,HIGH);
         
    }
  /**********************leri SA************************/
  //Gelen veri 'G' ise araba ileri sol(apraz) gider.
    else if (state == 'I') {
      analogWrite(motorA1,vSpeed ); analogWrite(motorA2, 0);  
        analogWrite(motorB1, 100);    analogWrite(motorB2, 0);
        digitalWrite(2,HIGH); 
        digitalWrite(4,LOW); 
    }
  /**********************leri SOL************************/
  //Gelen veri 'I' ise araba ileri sa(apraz) gider.
    else if (state == 'G') {
        analogWrite(motorA1, 100); analogWrite(motorA2, 0); 
        analogWrite(motorB1, vSpeed);      analogWrite(motorB2, 0);
        digitalWrite(3,HIGH); 
        digitalWrite(4,LOW);  
    }
  /***********************Geri****************************/
  //Gelen veri 'B' ise araba geri gider.
    else if (state == 'B') {
      analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, vSpeed); 
    }
  /**********************Geri Sol************************/
  //Gelen veri 'H' ise araba geri sol(apraz) gider
    else if (state == 'H') {
      analogWrite(motorA1, 0);   analogWrite(motorA2, 100); 
        analogWrite(motorB1, 0); analogWrite(motorB2, vSpeed); 
    }
  /**********************Geri Sa************************/
  //Gelen veri 'J' ise araba geri sa(apraz) gider
    else if (state == 'J') {
      analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed); 
        analogWrite(motorB1, 0);   analogWrite(motorB2, 100);
        digitalWrite(2,HIGH);  
        digitalWrite(4,LOW); 
    }
  /***************************Sol*****************************/
  //Gelen veri 'L' ise araba sola gider.
    else if (state == 'L') {
      analogWrite(motorA1, vSpeed);   analogWrite(motorA2, 150); 
        analogWrite(motorB1, 0); analogWrite(motorB2, 0); 
        digitalWrite(3,HIGH); 
        digitalWrite(4,LOW); 
    }
  /***************************Sa*****************************/
  //Gelen veri 'R' ise araba saa gider
    else if (state == 'R') {
      analogWrite(motorA1, 0);   analogWrite(motorA2, 0); 
        analogWrite(motorB1, vSpeed);   analogWrite(motorB2, 150);
        digitalWrite(2,HIGH); 
        digitalWrite(4,LOW);      
    }
  
  /************************Stop*****************************/
  //Gelen veri 'S' ise arabay durdur.
    else if (state == 'S'){
        analogWrite(motorA1, 0);  analogWrite(motorA2, 0); 
        analogWrite(motorB1, 0);  analogWrite(motorB2, 0);
        digitalWrite(4,LOW); 
    }  
}

line_tracking.ino

Arduino
#define echoPin A0 //Ultrasonik sensrn echo pini Arduino'nun 12.pinine
#define trigPin A1 //Ultrasonik sensrn trig pini Arduino'nun 13.pinine tanmland.

#define MotorR1 8
#define MotorR2 9
#define MotorRE 10  // Motor pinlerini tanmlyoruz.
#define MotorL1 6
#define MotorL2 7
#define MotorLE 5


byte timer=0;
long sure, uzaklik; //sre ve uzaklk diye iki deiken tanmlyoruz.

void setup() {


  // ultrasonik sensr Trig pininden ses dalgalar gnderdii iin OUTPUT (k),
  // bu dalgalar Echo pini ile geri ald iin INPUT (Giri) olarak tanmlanr.
  pinMode(echoPin, INPUT);
  pinMode(trigPin, OUTPUT);

  pinMode(MotorL1, OUTPUT);
  pinMode(MotorL2, OUTPUT);
  pinMode(MotorLE, OUTPUT); //Motorlarmz k olarak tanmlyoruz.
  pinMode(MotorR1, OUTPUT);
  pinMode(MotorR2, OUTPUT);
  pinMode(MotorRE, OUTPUT);
  pinMode(11, INPUT);
  pinMode(12, INPUT);

 pinMode(4, OUTPUT);
  Serial.begin(9600);

}

void loop() {


 
  if(digitalRead(11)==1 && digitalRead(12)==0)
  {
   sag(); // ileri git
    digitalWrite(4,LOW);
  }
  if(digitalRead(11)==0&& digitalRead(12)==1)
  {
   sol(); // ileri git
    digitalWrite(4,LOW);
  }
  if(digitalRead(11)==1 && digitalRead(12)==1)
  {
   ileri(); // ileri git
    digitalWrite(4,LOW);
  }
  if(digitalRead(11)==0 && digitalRead(12)==0)
  {
    timer++;
    if (timer<100)
    {
   sol(); // ileri git
    digitalWrite(4,LOW);
    timer=0;
    } 
    
  }
  
}



void ileri(){  // Robotun ileri ynde hareketi iin fonksiyon tanmlyoruz.

  digitalWrite(MotorR1, HIGH); // Sa motorun ileri hareketi aktif
  digitalWrite(MotorR2, LOW); // Sa motorun geri hareketi pasif
  analogWrite(MotorRE, 100); // Sa motorun hz 150

  digitalWrite(MotorL1, HIGH); // Sol motorun ileri hareketi aktif
  digitalWrite(MotorL2, LOW); // Sol motorun geri hareketi pasif
  analogWrite(MotorLE, 100); // Sol motorun hz 150
  
  
}


void sag(){ // Robotun saa dnme hareketi iin fonksiyon tanmlyoruz.

  digitalWrite(MotorR1, HIGH); // Sa motorun ileri hareketi aktif
  digitalWrite(MotorR2, LOW); // Sa motorun geri hareketi pasif
  analogWrite(MotorRE, 0); // Sa motorun hz 0 (Motor duruyor)

  digitalWrite(MotorL1, HIGH); // Sol motorun ileri hareketi aktif
  digitalWrite(MotorL2, LOW); // Sol motorun geri hareketi pasif
  analogWrite(MotorLE, 100); // Sol motorun hz 150
  
  
}

void sol(){ // Robotun saa dnme hareketi iin fonksiyon tanmlyoruz.

  digitalWrite(MotorR1, HIGH); // Sa motorun ileri hareketi aktif
  digitalWrite(MotorR2, LOW); // Sa motorun geri hareketi pasif
  analogWrite(MotorRE, 100); // Sa motorun hz 0 (Motor duruyor)

  digitalWrite(MotorL1, LOW); // Sol motorun ileri hareketi aktif
  digitalWrite(MotorL2, HIGH); // Sol motorun geri hareketi pasif
  analogWrite(MotorLE, 0); // Sol motorun hz 150
  
  
}


void geri(){ // Robotun geri ynde hareketi iin fonksiyon tanmlyoruz.

  digitalWrite(MotorR1, LOW); // Sa motorun ileri hareketi pasif
  digitalWrite(MotorR2, HIGH); // Sa motorun geri hareketi aktif
  analogWrite(MotorRE, 150); // Sa motorun hz 150

  digitalWrite(MotorL1, LOW); // Sol motorun ileri hareketi pasif
  digitalWrite(MotorL2, HIGH); // Sol motorun geri hareketi aktif
  analogWrite(MotorLE, 150); // Sol motorun hz 150
  
}
void dur(){  // Robotun ileri ynde hareketi iin fonksiyon tanmlyoruz.

  digitalWrite(MotorR1, HIGH); // Sa motorun ileri hareketi aktif
  digitalWrite(MotorR2, LOW); // Sa motorun geri hareketi pasif
  analogWrite(MotorRE, 0); // Sa motorun hz 150

  digitalWrite(MotorL1, HIGH); // Sol motorun ileri hareketi aktif
  digitalWrite(MotorL2, LOW); // Sol motorun geri hareketi pasif
  analogWrite(MotorLE, 0); // Sol motorun hz 150
}

obstacle_avoiding.ino

Arduino
#include <Arduino.h>
#include <Wire.h>
#include <SoftwareSerial.h>


double angle_rad = PI/180.0;
double angle_deg = 180.0/PI;
void sol();
void ileri();
void saga();
void geri();
double uzaklik;
float getDistance(int trig,int echo){
    pinMode(trig,OUTPUT);
    digitalWrite(trig,LOW);
    delayMicroseconds(2);
    digitalWrite(trig,HIGH);
    delayMicroseconds(10);
    digitalWrite(trig,LOW);
    pinMode(echo, INPUT);
    return pulseIn(echo,HIGH,30000)/58.0;
}


void sol()
{
    analogWrite(5,150);
    
    digitalWrite(6,0);
    
    digitalWrite(7,1);
    
    digitalWrite(8,1);
    
    digitalWrite(9,0);
    
    analogWrite(10,150);
    
}

void ileri()
{
    analogWrite(5,100);
    
    digitalWrite(6,1);
    
    digitalWrite(7,0);
    
    digitalWrite(8,1);
    
    digitalWrite(9,0);
    
    analogWrite(10,100);
    
}

void saga()
{
    analogWrite(5,100);
    
    digitalWrite(6,1);
    
    digitalWrite(7,0);
    
    digitalWrite(8,0);
    
    digitalWrite(9,1);
    
    analogWrite(10,100);
    
}

void geri()
{
    analogWrite(5,100);
    
    digitalWrite(6,0);
    
    digitalWrite(7,1);
    
    digitalWrite(8,0);
    
    digitalWrite(9,1);
    
    analogWrite(10,100);
    
}


void setup(){
    
    pinMode(5,OUTPUT);
    pinMode(6,OUTPUT);
    pinMode(7,OUTPUT);
    pinMode(8,OUTPUT);
    pinMode(9,OUTPUT);
    pinMode(10,OUTPUT);
    pinMode(4,OUTPUT);
    pinMode(16,INPUT);
    pinMode(17,INPUT);
    pinMode(2,OUTPUT);
    pinMode(3,OUTPUT);
}

void loop(){
    
    uzaklik = getDistance(15,14);
    digitalWrite(4,1);
    if((((digitalRead(16))==(1))) && ((((digitalRead(17))==(1))) && ((uzaklik) > (20)))){
        ileri();
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,0);
    }
    if((((digitalRead(17))==(1))) && (((uzaklik) > (20)) && (((digitalRead(16))==(0))))){
        sol();
        digitalWrite(3,1);
    }
    if((((digitalRead(17))==(1))) && (((20) > (uzaklik)) && (((digitalRead(16))==(1))))){
        geri();
        _delay(0.25);
        saga();
        digitalWrite(2,1);
        _delay(0.15);
    }
    if((((digitalRead(17))==(1))) && (((uzaklik) > (20)) && (((digitalRead(16))==(0))))){
        sol();
        digitalWrite(3,1);
    }
    if((((digitalRead(17))==(0))) && (((uzaklik) > (20)) && (((digitalRead(16))==(1))))){
        saga();
        digitalWrite(2,1);
    }
    if((((digitalRead(17))==(0))) && (((uzaklik) > (20)) && (((digitalRead(16))==(0))))){
        ileri();
        digitalWrite(2,0);
        digitalWrite(3,0);
        digitalWrite(4,0);
    }
    if((((digitalRead(17))==(0))) && (((20) > (uzaklik)) && (((digitalRead(16))==(1))))){
        saga();
        digitalWrite(2,1);
    }
    if((((digitalRead(17))==(0))) && (((20) > (uzaklik)) && (((digitalRead(16))==(0))))){
        geri();
        _delay(0.25);
        saga();
        digitalWrite(2,1);
        _delay(0.15);
    }
    
    _loop();
}

void _delay(float seconds){
    long endTime = millis() + seconds * 1000;
    while(millis() < endTime)_loop();
}

void _loop(){
    
}

Credits

TheTNR

TheTNR

5 projects • 96 followers
A teacher at high school. I am an electronics teacher.

Comments