Temporary check for decision.

HI,is any one how to to do a temporary check for coding. Example below:

if (distance<5){

It will check the condition for the distance for eg one minutes before it do something else.

Sure. It's checks if the value of the variable named distance is less than 5, and then does something. You could have that something be wait a minute, or do you mean something else?

ChrisTenone:
Sure. It’s checks if the value of the variable named distance is less than 5, and then does something. You could have that something be wait a minute, or do you mean something else?

hi chrisTenone

Example interoute it will sent a message once when it detected the distance is < 5 .

Example:

if (distance <5){
Serial.print("Sent");
}

But the message only sent once when the distance been detected only when

if (distance >5){
Serial.print("Received");
}

Only when it detected another distance then it sent the another print again .And all over again and again

Joshtan:
Example:

if (distance <5){

Serial.print(“Sent”);
}





But the message only sent once when the distance been detected only when 




if (distance >5){
Serial.print(“Received”);
}




Only when it detected another distance then it sent the another print again .And all over again and again

Ah. Then you will want to use the way the loop() function works, and study Robin’s “Several things at the same time”, Bob’s “Using millis for timing, a beginner’s guide” or the Arduino IDE example, “Blink without delay”. These threads teach how to time a repetitive action, duch as check the distance and take an action.

You mean using

unsigned long currentMillis = millis();

  if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;

To count within microsecond?

I’m having a bit of trouble understanding what you want to do. Do you want to send the message only one time when the distance goes below 5? Or only when it changes?

Metallor:
I’m having a bit of trouble understanding what you want to do. Do you want to send the message only one time when the distance goes below 5? Or only when it changes?

Example:

if (dist <5)  {
     Serial.println("Checking distance again");
     if (dist<5 ){
     Serial.println("Sent");     
     
    
  }
  } 
    else if (dist >5){
      Serial.println("Received");

like if distance <5 it will check the condition and then sent println sent once only.
when dist >5 then println received once. And all over again .

To be honest that didn’t really clear anything up. I can say one thing about this latest bit of code though, this section:

if (dist <5)  {
     Serial.println("Checking distance again");
     if (dist<5 ){
     Serial.println("Sent");     
    
  }

The second if statement achieves nothing because dist has not changed since you entered the first if statement, so it will always be true.

If you want to check if something is true continuously for a certain length of time then you need to turn the test upside down and check if it is not true - like this

if (dist > 5) {
   lastTimeDistanceTooGreat = millis();  // reset the timer if it is outside the limit
}

if (millis() - lastTimDistanceTooGreat >= interval) {
  // it has been within range for interval
  // do stuff
}

If you just want something to happen the first time a test becomes true then compare the current value with the previous value - like this

if (dist < 5 and prevDistance >= 5) {
   // do stuff;
}
prevDistance = dist;

…R

Joshtan:
You mean using

unsigned long currentMillis = millis();

if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;




To count within microsecond?

Yes. It works with micros() just as well. Forms of this code are the standard way to do timing on microcontrollers. Essentially, you will want to set up a timing frame within loop to check your distance sensor every ... minute? Then when the distance is different than it was the last time you checked, update the display.

There seems to be a language problem.

Try asking in a language closer to your native language:
中文 - Chinese
Deutsch - German
Español - Spanish
Français - French
অসমীয়া, বাংলা, बड़ो (India)
Italiano - Italian
Nederlands - Dutch
Português - Portuguese
Русский - Russian
Svenska - Swedish, Suomi - Finnish, Norsk - Norwegian