How to print one time serial in loop with Ultrasonic and Arduino

Hi, I am currently working on a project involving Ultrasonic and Arduino
I use the ultrasonic to measure the depth of a bin, but it print infinite and continuous command
How do I print one time in serial loop using ultrasonic? I use this code

int trigPin = 6;
int echoPin = 5;
long duration, cm;

void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

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

duration = pulseIn(echoPin, HIGH);
cm = (duration / 2) / 29.1;

if (cm < 15)
{
Serial.print(“P”);
delay(1000);
}

if ((cm > 15) && (cm < 25))
{
Serial.print(“M”);
delay(1000);
}

if (cm > 25)
{
Serial.print(“E”);
delay(1000);
}
}

How do I print one time in serial loop using ultrasonic?

The setup() function is for doing things one time. The loop() function is for doing them over and over.

Why are you reading the ultrasonic sensor in loop() if you only want to do it once?

Do you expect that nothing will be added to the bin?

Why do you need to use an Arduino to measure the depth of the bin? A tape measure would be more appropriate.

It is unclear, to me, what you want to do. Measuring and printing only once in setup() does not seem useful. You would need to reset the board to take another reading.

Do you want to print only when the level in the bin changes?

read the how to use this forum-please read sticky to see how to post code and some good advice on how to get the most from the forum.

Send the ultrasonic sensor data only when requested by the app?

groundFungus:
Do you want to print only when the level in the bin changes?

Yes please can you help me with that

PaulS:
The setup() function is for doing things one time. The loop() function is for doing them over and over.

Do you expect that nothing will be added to the bin?

I think using setup() is not useful you have to generate that code again and again or even reset the program

Yes, I'm expecting a trash will be added to the depth of the bin, hence measuring the level of the bin.

Your half way there, having used “if”. If you take a reading , assign it to a variable “ last read”. Next time in the loop “if” new reading ==last reading , do nothing .. when it eventually changes , after the send , make “last read” = new read ready for next time through the loop.
Or .. wait for a command from the host ; “if” received command , measure and send .

Here is a sketch to illustrate how to detect a change. I removed the delays and used millis() to do the timing (see beginner’s guide to millis()). Removed magic numbers, put the range finder code into a function and set the pulseIn timeout so the program won’t get stuck there waiting.

// Only print on change in distance.  By C Goulding

const byte lowLimit = 15;
const byte highLimit = 25;
const byte changeAmount = 1; // how much the range changes for an output
const unsigned long sampleTime = 1000;  // time between ranges
// no use measuring over a couple of feet
const unsigned long pulseInTimeout = 10000; 

const byte trigPin = 6;
const byte echoPin = 5;

long lastDistance;
byte cm;

void setup()
{
   Serial.begin(115200);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   // get initial value of oldDuration
   getRange();
   lastDistance = cm;
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = sampleTime;
   if (millis() - timer >= interval)
   {
      timer = millis();
      getRange();
      // check if out of range
      if (cm == 0)
      {
         cm = lastDistance;
      }
      //Serial.println(cm);
      // check for a change of 1 or more cm
      if (abs (cm - lastDistance) >= changeAmount)
      {
         
         if (cm < lowLimit)
         {
            Serial.print("P");
         }

         if ((cm >= lowLimit) && (cm <= highLimit))
         {
            Serial.print("M");
         }

         if (cm > highLimit)
         {
            Serial.print("E");
         }
         Serial.println();
         // save this distance for next time
         lastDistance = cm;
      }
   }
}

void getRange()
{   
   long duration;
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);
   // added timeout so the rangefinder will not block so long
   duration = pulseIn(echoPin, HIGH, pulseInTimeout);
   cm = (duration / 2) / 29.1;
}

Note that the code is posted in code tags to make it easy to copy to the IDE for examination? Read the how to use the forum-please read sticky to see how to post code properly and some good advice on how to get the most from the forum.

Thank you "groundFungus" :wink:
really help me a lot.. keep doing a great job

by the way I change the
Serial.begin(115200);

into
Serial.begin(9600);

There's a simple and neat answer but.... you can't respect this forum enough to read a post and FIX YOURS.

This is the thread, stickied near the top of the list: How to use this forum - please read.

You don't use code tags, you don't respect the forum, I don't respect you.