Fixed 4 Sensor Autonomous Car

Hi all,

I have recently written the below code to make my twin DC motor stop and start once it detects something via the ultrasonic sensor at the front.

I am now looking to improve on this design by mounting 3 more sensors fixed to the car so that it can also monitor both sides and the rear of the car.

Now I do not know even where to start with using more than one sensor, so if I post the working code that I have below, maybe you guys could give me a steer in the right direction?

Thank you very much in advance! :slight_smile:

const int echoPin = 10; //const because it is Read only

int val = 0;
int leftPin = 3;
int rightPin = 4;
int trigPin = 12; //because it changes
long duration; //to calculate distance
long HR_dist = 0; //calculated distance

void setup()
{
  delay(500);
  //establish comms
  pinMode(echoPin, INPUT);
  pinMode(leftPin, OUTPUT);
  pinMode(rightPin, OUTPUT);
  pinMode(trigPin, OUTPUT);
}

void loop()
{
  delay(500);
  {
    long duration; //establish the variable
  }
  //sensor is triggered by a HIGH pulse of 10 or more m/s.
  //First give a short LOW pulse beforehand to ensure a clean
  //HIGH pulse
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  
  digitalWrite(trigPin, LOW);
  
  //Read the signal from the sensor: a HIGH pulse whose
  //duration is the time from the sending of the ping from trigPin
  //to the reception of echoPin off a real world object
  
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  
  if (duration >=4000)
    {
      digitalWrite (leftPin, HIGH);
      digitalWrite (rightPin, HIGH);
    }else{
      digitalWrite (leftPin, HIGH);
      digitalWrite (rightPin, LOW);
    }
}

I am yet to run this sketch but it does verify…

Just need to mount some brackets and stuff onto my car and see how it runs.

Any help is very much appreciated. Specially with making my coding more efficient.

I am a total nub! :smiley:

//This sketch is going to run 4 ultrasonic sensors to guide a
//twin DC Motor Car

int leftM = 1; //right motor
int rightM = 2; //left motor
int leftMR = 0; //left motor reverse
int rightMR = 7; //right motor reverse
int ftrigPin = 3; //front trigpin
int rtrigPin = 4; //right trigpin
int ltrigPin = 5; //left trigpin
int RtrigPin = 6; //rear trigpin

const int fechoPin = 8; //front echopin
const int rechoPin = 9; //right echopin
const int lechoPin = 10; //left echopin
const int RechoPin = 11; //rear echopin

void setup()
{
  delay (100);
  pinMode (leftM, OUTPUT);
  pinMode (rightM, OUTPUT);
  pinMode (ftrigPin, OUTPUT);
  pinMode (rtrigPin, OUTPUT);
  pinMode (ltrigPin, OUTPUT);
  pinMode (RtrigPin, OUTPUT);
  pinMode (fechoPin, INPUT);
  pinMode (rechoPin, INPUT);
  pinMode (lechoPin, INPUT);
  pinMode (RechoPin, INPUT);
  long duration;
}

void loop()
{
  long duration; //used for sonar measurement
  
  //ping all 4 sensors
  digitalWrite(ftrigPin, LOW);
  digitalWrite(rtrigPin, LOW);
  digitalWrite(ltrigPin, LOW);
  digitalWrite(RtrigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(ftrigPin, HIGH);
  digitalWrite(rtrigPin, HIGH);
  digitalWrite(ltrigPin, HIGH);
  digitalWrite(RtrigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(ftrigPin, LOW);
  digitalWrite(rtrigPin, LOW);
  digitalWrite(ltrigPin, LOW);
  digitalWrite(RtrigPin, LOW);
  
  //Read value from front sensor and move forwards if clear
  duration = pulseIn(fechoPin, HIGH);
  duration = pulseIn(rechoPin, HIGH);
  duration = pulseIn(lechoPin, HIGH);
  duration = pulseIn(RechoPin, HIGH);
  
  if (fechoPin >= 4000)
  {
    digitalWrite(leftM, HIGH);
    digitalWrite(rightM, HIGH);
  }else{
    digitalWrite(leftM, LOW);
    digitalWrite(rightM, LOW);
  }
  
  if ((fechoPin <= 4000) && (rechoPin <= 4000))
  {
    if((fechoPin >= 2000) && (rechoPin >= 2000))
    {
      digitalWrite(leftM, HIGH);
      digitalWrite(rightM, LOW);
    }
  }
 
  
  else if ((fechoPin <= 4000) && (lechoPin <= 4000))
  {
    if ((fechoPin <= 2000) && (lechoPin <= 2000))
    {
      digitalWrite(leftM, LOW);
      digitalWrite(rightM, HIGH);
    }
  }
  
  if ((fechoPin <= 2000) && (lechoPin <= 2000))
  {
    if ((rechoPin <= 2000) && (RechoPin >= 4000))
    {
      digitalWrite(leftMR, HIGH);
      digitalWrite(rightMR, HIGH);
    }
  }
  
  if ((fechoPin <= 1800) && (lechoPin <= 1800))
  {
    digitalWrite(leftMR, LOW);
    digitalWrite(rightMR, HIGH);
  }
  
  if ((fechoPin <= 1800) && (rechoPin <= 1800))
    {
      digitalWrite(leftMR, HIGH);
      digitalWrite(rightMR, LOW);
    }
}