Building a measurement system

Hello together,

I hope you can help me, because unfortunately I can't get any further at all. :o
At the moment I am trying to build a measuring system for volume data of pakets. I use the ELEGOO MEGA 2560 as controller and 3 HC-SR04 ultrasonic sensors.
I want to measure the height, depth, width with each sensor. But my proplem is that the sensor only measures when it is connected to the controller alone. I tried everything but can't find my mistake. The programm i wrote is looking like this:

int triggerPinLaenge = 2;
int echoPinLaenge = 3; 
int triggerPinBreite = 4;
int echoPinBreite = 5;
int triggerPinHoehe = 6;
int echoPinHoehe = 7;


long messungLaenge = 0;
int ergebnisLaenge = 0; 
long messungBreite = 0;
int ergebnisBreite = 0; 
long messungHoehe = 0;
int ergebnisHoehe = 0;

int gesamtLaenge = 35.5;
int gesamtBreite = 25;
int gesamtHoehe = 30;

int zwErgebnisL = 0;
int zwErgebnisB = 0;
int zwErgebnisH = 0;


void setup() {
 Serial.begin(9600);
 pinMode(triggerPinLaenge, OUTPUT);
 pinMode(echoPinLaenge, INPUT);
 pinMode(triggerPinBreite, OUTPUT);
 pinMode(echoPinBreite, INPUT);
 pinMode(triggerPinHoehe, OUTPUT);
 pinMode(echoPinHoehe, INPUT);

}

void loop() {
 
 digitalWrite(triggerPinLaenge, LOW);
 delayMicroseconds(2);
 digitalWrite(triggerPinLaenge, HIGH);
 delayMicroseconds(10);
 digitalWrite(triggerPinLaenge, LOW);

 delay(2);

 digitalWrite(triggerPinBreite, LOW);
 //delayMicroseconds(2);
 digitalWrite(triggerPinBreite, HIGH);
 delayMicroseconds(10);
 digitalWrite(triggerPinBreite, LOW);

 delay(2);

 digitalWrite(triggerPinHoehe, LOW);
 digitalWrite(triggerPinHoehe, HIGH);
 delayMicroseconds(10);
 digitalWrite(triggerPinHoehe, LOW);
 

 messungLaenge =  pulseIn(echoPinLaenge, HIGH);
  zwErgebnisL = (messungLaenge/2)/29;
  ergebnisLaenge = (gesamtLaenge - zwErgebnisL);
    // ergebnis = (messung/2)/29;
    
 messungBreite = pulseIn(echoPinBreite, HIGH);
   zwErgebnisB = (messungBreite/2)/29;
   ergebnisBreite = (gesamtBreite - zwErgebnisB);

  messungHoehe = pulseIn(echoPinHoehe, HIGH);
   zwErgebnisH = (messungHoehe/2)/29;
   ergebnisHoehe = (gesamtHoehe - zwErgebnisH);
  
Serial.print("Länge = ");
 Serial.print(ergebnisLaenge);
 Serial.print("  ");
 Serial.print("cm ");
 
Serial.print("Breite = ");
 Serial.print(ergebnisBreite);
 Serial.print(" ");
 Serial.print("cm ");

Serial.print("Höhe = ");
 Serial.print(ergebnisHoehe);
 Serial.print(" ");
 Serial.print("cm ");
 
   delay(2000);


}

I really hope someone can help me because I'm starting to get crazy :sob:.
I want to measure the volume data in one go. Later I want to connect an touch display, to start the measure with an start button. Please let me know if you have any ideas :art:

Thank you in advance.

Thank you for attempting to use code tags but you got it wrong

You need to put <­code> before the code and <­/code> after it. Please edit your post and change it. Even better and easier, Auto format the code in the IDE then right click and select Copy for Forum and post it here. It will already have code tags around it

Thank you for your message. Hope its right now :).

Thanks for adding the code tags

As to your problem, the first thing that I would do is to do the measurements independently rather than interleaving them. Measure the height, then the width then the depth. If I remember correctly pulseIn() is a blocking function so if you use it 3 times in quick succession, as you do, then the second and/or third one may time out waiting for the first one.

Do you have any idea how I could measure it independently? I thought about turning off the ultrasonic sensor after measuring but I couldn't find any solution for it.
Is there anything similar like the pulseIn() function?

Thanks alot.

measure height

  • expect the dimension
    then
    measure width
  • expect the dimension
    then
    measure depth
  • expect the dimension

if you have all three dimensions - calculate

just one step after another.

void loop() {
  digitalWrite(triggerPinLaenge, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPinLaenge, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPinLaenge, LOW);
  messungLaenge =  pulseIn(echoPinLaenge, HIGH);
  delay(2); // angst delay?

  digitalWrite(triggerPinBreite, LOW);
  //delayMicroseconds(2);
  digitalWrite(triggerPinBreite, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPinBreite, LOW);
  messungBreite = pulseIn(echoPinBreite, HIGH);
  delay(2);

  digitalWrite(triggerPinHoehe, LOW);
  digitalWrite(triggerPinHoehe, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPinHoehe, LOW);
  messungHoehe = pulseIn(echoPinHoehe, HIGH);
//...

btw, there is a very active forum German sub forum also (for your next question - not related to this ...)

Trigger sensor one.
Read sensor one’s result.
Wait 20ms.
Trigger sensor two.
Read sensor two’s result
Wait 20ms
Trigger sensor three.
Read sensor three’s result.
Wait 20ms.

Hi everyone,

thank you all for your help. It worked now with reading after the trigger. :slight_smile:

I will check out the german sub forum now for my next questions.

:grinning:

The NewPing library by Tim Eckel might be worth trying. It is in the Library Manager of the Arduino IDE.
https://bitbucket.org/teckel12/arduino-new-ping/