Issue with checking distance condition!

static bool dist_reach = false;
if(dist <5) {
  delay(1000);
  Serial.println("Checking Condition again");
  delay(10000);
  if(!dist_reach)
  {
    if(dist<5){
      Serial.println("sent notifcation1 to Telegram");     
      sendTelegramMessage();
      delay(bulk_messages_mtbs);
      Bot_lasttime = 0;
      dist_reach = true;
    }
  }
}
else if (dist >5) {
  if(dist_reach) {
    Serial.println("sent notifcation2 to Telegram"); 
    sendTelegramMessage2();
    delay(bulk_messages_mtbs);
    Bot_lasttime = 0;
  dist_reach = false;
  }
}

When it dist <5 it will check condition first then sending TelegramMessage. But when during it checking the condition i take off my hand the dist >5 it still sending the message.The condition checking system is not working.Any good suggestion ?

void loop() {
  dist = sonar.ping_cm();
  Serial.print(dist);
  Serial.println(" cm");
  delay(3000);

  static bool dist_reach = false;
  if(dist <5) {
  delay(1000);
  Serial.println("Checking Condition again");
  delay(10000);
  if(!dist_reach)
  {
    if(dist<5){
      Serial.println("sent notifcation1 to Telegram");     
      sendTelegramMessage();
      delay(bulk_messages_mtbs);
      Bot_lasttime = 0;
      dist_reach = true;
    }
  }
}

  else if (dist >55) {
    if(dist_reach) {
      Serial.println("sent notifcation2 to Telegram"); 
      sendTelegramMessage2();
      delay(3000);
      Bot_lasttime = 0;
    dist_reach = false;
  }
}
}

This is full void loop code.

dist = sonar.ping_cm();

That line gets a new value for dist.

