Can anyone please help me to modify the code so that it runs faster?

this is a program for a blindspot detection system using 4 ultrasonic and 2 ir sensors.
can someone help me in modifyng the code so that this runs faster?

#include<LiquidCrystal.h>
#define trigbl 14
#define echobl 15
#define trigbr 16
#define echobr 17
#define trigfl 18
#define echofl 19
#define trigfr 8
#define echofr 9
#define bl 10
#define br 11
long durationbl;
long durationbr;
long durationfl;
long durationfr;
int distancebl;
int distancebr;
int distancefl;
int distancefr;
int irr;
int irl;
int a;
int b;
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2);
pinMode(trigbl, OUTPUT);
pinMode(echobl, INPUT);
pinMode(trigbr, OUTPUT);
pinMode(echobr, INPUT);
pinMode(trigfl, OUTPUT);
pinMode(echofl, INPUT);
pinMode(trigfr, OUTPUT);
pinMode(echofr, INPUT);
pinMode(bl, INPUT_PULLUP);
pinMode(br, INPUT_PULLUP);
}
void loop()
{
digitalWrite(trigbl, LOW);
delay(2);
digitalWrite(trigbl, HIGH);
delay(10);
digitalWrite(trigbl, LOW);
durationbl = pulseIn(echobl, HIGH);
distancebl = durationbl * 0.034 / 2;
int a1=distancebl;
digitalWrite(trigbr, LOW);
delay(2);
digitalWrite(trigbr, HIGH);
delay(10);
digitalWrite(trigbr, LOW);
durationbr = pulseIn(echobr, HIGH);
distancebr = durationbr * 0.034 / 2;
int a2=distancebr;
digitalWrite(trigfl, LOW);
delay(2);
digitalWrite(trigfl, HIGH);
delay(10);
digitalWrite(trigfl, LOW);
durationfl = pulseIn(echofl, HIGH);
distancefl = durationfl * 0.034 / 2;
int a3=distancefl;
digitalWrite(trigfr, LOW);
delay(2);
digitalWrite(trigfr, HIGH);
delay(10);
digitalWrite(trigfr, LOW);
durationfr = pulseIn(echofr, HIGH);
distancefr = durationfr * 0.034 / 2;
int a4=distancefr;
irl =digitalRead(bl);
irr =digitalRead(br);
lcd.setCursor(1, 0);
lcd.print(“RL RR FL FR”);
if (a1<30 && irl==0)
{
a=0;
}
else
{
a=1;
}
if (a2<30 || irr==0)
{
b=0;
}
else
{
b=1;
}
if (a==1 && b==1 && a3>30 && a4>30)
{
lcd.setCursor(1, 1);
lcd.print("No Obstacle ");
}
else if ( a==0 && b==1 && a3>30 && a4>30)
{
lcd.setCursor(1, 1);
lcd.print(“XX “);
}
else if ( a==1 && b==0 && a3>30 && a4>30)
{
lcd.setCursor(1, 1);
lcd.print(” XX “);
}
else if ( a==1 && b==1 && a3<30 && a4>30)
{
lcd.setCursor(1, 1);
lcd.print(” XX “);
}
else if ( a==1 && b==1 && a3>30 && a4<30)
{
lcd.setCursor(1, 1);
lcd.print(” XX”);
}
else if ( a==0 && b==0 && a3>30 && a4>30)
{
lcd.setCursor(1, 1);
lcd.print("XX XX “);
}
else if ( a==1 && b==0 && a3<30 && a4>30)
{
lcd.setCursor(1, 1);
lcd.print(” XX XX ");
}

else if ( a==0 && b==1 && a3<30 && a4>30)
{
lcd.setCursor(1, 1);
lcd.print(“XX XX “);
}
else if ( a==0 && b==1 && a3>30 && a4<30)
{
lcd.setCursor(1, 1);
lcd.print(“XX XX”);
}
else if ( a==1 && b==0 && a3>30 && a4<30)
{
lcd.setCursor(1, 1);
lcd.print(” XX XX”);
}
else if ( a==0 && b==0 && a3<30 && a4>30)
{
lcd.setCursor(1, 1);
lcd.print(“XX XX XX “);
}
else if ( a==1 && b==1 && a3<30 && a4<30)
{
lcd.setCursor(1, 1);
lcd.print(” XX XX”);
}
else if ( a==1 && b==1 && a3>30 && a4<30)
{
lcd.setCursor(1, 1);
lcd.print(“XX XX XX”);
}
else if ( a==1 && b==0 && a3<30 && a4<30)
{
lcd.setCursor(1, 1);
lcd.print(“XX XX XX”);
}
else if ( a==1 && b==0 && a3<30 && a4<30)
{
lcd.setCursor(1, 1);
lcd.print(" XX XX XX");
}
else if ( a==0 && b==0 && a3<30 && a4<30)
{
lcd.setCursor(1, 1);
lcd.print("Call Help … ");
}
}

You could try reducing the pulseIn timeout.

Please remember to use code tags when posting code.

that delay if for transmitting the pulse from ultrasonic sensors.
so decreasing it wouldnt be a wise choice.
is there any alternative for digitalwrite() and digitalread()?

hruday1217:
that delay if for transmitting the pulse from ultrasonic sensors.
so decreasing it wouldnt be a wise choice.

I didn't mention any delay, I mentioned the timeout for the pulseIn function.

is there any alternative for digitalwrite() and digitalread()?

Yes, of course, but why?
You're talking about a processor that executes single instructions in the time in takes a beam of light to travel 20 metres.

can you suggest any other way to trim my code?

hruday1217:
can you suggest any other way to trim my code?

Sure - what are you trying to achieve?

AWOL:
Sure - what are you trying to achieve?

just a faster response time
theres a delay between the action and the result for ultrasonicsensors

theres a delay between the action and the result for ultrasonicsensors

That's just the speed of sound.
Or the timeout of the pulseIn function.

hruday1217:
just a faster response time
theres a delay between the action and the result for ultrasonicsensors

i tried using only 1 pulse as an input to all the four sensors instead of generating distinct pulses for 4 sensors
but that doesnt seem to work
if that worked im sure the code would become faster

One more time - HAVE YOU TRIED REDUCING THE TIMEOUT OF THE pulseIn function?

This looks like you have 4 ultrasonic sensors. Front-left, back right and so on.

Think about what happens when you are reversing and there is nothing in front of the front sensors. pulseIn() waits up to one second to listen for the echo from those sensors. It is never going to get a pulse.

Decide what is the longest distance you want to measure. Calculate that as microseconds. Put that into a constant in your code. Put that constant as the 3rd argument to pulseIn().

Haven't dealt with ultrasonic sensors yet, but would something like this work?

digitalWrite(trigbr, LOW);
digitalWrite(trigbl, LOW);
digitalWrite(trigfr, LOW);
digitalWrite(trigfl, LOW);
delay(2);
digitalWrite(trigbr, HIGH);
digitalWrite(trigbl, HIGH);
digitalWrite(trigfr, HIGH);
digitalWrite(trigfl, HIGH);
delay(10);
digitalWrite(trigbr, LOW);  
digitalWrite(trigbl, LOW);  
digitalWrite(trigfr, LOW);  
digitalWrite(trigfl, LOW);

evanmars:
Haven't dealt with ultrasonic sensors yet, but would something like this work?

digitalWrite(trigbr, LOW);

digitalWrite(trigbl, LOW);
digitalWrite(trigfr, LOW);
digitalWrite(trigfl, LOW);
delay(2);
digitalWrite(trigbr, HIGH);
digitalWrite(trigbl, HIGH);
digitalWrite(trigfr, HIGH);
digitalWrite(trigfl, HIGH);
delay(10);
digitalWrite(trigbr, LOW); 
digitalWrite(trigbl, LOW); 
digitalWrite(trigfr, LOW); 
digitalWrite(trigfl, LOW);

For some definitions of "work", but in practise, you wouldn't be able to distinguish returns.

there is the NewPing library which handles max distance for pinging and has cool features. you might want to explore how this can help

(source on GitHub)

Here’s a start;

#include<LiquidCrystal.h>
//                        bl  br  fl  fr
const byte  trigPin[4] = {14, 16, 18, 8};
const byte  echoPin[4] = {15, 17, 19, 9};
const unsigned long myTimeout = ???; // You need to decide this value - you could even have individual values for each sensor by making this array.


LiquidCrystal lcd(7, 6, 5, 4, 3, 2);


void setup()
{
  lcd.begin(16, 2);
  for (int i = 0; i < 4; i++) {
    pinMode(trigPin [i], OUTPUT);
    digitalWrite (trigPin [i], LOW);
    pinMode(echoPin [i], INPUT);
  }
}

unsigned long range (int index)
{
  digitalWrite(trigPin [index], HIGH);
  delay(10);
  digitalWrite(trigPin [index], LOW);
  return  (pulseIn(echoPin [index], HIGH, myTimeout) * 0.034) / 2;
}

Just remember to limit the aggregate ranging rate to no more than about 20 Hz, so no more than 5 Hz per sensor, if all four are active at the same time.

If I am not hijacking this thread too badly, can you explain why it would be different than one at a time?

I think I see. Cross-talk. All at the same time would pick up some of the signal from the others. One at a time ensures the sensor is only picking up its own ultrasonic signal.

evanmars:
If I am not hijacking this thread too badly, can you explain why it would be different than one at a time?

I think I see. Cross-talk. All at the same time would pick up some of the signal from the others. One at a time ensures the sensor is only picking up its own ultrasonic signal.

you got it :slight_smile:

It is also important not to ping too frequently, in case a late echo from an earlier ping is mistaken for a closer return.

MorganS:
This looks like you have 4 ultrasonic sensors. Front-left, back right and so on.

Think about what happens when you are reversing and there is nothing in front of the front sensors. pulseIn() waits up to one second to listen for the echo from those sensors. It is never going to get a pulse.

Decide what is the longest distance you want to measure. Calculate that as microseconds. Put that into a constant in your code. Put that constant as the 3rd argument to pulseIn().

will try this now

AWOL:
Here’s a start;

#include<LiquidCrystal.h>

//                        bl  br  fl  fr
const byte  trigPin[4] = {14, 16, 18, 8};
const byte  echoPin[4] = {15, 17, 19, 9};
const unsigned long myTimeout = ???; // You need to decide this value - you could even have individual values for each sensor by making this array.

LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup()
{
 lcd.begin(16, 2);
 for (int i = 0; i < 4; i++) {
   pinMode(trigPin [i], OUTPUT);
   digitalWrite (trigPin [i], LOW);
   pinMode(echoPin [i], INPUT);
 }
}

unsigned long range (int index)
{
 digitalWrite(trigPin [index], HIGH);
 delay(10);
 digitalWrite(trigPin [index], LOW);
 return  (pulseIn(echoPin [index], HIGH, myTimeout) * 0.034) / 2;
}



Just remember to limit the aggregate ranging rate to no more than about 20 Hz, so no more than 5 Hz per sensor, if all four are active at the same time.

thank you,i will immediately try this