FSR Sensor to keep pressure in 7 seconds?

Hello,

I have a FSR sensor, where I need to ensure that it has a pressing value of at least 250 in 7 seconds. The values are not important, but needs to be above 250 in 7 seconds in order to run an other function.

The only thing I got so far is this.

FSRreading = AnalogRead(FSR);

if(FSRreading>250) {

other_function();


}

So this only checks once and execute the other_function. How can I ensure that it has been pressed for 7 seconds?

Thank you in advance :smiley:

Record the time the reading first exceeds the threshold.
Use millis().

Take it from there.

I've found that the best way to code "Has condition been true for at least 'time'?" is to restart a millis() timer every time the condition is false. Then if the timer reaches 'time' the condition has been true that long.

const byte FSRPin = A0;
const unsigned long SevenSeconds = 7000;

void setup() {}

unsigned long TimerStart;
void loop()
{
  int reading = analogRead(FSRPin);

  if (reading <= 250)
    TimerStart = millis();

  if (millis() - TimerStart >= SevenSeconds)
  {
    // reading has been above 250 for 7 seconds or more
    do_the_thing();

    // Don't do it again unless the reading is high for
    // ANOTHER 7 seconds
    TimerStart = millis();
  }
}

void do_the_thing() {}