Help me please, 3 ultrasound sensors

Hi,
I'm student of robotics and our teacher asked us to do a 3 ultrasound sensor circuit/system on Wokwi using Arduino Uno. He told us that it wouldn't work because of pulseIn, he also requested us to use libraries (<Ultrasound.h>, <NewPing.h>, <LiquidCrystal.h>. I've been looking for solutions and I didn't find anything. Here's the only code that I have:

#define ECHO_PIN 2
#define TRIG_PIN 3

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

float readDistanceCM() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  int duration = pulseIn(ECHO_PIN, HIGH);
  return duration * 0.034 / 2;
}

void loop() {
  float distance = readDistanceCM();

  bool isNearby = distance < 100;
  digitalWrite(LED_BUILTIN, isNearby);

  Serial.print("Measured distance: ");
  Serial.println(readDistanceCM());

  delay(100);
}

Welcome to the forum

What is the sketch supposed to do ?

Detect the distance and if it's nearby the LED will turn ON. Later we will do a smartcane (the one that blind people use) se we have to make this work, but it only reads the first one

does your code of post #1 work OK with one sensor?
the problem is pulsein will only work with one sensor
you will have to trigger the three sensosr and measure the width of each pulse concurrently using millis()
I suggest changing the code of post #1 to use mills() then extend it to three sensors

Do I have to change pulseIn with mills?

remove the call to pulsein() and use millis() to measure the pulse width(), e.g.

  1. trigger the sensor
  2. read millis() to get startTime
  3. wait for echo to go low
  4. pulse width = mills() - startTime
const byte numSensors = 3;
const byte ECHO_PIN[numSensors] = {2, 5, 8};
const byte TRIG_PIN[numSensors] = {3, 4, 7};

void setup() {
  Serial.begin(115200);
  pinMode(LED_BUILTIN, OUTPUT);
  for (byte s=0; s<numSensors; s++) {
    pinMode(TRIG_PIN[s], OUTPUT);
    pinMode(ECHO_PIN[s], INPUT);
  }
}

float readDistanceCM(byte s) {
  digitalWrite(TRIG_PIN[s], LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN[s], HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN[s], LOW);
  int duration = pulseIn(ECHO_PIN[s], HIGH);
  return duration * 0.034 / 2;
}

void loop() {
  bool isNearby = false;
  Serial.print("Measured distance: ");
  for (byte s=0; s<numSensors; s++) {
    float distance = readDistanceCM(s);
    isNearby |= distance < 100;
    Serial.print(distance);
    Serial.print(" ");
  }
  Serial.println();
  digitalWrite(LED_BUILTIN, isNearby ? HIGH : LOW);
  delay(100);
}

(Untested!)

It worked, thank you so much!!!!

I did you a favour. Now do me a favour in return. Read and understand the changes I made to the code. Ask about any change you don't understand.

1 Like

I was going to do that, my teacher wouldn't accept the answe if I don't understand it. A couple of days ago we worked with arrays so I understand it!!! Thank you!!!

Ok, so let's hear your explanation of this line

isNearby |= distance < 100;

(Sorry if I make some mistakes or I don't explain it with the right words, I'm not an English speaker)
We made a boolean variable that its values are true or false. The name of this variable is isNearby and we say it's false. I never worked with this variable I'm trying to find what true or false means in this variable. Then that line means that isNearby means that the distance is less than 100cm

Correct, but now there are 3 distance values from the 3 sensors. That code line executes 3 times for each execution of loop(). The led is only updated once, and it is lit if any one of the 3 distances are less than 100cm. How does that line of code combine the 3 distances?

Because we did two arrays with the outputs and the inputs at the beginning and then we did a for loop with the letter s. Then we did a float variable called distance where we put that its meaning is readDistanceCM(s). The s is the for loop and we defined readDistanceCM before with the outputs and the inputs. When we give isNearby a meaning we use the variable that we created (distance).

Your teacher will immediately know, from that explanation, that you don't understand the line of code I asked about!

Maybe this will help

isNearby |= distance < 100;

is a shorter way to write

isNearby = isNearby || distance < 100;

The "||" is logical-OR. If the value to the left is true OR the value to the right is true, then the answer is true. Does that help?

I will search more of this because I didn't know that existed. Thank you for your help!!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.