Multiple Ultrasonic sensor

hi i really need some help... Im a newbies. my question is how to code for multi ultrasonic sensor using the code below?

int tonePin = 8;    // buzzer pin
int trigPin = 9;    // sensor trigger pin
int echoPin = 10;   // sensor echo pin
int clockPin = 11;  // shift register clock pin
int latchPin = 12;  // shift register latch pin
int dataPin = 13;   // shift register data pin

unsigned long previousMillisLEDS = 0; // initialization of the previosMillis for LEDS
unsigned long previousMillisLOW = 0;  // initialization of the previosMillis for LOW distance buzzer
unsigned long previousMillisMID = 0;  // initialization of the previosMillis for MED distance buzzer
unsigned long previousMillisHIGH = 0; // initialization of the previosMillis for HIGH distance buzer

const int intervalLEDS = 100; // interval of refreshing the LEDS state
const int intervalLOW = 800;  // interval of the LOW distance buzzer
const int intervalMID = 400;  // interval of the MED distance buzzer
const int intervalHIGH = 100; // interval of the HIGH distance buzzer

const int freqLOW = 1500;  // frequency of the LOW distabce buzzer
const int freqMID = 1800;  // frequency of the MED distabce buzzer
const int freqHIGH = 2000; // frequency of the HIGH distabce buzzer

const int durLOW = 100;   // on time of the LOW distabce buzzer
const int durMED = 100;   // on time of the MED distabce buzzer
const int durHIGH = 80;   // on time of the HIGH distabce buzzer

const byte patterns[9] = {   // initialization of the patterns the LEDS are going to display
  B00000000,  // all LEDS OFF
  B00000001,  // 1 LED ON
  B00000011,  // 2 LEDS ON
  B00000111,  // 3 LEDS ON
  B00001111,  // 4 LEDS ON
  B00011111,  // 5 LEDS ON
  B00111111,  // 6 LEDS ON
  B01111111,  // 7 LEDS ON
  B11111111,  // 8 LEDS ON
};

int prox = 0; // initialization of the proximity value (0-8)
int dur;      // initialization of the duration between the Trigger and Echo signal of the sensor
int dist;     // initialization of the distance between the sensor and the object in front of it (in centimeters)

void setup() {
  pinMode(tonePin, OUTPUT);   // set tone pin to OUTPUT
  pinMode(trigPin, OUTPUT);   // set trigger pin to OUTPUT
  pinMode(echoPin, INPUT);    // set echo pin to INPUT
  pinMode(clockPin, OUTPUT);  // set clock pin to OUTPUT
  pinMode(latchPin, OUTPUT);  // set latch pin to OUTPUT
  pinMode(dataPin, OUTPUT);   // set data pin to OUTPUT
}

void loop() {

  unsigned long currentMillis = millis(); // set the currentMillis variable to the current number of milliseconds from the start of the loop

  if ((unsigned long)(currentMillis - previousMillisLEDS) >= intervalLEDS) {    // check if the time between the current time and previous time for LEDS is larger or equal to the interval the LEDS should stay on
    digitalWrite(latchPin, LOW);  // set the latch pin to LOW
    digitalWrite(trigPin, LOW);   // set the trigger pin to LOW
    delayMicroseconds(2);         // delay 2 microseconds
    digitalWrite(trigPin, HIGH);  // set the trigger pin to HIGH and send out a sound signal
    delayMicroseconds(100);       // delay 100 microseconds
    digitalWrite(trigPin, LOW);   // set the trigger pin to LOW
    dur = pulseIn(echoPin, HIGH); // caluclate the duration between the trigger and echo
    dist = dur / 2 / 29;          // calculate distance in centemeters based on the speed of sound

    prox = map(dist, 0, 48, 8, 0);    // map the distannce between 0 and 48 cm to a value between 0 and 8
    shiftOut(dataPin, clockPin, MSBFIRST, patterns[prox]);  // send the pattern to the shift register based on the prox value (0-8)
    digitalWrite(latchPin, HIGH);   // latch the shift register
    previousMillisLEDS = currentMillis;   // set the previousMillis for LEDS to the current time in milliseconds from the start of the loop
  }
  if (prox < 0) {   // if we get a proximity value less than 0 set it to 0
    prox = 0;
  }
  else if (prox == 6) {   // if we get a proximity value of 6
    if ((unsigned long)(currentMillis - previousMillisLOW) >= intervalLOW) {   // check if the time between the current time and previous time for LOW buzzer interal is larger or equal to the interval the buzzer should stay on
      tone(tonePin, freqLOW, 100);    // set the tone pin to the LOW frequency and an on time of 100 milliseconds
      previousMillisLOW = currentMillis; // set the previousMillis for LOW buzzer interal to the current time in milliseconds from the start of the loop
    }

  }
  else if (prox == 7) { // if we get a proximity value of 7
    if ((unsigned long)(currentMillis - previousMillisMID) >= intervalMID) {  // check if the time between the current time and previous time for MID buzzer interal is larger or equal to the interval the buzzer should stay on
      tone(tonePin, freqMID, 100);    // set the tone pin to the MID frequency and an on time of 100 milliseconds
      previousMillisMID = currentMillis;  // set the previousMillis for MID buzzer interal to the current time in milliseconds from the start of the loop
    }

  }
  else if (prox == 8) { // if we get a proximity value of 8
    if ((unsigned long)(currentMillis - previousMillisHIGH) >= intervalHIGH) {  // check if the time between the current time and previous time for HIGH buzzer interal is larger or equal to the interval the buzzer should stay on
      tone(tonePin, freqHIGH, 80);    // set the tone pin to the HIGH frequency and an on time of 80 milliseconds
      previousMillisHIGH = currentMillis;   // set the previousMillis for HIGH buzzer interal to the current time in milliseconds from the start of the loop
    }
  }
}

post moved - please don't hijack someone's else discussion, especially when it's totally unrelated

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).

Two things

  1. Use arrays for the sensors so you don't duplicate code
  2. Don't fire them all at once, go in sequence

Confused about one thing - you have only one buzzer. Obviously this leaves a big problem definition gap - how will you logically connect the ultrasonic data from 4 sensors to one buzzer? What should happen? What did the professor say? :slight_smile:

Have you defined how the code should behave?

Would you care to share your thoughts?

Hi,
What sensors are you using?

Have you looked at the NewPing library?
https://playground.arduino.cc/Code/NewPing/
It even has an example for 15 ultrasonic sensors.
It will make your coding so much easier.

Tom... :grinning: :+1: :coffee: :australia:

Other posts/duplicates DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.

thanks for the recommendation

im so sorry for being such a rude

The NewPing library handles ultrasonic rangefinders and has an example showing how to read multiple (15) sensors.

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