HCSRO4 problems

Hello, I am doing a project that tracks a train and switches the electromagnet on when the train is closed. I am using a code for the HCSR04 but the readings for distance are messed up, sometimes they are correct but sometimes they are wrong, just really inconsistent, I don't think it's the circuit. This is the code I used.

#include <NewPing.h>

#define MAX_DISTANCE 200 // Maximum distance in centimeters

// Define ultrasonic sensor pins
const int trigpin_1 = 3;
const int echopin_1 = 2;
const int trigpin_2 = 5;
const int echopin_2 = 4;
const int trigpin_3 = 7;
const int echopin_3 = 6;

// Define MOSFET pins
const int mosfet_1 = 8;
const int mosfet_2 = 9;
const int mosfet_3 = 12;

// Create NewPing objects for each ultrasonic sensor
NewPing sonar_1(trigpin_1, echopin_1, MAX_DISTANCE);
NewPing sonar_2(trigpin_2, echopin_2, MAX_DISTANCE);
NewPing sonar_3(trigpin_3, echopin_3, MAX_DISTANCE);

void setup() {
  Serial.begin(9600); // Initialize serial communication
  pinMode(mosfet_1, OUTPUT);
  pinMode(mosfet_2, OUTPUT);
  pinMode(mosfet_3, OUTPUT);
}

void loop() {
  // Read distances from ultrasonic sensors
  int cm_1 = sonar_1.ping_cm();
  int cm_2 = sonar_2.ping_cm();
  int cm_3 = sonar_3.ping_cm();

  // Print distances to serial monitor
  Serial.print("Distance from Sensor 1: ");
  Serial.print(cm_1);
  Serial.println(" cm");

  Serial.print("Distance from Sensor 2: ");
  Serial.print(cm_2);
  Serial.println(" cm");

  Serial.print("Distance from Sensor 3: ");
  Serial.print(cm_3);
  Serial.println(" cm");

  // Control MOSFETs based on distance readings
  digitalWrite(mosfet_1, cm_1 < 10 ? HIGH : LOW);
  digitalWrite(mosfet_2, cm_2 < 10 ? HIGH : LOW);
  digitalWrite(mosfet_3, cm_3 < 10 ? HIGH : LOW);

  // Add a short delay to avoid excessive readings
  delay(100);
}

You may need to leave some time between pings to let the old echos die out. I usually wait about 35 ms between.

The quality of the return echo depends on the target. Solid surfaces perpendicular to the sensors are best. Soft targets or targets at an angle may not return strong signals.

Pleasse where shld I put the delay?

Delay between sending the pings and eliminate the 100ms at the end of the code.

What is the position in space of those 3 sensors, maybe you have interference problem?

For timed intervals in milliseconds, read about "millis()"...
https://docs.arduino.cc/built-in-examples/digital/BlinkWithoutDelay/

I tried it and it still didn't work, but I don't think the delay is the reason the readings are messing up

Sometimes the readings would be right at first, then after like 3 times cm<10, they start messing up. Like they stay on the last number they were on

Why don't you think that is the reason?

I don't know, I just didn't think it would affect the readings

You should make the sketch that disproves (fails) "...leave some time between pings..." is a solution.

I am doing a train project and using hcsro4's to track train positions. I was testing a code, when I use this code the readings are accurate and consistent:

const int trigpin_1 = 3;
const int echopin_1 = 2;
const int trigpin_2 = 5;
const int echopin_2 = 4;
const int trigpin_3 = 7;
const int echopin_3 = 6;
int cm_1;
int cm_2;
int cm_3;



long readUltrasonicDistance_1(int trigpin_1, int echopin_1) {
  pinMode(trigpin_1, OUTPUT);  // Clear the trigger
  digitalWrite(trigpin_1, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_1, LOW);
  pinMode(echopin_1, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_1, HIGH);
}

long readUltrasonicDistance_2() {
  pinMode(trigpin_2, OUTPUT);  // Clear the trigger
  digitalWrite(trigpin_2, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_2, LOW);
  pinMode(echopin_2, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_2, HIGH);
}
long readUltrasonicDistance_3() {
  pinMode(trigpin_3, OUTPUT);  // Clear the trigger
  digitalWrite(trigpin_3, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_3, LOW);
  pinMode(echopin_3, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_3, HIGH);
}

