Adding Time Duration to Sensor Trigger

Hi all,

I need help as I think I'm missing some fundamental information regarding the use of millis.

We have a simple set up using a JSN-SR04T sensor that triggers an LED when someone is within a distance range.

However, as this is outside, the readings quite often reads anomalies and triggers the LED. What I'm looking to do is add code to only trigger the LED if someone is within that range for 1 sec.

I am a complete beginner so go easy, here is my code so far:


// defines pins numbers
const int trigPin = 6;
const int echoPin = 5;
const int ledPin = 9;

// defines variables
long duration;
int distance;
int triggerDistance;


void setup()  {
  pinMode(trigPin, OUTPUT); //Sets the trigPin as an Output
  pinMode(echoPin, INPUT); //Sets the echoPin as an Input
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600); // Starts the serial communication
}


void loop() {
  // CLears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  //Sets the trigPin on HIGH state for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(30);
  digitalWrite(trigPin, LOW);

  // Reads rhe echoPin, returns the sound wave travek time in microseconds
  duration = pulseIn(echoPin, HIGH);

  //Calculating the distance
  distance = duration*0.034/2;

  triggerDistance = distance;
  if (triggerDistance >= 60 && triggerDistance <= 80) {
    digitalWrite(ledPin, HIGH);
    delay(5000);
  }
  else{
    digitalWrite(ledPin, LOW);
  }
  //Prints the distance
  Serial.print("distance: ");
  Serial.println(distance);
}

Thanks in advance

Have you looked at the BlinkWithoutDelay example?

This code currently lights the LED as soon as someone is in range. Instead you need to start a timer and then check if has been longer than 1 seconds before turning on the LED.

Should be close:

#define ENABLED             true
#define DISABLED            false

#define LEDon               HIGH
#define LEDoff              LOW

// defines pins numbers
const byte trigPin        = 6;
const byte echoPin        = 5;
const byte ledPin         = 9;
const byte heartbeatLED   = 13;

long duration;
int distance;
int triggerDistance;

boolean timingFlag        = DISABLED;
boolean LEDflag           = DISABLED;

//*********************************
//timing stuff
unsigned long heartbeatMillis;
unsigned long checkDistancMillis;
unsigned long timingMillis;
unsigned long LEDonMillis;

const unsigned long delayInterval = 1000ul;
const unsigned long LEDonInterval = 5000ul;


//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(heartbeatLED, OUTPUT);
  pinMode(ledPin, OUTPUT);

  pinMode(trigPin, OUTPUT); //Sets the trigPin as an Output
  pinMode(echoPin, INPUT);  //Sets the echoPin as an Input

} //END of   setup()


//********************************************^************************************************
void loop()
{
  //*********************************                              heartbeat TIMER
  //is it time to toggle the heartbeatLED ?
  if (millis() - heartbeatMillis >= 500)
  {
    //restart this TIMER
    heartbeatMillis = millis();

    //toggle the heartbeatLED
    digitalWrite(heartbeatLED, !digitalRead(heartbeatLED));
  }

  //*********************************                              Check for object TIMER
  //is it time to check for objects (i.e. every 500ms) ?
  if (millis() - checkDistancMillis >= 500)
  {
    //restart this TIMER
    checkDistancMillis = millis();

    checkForObjects();
  }

  //*********************************                              validated TIMER
  //if enabled has this TIMER expired ?
  if (LEDflag == DISABLED && timingFlag == ENABLED && millis() - timingMillis >= delayInterval)
  {
    //enable the LED ON TIMER
    LEDflag = ENABLED;

    //restart the TIMER
    LEDonMillis = millis();

    digitalWrite(ledPin, LEDon);

    Serial.println("LED ON  <---------<<<<<< ");
  }

  //*********************************                              LED ON TIMER
  //if enabled has this TIMER expired ?
  if (LEDflag == ENABLED && millis() - LEDonMillis >= LEDonInterval)
  {
    LEDflag = DISABLED;

    timingFlag = DISABLED;

    digitalWrite(ledPin, LEDoff);

    Serial.println("LED OFF <---------<<<<<< ");
  }

} //END of   loop()


//********************************************^************************************************
void checkForObjects()
{
  //*********************************
  //Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

  //Sets the trigPin on HIGH state for 10 microseconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(30);
  digitalWrite(trigPin, LOW);

  //*********************************
  //Reads rhe echoPin, returns the sound wave travek time in microseconds
  duration = pulseIn(echoPin, HIGH);

  //Calculating the distance
  distance = duration * 0.034 / 2;

  triggerDistance = distance;
  Serial.print("distance: ");
  Serial.println(distance);

  //*********************************
  //are we within the target range ?
  if (triggerDistance >= 60 && triggerDistance <= 80)
  {
    //are we currently timing
    if (timingFlag == DISABLED)
    {
      //enabled the timing
      timingFlag = ENABLED;

      //restart the TIMER
      timingMillis = millis();
    }
  }

  else
  {
    //disable the timing
    timingFlag = DISABLED;
  }

} //END of   checkForObjects()

This feature is easier to code if you rephrase it, "if the last time someone was not in range, is greater than 1 second ago".

Then it's a simple time stamp record and and check.