Using millis for a conditional timer

i am working on a project that will use a Sharp distance sensor to detect if an object is there/not there.

Something like this needs to be programmed for it.

If object is detected (analog input 0 = XXX) for XXX seconds write to serial "Object Present" and hold that state until the object is removed for XXX seconds them write to serial "Object Removed" and hold that state until the object is then put back for the same XXX seconds.

I have done very little programming and any help on this would be very appreciated.

Thank you!

This is what I have so far:

const int irSense = A0; // Connect sensor to analog pin A0
int distance = 0;

void setup() {
Serial.begin(9600); // Use Serial Monitor window
}

void loop() {
distance = analogRead(irSense); // Read the sensor
Serial.println(distance, DEC); // Display value in Serial Monitor window
delay(1000); // Wait 1/4 second for the next read

if (distance >= 1000) Serial.println(“Present”);
if (distance <= 600) Serial.println(“NotPresent”);
}

But this prints to serial as soon as the analog signal is >= 1000 and as soon as the signal is <= 600

I need to monitor the analog signal. If the signal is >= 1000 for XXX seconds then serial print.
If analog signal is <= 400 for XXX seconds then serial print “Not Present”

Don’t use the delay() function as it stops sketch execution for that amount of time.

Have you learned how to use millis() as in the BWD technique?

Start a timer when a condition is met.
When the timer expires, do what you have to do.

.

I'm not very good with C programming.

I can get the blink without delay working but cannot seem to incorporate that into my original code.

I guess im not putting the "if" and "else" in the proper places or using {}, () properly.

“I can get the blink without delay working but cannot seem to incorporate that into my original code.”

Show us your attempt.

To check if something has been in a desired condition for a certain length of time you need to reset the timer every time it is NOT in that desired condition - which may seem counter-intuitive. Like this pseudo code

if (condition is wrong) {
   lastWrongMillis = millis();
}

if (millis() - lastWrongMillis >= desiredInterval) {
   condition has been correct throughout the interval
}

...R

As Robin2 stated.

Lower limit code left for you to do:

//**********************************************************************
//
// millis() returns the current run time (in ms) since the last restart.
//
//**********************************************************************
//
unsigned long blockingMillis;                //The time when this Timer was (re)started
const unsigned long blockingInterval = 500;  //1/2 second

unsigned long analogMillis;                  //The time when this Timer was (re)started
const unsigned long analogInterval   = 250;  //1/4 second

const unsigned int XXX               = 1000; //1 second(s)

unsigned long PresentMillis;                 //The time when this Timer was (re)started
const unsigned long PresentInterval  = XXX;

//**********************************************************************

//LED wiring: +5V---anode-[>|]-cathode---[ 220R ]----OutputPin
const byte blockingLED = 13;

const int upperLimit   = 1000;
const int irSense      = A0;     // Connect sensor to analog pin A0
unsigned int distance  = 0;

//                            s e t u p ( )
//======================================================================
void setup()
{
  Serial.begin(9600);

  pinMode(blockingLED, OUTPUT);

} //END of:                   s e t u p ( )


//                             l o o p ( )
//======================================================================
void loop()
{
  //******************************************
  //Some code to check for blocking code, LED should flash at 1 Hertz rate.
  if (CheckTimer(blockingMillis, blockingInterval))
  {
    //Toggle the Blocking LED
    digitalWrite(blockingLED, !digitalRead(blockingLED));
  }

  //******************************************
  //Is it time to check the analogs?
  if (CheckTimer(analogMillis, analogInterval))
  {
    //it is now time to read the analog
    distance = analogRead(irSense);  // Read the sensor
    Serial.println(distance, DEC);   // Display value in Serial Monitor window
  }

  //******************************************
  //upper limit code
  if (distance < upperLimit)
  {
    PresentMillis = millis(); //re-initalize timing
  }

  if (CheckTimer(PresentMillis, PresentInterval))
  {
    Serial.println("Present");
  }

  //******************************************
  //lower limit code

  //put code here              <----<<<<

  //******************************************


  //******************************************
  //Put other non-blocking stuff here
  //******************************************

} //END of:                    l o o p ( )


//======================================================================
//                          F U N C T I O N S
//======================================================================


//                        C h e c k T i m e r ( )
//======================================================================
// Used when actions are needed 'after' a period of time.
boolean CheckTimer(unsigned long &lastMillis, const unsigned long Interval)
{
  //lastMillis = the time this "timer" was (re)started
  //Interval   = interval/delay (mS) we are looking for.

  //Is this timer enabled and has it expired?
  if (millis() - lastMillis >= Interval)
  {
    lastMillis = millis();
    //or use
    //lastMillis += Interval;    //get ready for the next iteration

    //This Timer did expired
    return true;
  }

  //This timer did not expire
  return false;

} //END of:               C h e c k T i m e r ( )

//======================================================================
//                            END OF CODE
//======================================================================

You guys are so awesome! Thank you both so much! :slight_smile: :slight_smile: :slight_smile: