Is there something wrong with my Ultrasonic code

hello please help me, my ultrasonic sensor sometime like crazy

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);// initialize the library with the numbers of the interface pins

#include <Servo.h> //includes the servo library

Servo myservo1;

int trigPin1 = 30;

int echoPin1 = 31;

int trigPin2 = 32;

int echoPin2 = 33;

int trigPin3 = 34;

int echoPin3 = 35;

int trigPin4 = 36;

int echoPin4 = 37;

int led1 = 48;

int led2 = 49;

int led3 = 46;

int led4 = 47;

int led5 = 44;

int led6 = 45;

int led7 = 42;

int led8 = 43;

int duration = 0;

int distance = 0;

int ir_1 = 4;

int ir_2 = 7;

int Total = 4;

int Space;

bool flag1 = 0;

bool flag2 = 0;

void setup()

{ Serial.begin (9600);

pinMode(trigPin1, OUTPUT);

pinMode(echoPin1, INPUT);

pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT);

pinMode(trigPin2, OUTPUT);

pinMode(echoPin2, INPUT);

pinMode(led3, OUTPUT);

pinMode(led4, OUTPUT);

pinMode(trigPin3, OUTPUT);

pinMode(echoPin3, INPUT);

pinMode(led5, OUTPUT);

pinMode(led6, OUTPUT);

pinMode(trigPin4, OUTPUT);

pinMode(echoPin4, INPUT);

pinMode(led7, OUTPUT);

pinMode(led8, OUTPUT);

Serial.begin(9600);

myservo1.attach(9);

myservo1.write(90);

lcd.begin(16, 2);

lcd.setCursor (0,0);

lcd.print(" SMART CAR ");

lcd.setCursor (0,1);

lcd.print(" PARKING ");

delay (2000);

lcd.clear();

Space = Total;

}

void loop()

{ //long duration, distance;

digitalWrite(trigPin1, LOW);

delayMicroseconds(2);

digitalWrite(trigPin1, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin1, LOW);

duration = pulseIn(echoPin1, HIGH);

distance = (duration/2) / 29.1;

if (distance < 8)

{ digitalWrite(led1,HIGH);

digitalWrite(led2,LOW);

}

else {

digitalWrite(led1,LOW);

digitalWrite(led2,HIGH);

}

digitalWrite(trigPin2, LOW);

delayMicroseconds(2);

digitalWrite(trigPin2, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin2, LOW);

duration = pulseIn(echoPin2, HIGH);

distance = (duration/2) / 29.1;

if (distance < 8)

{ digitalWrite(led3,HIGH);

digitalWrite(led4,LOW);

}

else {

digitalWrite(led3,LOW);

digitalWrite(led4,HIGH);

}

digitalWrite(trigPin3, LOW);

delayMicroseconds(2);

digitalWrite(trigPin3, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin3, LOW);

duration = pulseIn(echoPin3, HIGH);

distance = (duration/2) / 29.1;

if (distance < 8)

{ digitalWrite(led5,HIGH);

digitalWrite(led6,LOW);

}

else {

digitalWrite(led5,LOW);

digitalWrite(led6,HIGH);

}

digitalWrite(trigPin4, LOW);

delayMicroseconds(2);

digitalWrite(trigPin4, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin4, LOW);

duration = pulseIn(echoPin4, HIGH);

distance = (duration/2) / 29.1;

if (distance < 8)

{ digitalWrite(led7,HIGH);

digitalWrite(led8,LOW);

}

else {

digitalWrite(led7,LOW);

digitalWrite(led8,HIGH);

}

delay(500);

if(digitalRead (ir_1) == LOW && flag1==0){

if(Space>0){flag1=1;

if(flag2==0){myservo1.write(0); Space = Space-1;}

}else{

lcd.setCursor (0,0);

lcd.print(" SORRY PARKING ");

lcd.setCursor (0,1);

lcd.print(" FULL ");

delay (1000);

lcd.clear();

}

}

if(digitalRead (ir_2) == LOW && flag2==0){flag2=1;

if(flag1==0){myservo1.write(0); Space = Space+1;}

}

if(flag1==1 && flag2==1){

delay (1000);

myservo1.write(100);

flag1=0, flag2=0;

}

lcd.setCursor (0,0);

lcd.print("TOTAL SPACE: ");

lcd.print(Total);

lcd.setCursor (0,1);

lcd.print("SPACE LEFT : ");

lcd.print(Space);

}

Your sensors are probably confused by all the echoes they're receiving.
Put some delays between the pings.

(Too much code, BTW)

thank you for helping because i still new in coding. where i should put the delay?

Between the pings.

i already put the delay but the problem still the same

Ok.
Well at least I tried.

thank youuuuuuuu

Few remarks, maybe not related to your issue:

duration and distance are in INT format but you make calculation with float:
distance= (duration/2)/29.1

this could do the trick:
distance = ((float)distance/2)/29.1

you use servo and liquidcrystal libraries. Is it possible they interfer with the timer called by pulseIn() ?
My biggest fear is to have my code trapped down by hidden functions. I propose you to code your own timer function in place of pulseIn().

I worked with a nano 33 BLE and I wrote a sketch using only timer3 (or 4) and shorcuts (stuff not exactly but like interrupt). But it is dedicated to the nano 33 BLE board.

I also propose you group your distance measurment in a function and just send the TriggerPin and EchoPin. Then, you should be able to put a simple delay between any ultrasonic measurment and maybe detect if one is going crazy.

Did you mention your board? with this info, we could help you better if you manage another way to code your US measure.

Very, very unlikely.

@OP, get back to us when you're ready to engage.

What happens when the OP ties using the newping library?

Wait - you don't mean the single delay(500)?
No.
That's not between the pings.

can i know where pings actually

Did you write the code?

There is actually a 1 second delay between pings because of the 1 second time-out on the pulseln() if no echo. But if an echo is returned, the next sensor is pinged and just might hear the last echo from the previous sensor. Too much code and not likely to work well, as seems to be the problem.

I am using arduino mega board

Sorry sir, can u give me some examples for this solution. I still newbie in coding, so I cant imagine what you are saying rn. Sorry again

Get one ultrasonic sensor working reliably.
Then add another.
Don't forget to wait between sending pulses to the rangers.

About 30 milliseconds should be enough

Some comments in your code wouldn't go amiss.

1 Like

I was just going to suggest this. Troubleshoot by working with 1 piece at a time. If 1 sensor won't work then 10 probably won't either.
Joe

when i put only one sensor it work fine but if i added 1 more sensor it become crazy

already done that but the problem not solved