The "other one" works if you put delays between the pings... "mosfets" turn on over 200cm (pardon the pacmen 01042024)
Need to see a wiring diagram/schematic of how you have things connected.
I stopped using the newping code, cuz it was having a lot of problems, I added the delay but it was still messing up
@l4v3r, please do not cross-post. Threads merged.
yh, sorry
What problem(s)? You did not try. I tried.
I did, the measurements get stuck on the same number very often, and the delay for the mosfet to go on is too big. This is 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();
delay(50);
int cm_2 = sonar_2.ping_cm();
delay(50);
int cm_3 = sonar_3.ping_cm();
delay(50);
// 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);
}
Your code uses pins 8,9,12 for the mosfets but you schematic shows 11,12,13
yh, I changed the pins in the real world version
So, if you disconnect the wires from pins 10, 11, ans 12, does the code work correctly?
yh on tinkercad
So you don't have a real circuit?
I do, but I can't use 10 and 11 cuz there r pins stuck in them
So does the real circuit work if you remove the wires from 8,9,12?
wdym?
If you disconnect the wires from pins 8, 9 and 12, and you run the code you posted, do the unltasonic sensors give you the correct readings?
Yh, I just tried, when I disconnected 8,9,12 the readings were consistently accurate
Are you really using 9V battery to power 3 pump DC motors? Do you have flyback diodes over pump motors? Any photo of what you`re actually doing?
OK, then the problem is with the way you have your motors and mosfets connected.