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
}