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 ...... ");
}
}
1 Answer 1
You may want to check the datasheet of your sonar sensors to see what is
the recommended trigger pulse duration. On a typical HC-SR04, it is ten
microseconds, not milliseconds. So use delayMicroseconds(10);
.
If your program runs really slowly, the culprit is most probably
pulseIn()
. This function will wait for an incoming pulse and, if there
is no incoming pulse, it waits one full second before timing out. This
can be simply fixed by providing a third argument to pulseIn()
, which
is the desired timeout in microseconds. Set it to something reasonable,
maybe around 25,000 μs for a maximum detection range of 4 m.
else if
blocks ......lcd.print("abc"); lcd.print("123");