Greetings everyone!
How can I code a if-else condition with time period? I want my ultrasonic sensor to do something when a condition met in 5 seconds. Any help from you guys would be much apprreciated.
void CheckSensorTrash() {Â Â // Method for the Ultrasonic sensor in front to detect Obstacle
 digitalWrite(trig_trash, LOW);
 delayMicroseconds(5);
 digitalWrite(trig_trash, HIGH);
 delayMicroseconds(10);
 digitalWrite(trig_trash, LOW);
 duration = pulseIn(echo_trash, HIGH);
 SensorTrash = (duration/2) * 0.034;
 if (SensorTrash <= 3 && SensorTrash > 0){ // for 5 seconds it should do the statement
 //do something
 }
void CheckSensorTrash()
{
  static unsigned long
    timerTrashStart;
  static bool
    flagThingDone = false;
    flagTrashFirst = false;
       Â
  // Method for the Ultrasonic sensor in front to detect Obstacle
  digitalWrite(trig_trash, LOW);
  delayMicroseconds(5);
  digitalWrite(trig_trash, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_trash, LOW);
  duration = pulseIn(echo_trash, HIGH);
  SensorTrash = (duration/2) * 0.034;
  if (SensorTrash <= 3 && SensorTrash > 0)
  {
    if( !flagThingDone )
    { Â
      if( !flagTrashFirst )
      {
        timerTrashStart = millis();
        flagTrashFirst = true;
       Â
      }//if
      else
      {
        if( (millis() - timerTrashStart) > 5000 )
        {
          //do thing
 Â
          flagThingDone = true;
         Â
        }//if
       Â
      }//else
    }//if
  }//if
  else if( SensorTrash > 5 )
  {
    flagThingDone = false;
    flagTrashFirst = true;
   Â
  }//elif
 Â
}//CheckSensorTrash