delay

hi there i have this bit of code in one of my sketchs to tell me when the voltage go's to high but the problem is it pick up every spike, so how can i add a delay something like 2s ?

    int FalSafeValue = analogRead(FalSafeInput);
    if (FalSafeValue > 360) {  
      if (count1 < 1) {
        mydisp.drawStr(0, 4, "  OVER VOLTAGE  ");
        digitalWrite(alarmLED,           HIGH);
        count1++;  // add one (1) to our count
        if(count2 == 1) count2 =0;       // resetting the count 2
      }
    } 
    else {
      if (count2 < 1) {
        mydisp.drawStr(0, 4, "                ");
        count2++;
        if(count1 == 1) count1 =0;       // resetting the count 1
      }
    }

thanks Joe

A better different solution rather than delay, timeout, interval, etc., is to use hysteresis.

For example:

Make the alarmLED go HIGH when FalSafeValue > 360
Make the alarmLED go LOW when FalSafeValue < 300

If @dlloyd's suggestion is not suitable and you really only want to sample the value at intervals look at how millis() is used to manage timing in the demo several things at a time

Don't use the delay() function as it will get in the way of other things.

...R

This is a snippet from my sketch measuring the current on an USB output. It is not difficult to guess the meaning of the variables and constants

  if (USBcurrent <= CURRENT_USBmax) {
    millisLastUSBcurrentOK = millisNow;
  } // overcurrent must be present for a certain amount of time (WAIT_confirmOvercurrentUSB) before an error is reported
  else if (millisNow - millisLastUSBcurrentOK > WAIT_confirmOvercurrentUSB) {
    flagUSBoverload = true; // this condition is remembered until sleep
  }