Problem with code or hardware?

I am quite new at arduino and im trying to create a system with three ultrasonic sensors, which when they detect something, their respective led lights up and a buzzer activates. However, when I activated it the led and buzzer of one ultrasonic sensor wont turn off, the second sensor's led wont turn on, and the other detects and the led activates but its buzzer doesn't.

This is my code:

const int echoPinR = 2; const int trigPinR = 3;
const int echoPinC = 6; const int trigPinC = 7;
const int echoPinL = 4;const int trigPinL = 5;
const int alertLedPinR = 8;const int alertLedPinL = 9;
const int alertLedPinC = 10;
const int buzzerR = 12; const int buzzerL = 11;

void setup() {
pinMode(trigPinR, OUTPUT);
pinMode(echoPinR, INPUT);
pinMode(trigPinL, OUTPUT);
pinMode(echoPinL, INPUT);
pinMode(trigPinC, OUTPUT);
pinMode(echoPinC, INPUT);

Serial.begin(9600);
}

void loop() {
long durationR, distanceR, durationC, distanceC, durationL, distanceL;

digitalWrite(trigPinR, LOW);
delayMicroseconds(2);
digitalWrite(trigPinR, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinR, LOW);
durationR = pulseIn(echoPinR, HIGH);
distanceR = durationR * 0.0343 / 2;

digitalWrite(trigPinC, LOW);
delayMicroseconds(2);
digitalWrite(trigPinC, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinC, LOW);
durationR = pulseIn(echoPinC, HIGH);
distanceR = durationC * 0.0343 / 2;

digitalWrite(trigPinL, LOW);
delayMicroseconds(2);
digitalWrite(trigPinL, HIGH);
delayMicroseconds(10);
digitalWrite(trigPinL, LOW);
durationL = pulseIn(echoPinL, HIGH);
distanceL = durationL * 0.0343 / 2;

Serial.print("Distance Sensor 1: ");
Serial.print(distanceR);
Serial.println(" cm");

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

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

if (distanceR < 40) {
digitalWrite(alertLedPinR, HIGH);
tone(buzzerR, 1000);
} else {
digitalWrite(alertLedPinR, LOW);
noTone(buzzerR);
}

if (distanceC < 40) {
digitalWrite(alertLedPinC, HIGH);

} else {
digitalWrite(alertLedPinC, LOW);

}

if (distanceL < 40) {
digitalWrite(alertLedPinL, HIGH);
tone(buzzerL, 1000);
} else {
digitalWrite(alertLedPinL, LOW);
noTone(buzzerL);
}
delay(500);
}

im trying to find out if this is a code problem or a hardware problem. Im open for advice

Welcome to the Arduino forum. Did you read about how to get the most out of the forum? please do!

Your code should have started with a simple program using just one ultrasonic sensor so you can figure out how it works and how to use it. You copied other peoples code and did not bother to study the documentation relating to the echo return form the sensor. It can and will return 0 if no echo is received, but you are not testing for 0. In addition, the code for the echo return also has a time-out variable which you do not have. When you leave it out, the time-out is one second. That is fine if you really want to stop your program right there for one second, but I am guessing you want it to run faster.

Get one sensor to work properly, then add a second and get it working and then the third. Do it in steps so the number of bugs is no overwhelming!

You may find that another sensor will hear the sound pulse or the echo of a pulse from a previous sensor. You will have to experiment and determine how long to wait between sensor triggers.

Check set-by-set:

Set-1: LED1, Buz1, Uson1
Set-2: LED2, Buz2, Uson2
Set-3: LED3, Buz3, Uson3

The “Start with Small Steps” strategy (SSS) recommends testing one small, manageable hardware or software module at a time and then gradually adding the next component, continuing this process until the entire project is completed. Give meaningful names to the variables/identifiers.

1. Connec Ultrasonic Sensor 1, LedUson1, and buzzerUson1 with UNOR3 as per Fig-1.


Figure-1:

2. Upload the following sketch for Ultrasonic Seosor 1 only. If it works, then add with it codes for Untrasonic Senspr 2 and for Ulttrasonic Sensor 3.

const int trigPinUson1 = 2;
const int echoPinUson1 = 3;
const int alertPinLedUson1 = 4;
const int buzzerPinUson1 = 5;

void setup()
{
  pinMode(trigPinUson1, OUTPUT);
  pinMode(echoPinUson1, INPUT);
  pinMode(alertPinLedUson1, OUTPUT);
  pinMode(buzzerPinUson1, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  long durationUson1, distanceUson1;

  digitalWrite(trigPinUson1, LOW);
  delayMicroseconds(2);

  durationUson1 = pulseIn(echoPinUson1, HIGH);
  distanceUson1 = durationUson1 * 0.0343 / 2;

  Serial.print("Distance Utrasonic Sensor 1: ");
  Serial.print(distanceUson1);
  Serial.println(" cm");

  if (distanceUson1 < 40)
  {
    digitalWrite(alertPinLedUson1, HIGH);
    tone(buzzerPinUson1, 1000);
  }
  else
  {
    digitalWrite(alertPinLedUson1, LOW);
    noTone(buzzerPinUson1);
  }

  delay(500);
}

Led pins and Buzzer pins should be declared as OUTPUT too.

Copy-Paste mistake: should be durationC (I didn't check them all...)

Here is the Wokwi simulation:

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Your second set of these...

  durationR = pulseIn(echoPinC, HIGH);
  distanceR = durationC * 0.0343 / 2;

Should be this...

  durationC = pulseIn(echoPinC, HIGH);
  distanceC = durationC * 0.0343 / 2;

Notice the two "R" and the two "C"

I forgot to include my results...

Before the change...

Distance Sensor 1: 0 cm
Distance Sensor 2: 395 cm
Distance Sensor 3: 0 cm
Distance Sensor 1: 0 cm
Distance Sensor 2: 395 cm
Distance Sensor 3: 0 cm

After the change...

Distance Sensor 1: 401 cm
Distance Sensor 2: 401 cm
Distance Sensor 3: 401 cm
Distance Sensor 1: 401 cm
Distance Sensor 2: 401 cm
Distance Sensor 3: 401 cm

Hi, @xidexjxo
Welcome to the forum.

Have you looked at the NewPing Library?
https://docs.arduino.cc/libraries/newping/

It has an example code for three sensors.

// ---------------------------------------------------------------------------
// Example NewPing library sketch that pings 3 sensors 20 times a second.
// ---------------------------------------------------------------------------

#include <NewPing.h>

#define SONAR_NUM 3      // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.

NewPing sonar[SONAR_NUM] = {   // Sensor object array.
  NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(6, 7, MAX_DISTANCE),
  NewPing(8, 9, MAX_DISTANCE)
};

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
    delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
    Serial.print(i);
    Serial.print("=");
    Serial.print(sonar[i].ping_cm());
    Serial.print("cm ");
  }
  Serial.println();
}

Tom.... :smiley: :+1: :coffee: :australia: