Delay too long, need to shorten it

hi i am doing a project with ultrasonic sensor and each sensor need to have delay in between to ensure stablity but the total delay is also affecting the delay to light up the led from lighting up immedately

function: if any 5 of the ultrasonic sensor sense object less than 10cm,led will on immediately

problem: too much delay needed for ultrasonic sensor which caused led to not be able to light up immedately

the program run perfectly excepting for the delay

how can i solve it

int 1st_trigger = 3;
int 1st_echo = 4;
int 2nd_trigger = 5;
int 2nd_echo = 6;
int 3rd_trigger = 7;
int 3rd_echo = 8;
int 4th_trigger = 9;
int 4th_echo = 10;
int 5th_trigger = 11;
int 5th_echo = 12;

int led = 13

long 1st_duration;
long 2nd_duration;
long 3rd_duration;
long 4th_duration;
long 5th_duration;

int 1st_distance;
int 2nd_distance;
int 3rd_distance;
int 4th_distance;
int 5th_distance;

int Ultrasonic_status = 0;

void setup() 
{
  pinMode(1st_trigger,OUTPUT);
  pinMode(1st_echo,INPUT);
  pinMode(2nd_trigger,OUTPUT);
  pinMode(2nd_echo,INPUT);
  pinMode(3rd_trigger,OUTPUT);
  pinMode(3rd_echo,INPUT);
  pinMode(4th_trigger,OUTPUT);
  pinMode(4th_echo,INPUT);
  pinMode(5th_trigger,OUTPUT);
  pinMode(5th_echo,INPUT);
  pinMode(led,OUTPUT_PULLUP);
}

void loop()
{ 
  Ultrasonic();
if (Ultrasonic_status = 1)
{
digitalWrite(led,HIGH);
}
else
{
digitalWrite(led,LOW);
}
}

 void Ultrasonic()
 {
  digitalWrite(1st_trigger,HIGH);
  delayMicroseconds(5);
  digitalWrite(1st_trigger,LOW);
  1st_duration=pulseIn(1st_echo,HIGH);
  1st_distance = (1st_duration*0.034/2);

  digitalWrite(2nd_trigger,HIGH); 
  delayMicroseconds(5);
  digitalWrite(2nd_trigger,LOW);
  2nd_duration=pulseIn(2nd_echo,HIGH);
  2nd_distance = (2nd_duration*0.034/2);

  digitalWrite(3rd_trigger,HIGH); 
  delayMicroseconds(5);
  digitalWrite(3rd_trigger,LOW);
  3rd_duration=pulseIn(3rd_echo,HIGH);
  3rd_distance = (3rd_duration*0.034/2);

  digitalWrite(4th_trigger,HIGH); 
  delayMicroseconds(5);
  digitalWrite(4th_trigger,LOW);
  4th_duration=pulseIn(4th_echo,HIGH);
  4th_distance = (4th_duration*0.034/2);

  digitalWrite(5th_trigger,HIGH); 
  delayMicroseconds(5);
  digitalWrite(5th_trigger,LOW);
  5th_duration=pulseIn(5th_echo,HIGH);
  5th_distance = (5th_duration*0.034/2);
 
if ((1st_distance < 10) || (2nd_distance < 10) || (3rd_distance < 10) || (4th_distance < 10) || (5th_distance < 10))
{
Ultrasonic_status = 1;
}
else 
{
Ultrasonic_status = 0;
}

Use millis() instead of delay()

The functions delay() and delayMicroseconds() block the Arduino until they complete.
Have a look at how millis() is used to manage timing without blocking in Several Things at a Time.

And see Using millis() for timing. A beginners guide if you need more explanation.

The same techniques work with micros()

...R

I'm thinking that the 5us delays used to time the trigger pulses are hardly the problem. Nor will a millis() / micros() technique solve it. I'm guessing that the problem is that pulseIn() is a blocking function that waits for the return pulse from the sensor. Even worse, it has a default timeout (i.e. no return pulse recieved) of 1000000us.

You can calculate the required timeout for the pulsIn function; the formula is in your cide :wink:

Fill in the number for the distance (e.g. 50 cm) and calculate the time. With that time as timeout, you can measure distances up till 50 cm.

so is there any good solution to reduce the delay

1 Like

gfvalvo:
it has a default timeout (i.e. no return pulse recieved) of 1000000us.

Alextak:
so is there any good solution to reduce the delay

According to its playground page, the NewPing library:

  • Doesn't lag for a full second if no ping echo is received like all other ultrasonic libraries.
  • Ping sensors consistently and reliably at up to 30 times per second.

Don't declare the LED output as an INPUT, or it will never work.

Instead of reading all the ultrasonic sensors, then looking to see if any of the distances is < 10, check for distance < 10 after each sensor read and set the LED appropriately at that point. Also, instead of checking for distance < 10, forget about calculating the actual distance and do the comparison with the number returned by pulseIn(), won't save much time, but the calculation is a needless step.

Are the ultrasonic sensors pointed in directions where they will not interfere with each other if used simultaneously? If so, trigger all the sensors simultaneously, then check all the sensor inputs simultaneously at the expected interval and see if any of them have gone low. If the sensor is an HC-SR04, at the end of the trigger pulse, it outputs 8 pulses at a 40kHz rate, sets the sensor output HIGH, then when it receives an echo sets the sensor output LOW. Eight cycles of 40kHz takes 200uS, plus your interval (approximately 588uS from your equation), so check for any sensor low after 788uS. You would need to re-arrange the sensor trigger and echo pins, so a direct port read/write would work with all simultaneously, but with five sensors it would work nicely on an UNO to have the echo pins as 3-7 and the trigger pins as 8-12. This is assuming that you are not going to use the actual distances at some other point in your sketch.

Alextak:
pinMode(led,INPUT_PULLUP);

david_2018:
Don't declare the LED output as an INPUT, or it will never work.

I thought that one was already sorted the day before in OP's related thread:

meltDown:
....I just saw that your led is set as input pullup, not output:

(deleted)