if(dist <5) {
  delay(1000);
  Serial.println("Checking Condition again");
  delay(10000);
  if(!dist_reach)
  {
    if(dist<5){

That Serial.print statement claims that it is getting a new value for dist, but it isn’t.

Also be aware that this line:

delay(10000);

says, stop running all the other code and ignore all the sensors for 10 seconds. So you shouldn’t expect it to be very responsive if it has delay lines in it.

dist = sonar.ping_cm();
Serial.print(dist);
Serial.println(" cm");
delay(100);

eh .I am using the dist=sonar.ping_cm(); because i want the distance to be flowing automatic.

if(dist <5) {
   delay(1000);
   Serial.println("Checking Condition again");
   delay(10000);
   if(!dist_reach)

the delay(10000);
is to the sensor have 10 seconds to checking the condition is still <5 before it sending message , so i taking both away ?

Joshtan:
the delay(10000);
is to the sensor have 10 seconds to checking the condition is still <5 before it sending message , so i taking both away ?

You have to write code to have the sensor check the distance. The code delay(10000) means the Arduino does literally NOTHING in that time.

 if(dist <5) {
       delay(1000);
       Serial.println("Checking Condition again");
       delay(10000);
       if(!dist_reach)
  {
          if(dist<5){
            Serial.println("sent notifcation1 to Telegram");
            sendTelegramMessage();
            delay(bulk_messages2_mtbs);
            Bot_lasttime = 0;
            dist_reach = true;
    }
  }
}

so i push the

 if(dist<5){
            Serial.println("sent notifcation1 to Telegram");
            sendTelegramMessage();
            delay(bulk_messages2_mtbs);
            Bot_lasttime = 0;
            dist_reach = true;

to below delay(10000);
?

void loop() {
  dist = sonar.ping_cm();
  Serial.print(dist);
  Serial.println(" cm");
  delay(100);
  static bool dist_reach = false;
  if(dist <5) {
       delay(1000);
       if(!dist_reach){
       Serial.println("Checking Condition again");
       if(dist<5){
            delay(3000);
            Serial.println("sent notifcation1 to Telegram");
            sendTelegramMessage();
            delay(bulk_messages2_mtbs);
            Bot_lasttime = 0;
            dist_reach = true;
  
       }
       }   
}
  else if (dist >55) {
    if(dist_reach) {
      Serial.println("sent notifcation2 to Telegram");
      sendTelegramMessage2();
      delay(3000);
      Bot_lasttime = 0;
    dist_reach = false;


}
}
}



[code]


yeah.I am changing it but it still cannot checking condition.........

Joshtan:

dist = sonar.ping_cm();

Serial.print(dist);
Serial.println(" cm");
delay(100);




eh .I am using the dist=sonar.ping_cm(); because i want the distance to be flowing automatic.

That doesn't make it automatic. That takes one reading, stores it in the variable dist and that's it. It won't read the sensor again until it comes to another line that tells it to read the sensor. For the rest of your program until it gets to that line again dist will simply keep the same value it had the one time you read the sensor. It's not automatically updating. That isn't how code works.

Delta_G:
That doesn't make it automatic. That takes one reading, stores it in the variable dist and that's it. It won't read the sensor again until it comes to another line that tells it to read the sensor. For the rest of your program until it gets to that line again dist will simply keep the same value it had the one time you read the sensor. It's not automatically updating. That isn't how code works.

so should i take it off and change to

// Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(ECHOPIN, HIGH); // Measure the time till the pulse is reflected back.
    // Calculating the distance
    dist= duration*0.034/2;
    // Prints the distance on the Serial Monitor
    Serial.print("Distance: ");
    Serial.println(distance); // new line after the distance has been printed.

No. You should add a line to read the sensor again at the point in the code where you want it to be read again.

Delta_G:
No. You should add a line to read the sensor again at the point in the code where you want it to be read again.

void loop() {
dist = sonar.ping_cm();
Serial.print(dist);
Serial.println(" cm");
delay(100);
static bool dist_reach = false;
if(dist <5) {
delay(1000);
Serial.println(“Checking Condition again”);
delay(10000);
if(dist<5){
Serial.println(“sent notifcation1 to Telegram”);
sendTelegramMessage();
delay(bulk_messages2_mtbs);
Bot_lasttime = 0;
dist_reach = true;
if(!dist_reach)
{
if(dist<5){
Serial.println(“sent notifcation1 to Telegram”);
sendTelegramMessage();
delay(bulk_messages2_mtbs);
Bot_lasttime = 0;
dist_reach = true;
}
}
}
else if (dist >55) {
if(dist_reach) {
Serial.println(“sent notifcation2 to Telegram”);
sendTelegramMessage2();
delay(3000);
Bot_lasttime = 0;
dist_reach = false;

}
}
}

like that ?

Like what? I don’t see what you changed.

this line:

dist = sonar.ping_cm();

gets a new value for dist.

If you expect the value of dist to change in here:

 if(dist <5) {
       delay(1000);
       Serial.println("Checking Condition again");
       delay(10000);
       if(dist<5){

then that line should appear there again.

Please read the “How to use this forum” thread and learn to post code correctly. You will find that thumbing your nose at the forum etiquette will tend to cause the help to dry up pretty fast. Have enough respect and courtesy for the people offering you free help to not make it harder on them than it has to be. Please use code tags when posting code. The “How to use this forum” thread will explain how.

Delta_G:
Please read the "How to use this forum" thread and learn to post code correctly. You will find that thumbing your nose at the forum etiquette will tend to cause the help to dry up pretty fast. Have enough respect and courtesy for the people offering you free help to not make it harder on them than it has to be. Please use code tags when posting code. The "How to use this forum" thread will explain how.

yeah . i am sorry .

Delta_G:
Like what? I don’t see what you changed.

this line:

dist = sonar.ping_cm();

gets a new value for dist.

If you expect the value of dist to change in here:

 if(dist <5) {

delay(1000);
      Serial.println(“Checking Condition again”);
      delay(10000);
      if(dist<5){




then that line should appear there again. 



Please read the "How to use this forum" thread and learn to post code correctly. You will find that thumbing your nose at the forum etiquette will tend to cause the help to dry up pretty fast. Have enough respect and courtesy for the people offering you free help to not make it harder on them than it has to be. Please use code tags when posting code. The "How to use this forum" thread will explain how.

thanks you very much the problem is been solve thanks .Really thanks!