void setup() {
  Serial.begin(9600);
  pinMode(trigpin_1, OUTPUT);
  pinMode(echopin_1, INPUT);
  pinMode(trigpin_2, OUTPUT);
  pinMode(echopin_2, INPUT);
  pinMode(trigpin_3, OUTPUT);
  pinMode(echopin_3, INPUT);
}

void loop() {
 cm_1 = 0.01723 * readUltrasonicDistance_1(3, 2);
  Serial.print("Distance from Sensor 1: ");
  Serial.print(cm_1);
  Serial.println(" cm");

 cm_2 = 0.01723 * readUltrasonicDistance_2();
  Serial.print("Distance from Sensor 2: ");
  Serial.print(cm_2);
  Serial.println(" cm");

 cm_3 = 0.01723 * readUltrasonicDistance_3();
  Serial.print("Distance from Sensor 3: ");
  Serial.print(cm_3);
  Serial.println(" cm");

  delay(100); // Delay between measurements
}

But when I add the mosfets to turn on, the readings start to mess up, sometimes they r stuck on same reading, or they aren't as accurate:

const int trigpin_1 = 3;
const int echopin_1 = 2;
const int trigpin_2 = 5;
const int echopin_2 = 4;
const int trigpin_3 = 7;
const int echopin_3 = 6;
int cm_1;
int cm_2;
int cm_3;
const int mosfet_1 = 8;
const int mosfet_2 = 9;
const int mosfet_3 = 12;


long readUltrasonicDistance_1(int trigpin_1, int echopin_1) {
  pinMode(trigpin_1, OUTPUT);  // Clear the trigger
  digitalWrite(trigpin_1, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_1, LOW);
  pinMode(echopin_1, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_1, HIGH);
}

long readUltrasonicDistance_2() {
  pinMode(trigpin_2, OUTPUT);  // Clear the trigger
  digitalWrite(trigpin_2, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_2, LOW);
  pinMode(echopin_2, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_2, HIGH);
}
long readUltrasonicDistance_3() {
  pinMode(trigpin_3, OUTPUT);  // Clear the trigger
  digitalWrite(trigpin_3, LOW);
  delayMicroseconds(2);
  // Sets the trigger pin to HIGH state for 10 microseconds
  digitalWrite(trigpin_3, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigpin_3, LOW);
  pinMode(echopin_3, INPUT);
  // Reads the echo pin, and returns the sound wave travel time in microseconds
  return pulseIn(echopin_3, HIGH);
}

void setup() {
  Serial.begin(9600);
  pinMode(trigpin_1, OUTPUT);
  pinMode(echopin_1, INPUT);
  pinMode(trigpin_2, OUTPUT);
  pinMode(echopin_2, INPUT);
  pinMode(trigpin_3, OUTPUT);
  pinMode(echopin_3, INPUT);
  pinMode(mosfet_1, OUTPUT);
  pinMode(mosfet_2, OUTPUT);
  pinMode(mosfet_3, OUTPUT);
}

void loop() {
 cm_1 = 0.01723 * readUltrasonicDistance_1(3, 2);
  Serial.print("Distance from Sensor 1: ");
  Serial.print(cm_1);
  Serial.println(" cm");
  if (cm_1<20){digitalWrite(mosfet_1, HIGH);}
  else{digitalWrite(mosfet_1, LOW);}

 cm_2 = 0.01723 * readUltrasonicDistance_2();
  Serial.print("Distance from Sensor 2: ");
  Serial.print(cm_2);
  Serial.println(" cm");
  if (cm_2<20){digitalWrite(mosfet_2, HIGH);}
  else{digitalWrite(mosfet_2, LOW);}

 cm_3 = 0.01723 * readUltrasonicDistance_3();
  Serial.print("Distance from Sensor 3: ");
  Serial.print(cm_3);
  Serial.println(" cm");
  if (cm_3<20){digitalWrite(mosfet_3, HIGH);}
  else{digitalWrite(mosfet_3, LOW);}

  delay(100); // Delay between measurements
}

This seem to be the same as your other post

I thought. the code was just messed up, but know it works well without mosfet and doesn't work with mosfet, so I don't know if it's the code or the mosfet, cuz mosfets work well too

Why did you started a new thread?
Crossposting is a violation of the forum rules.

So which post is correct this one or the other one?

My bad, I thought it would be like a new topic since problem changed

This one is correct then

What is connected to the mosfet_1,2,3 pins?

It's meant to be an electromaget and led, but rn I'm using water pump and leds