Trying to run an if statement result for a specific amount of time

Hello,

I am working on a sensor that activates a light when a certain force threshold is met, but will stop and emit a second light if the applied force drops below the threshold within 10 seconds. I'm a beginner and tried using if statements, so maybe I should use a for loop instead...

Does anyone have any advice on how to tweak this code?

Here it is:

// Pin layout, leds, fsr
const byte ledPin1 = 10;
const byte ledPin2 = 13;
const byte fsrPin = A0;

const byte starttime = millis();
byte endtime = starttime;
byte loopcount = 0;

int fsrthreshold = 150;  // adjust threshold for your setup

void setup()
{
   Serial.begin(115200);
   pinMode(LED_BUILTIN, OUTPUT);
}

void loop()
{
  int fsrValue = readfsr();
  if( fsrValue > fsrthreshold) // force over threshold, fade
  {
   fadeLed2(fsrValue / 4); // 1023 adc to 255 pwm
  }
  else if (0 < fsrValue < fsrthreshold) // Force drops beneath the necessary level
  {
    fadeLed1(fsrValue / 4);
    while ((endtime - starttime) <=1000) // do this loop for up to 1000mS
    {
    // code here
    loopcount = loopcount+1;
    endtime = millis();
    }
  }
  else if( fsrValue = 0) // no force detected, blink
  {
   digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
   delay(500);                       // wait for a second
   digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
   delay(500);
  }
}

int readfsr()
{
   return(analogRead(fsrPin));
}



void fadeLed1(byte fadeValue)
{
   analogWrite(ledPin1, fadeValue);
}

void fadeLed2(byte fadeValue)
{
   analogWrite(ledPin2, fadeValue);
}

have you considered a do while loop for the 10 sec time with a break for the force threshold?

Armadillo1:
I'm a beginner and tried using if statements, so maybe I should use a for loop instead...

I recommend instead that you study these two posts:

Several things at the same time

Using millis() for timing beginner’s guide

Then you can get rid of the two 500ms delays (during which time the processor does nothing useful).

Some problems with your code:

const byte starttime = millis();
byte endtime = starttime;

Times are unsigned long, not byte and I think you don't want starttime to be a constant, you want to keep resetting it.

else if (0 < fsrValue < fsrthreshold) // Force drops beneath the necessary level

should be:

else if ((fsrValue > 0) && (fsrValue < fsrthreshold) ) // Force drops beneath the necessary level

Based on your description it seems like this line:

while ((endtime - starttime) <=1000) // do this loop for up to 1000mS

is meant to check for 10 seconds, remember that 1000 ms = 1 second.

Don't have two functions that do the same thing on different pins, pass the pin number as a parameter.

void fadeLed(byte ledPin, byte fadeValue)
{
   analogWrite(ledPin, fadeValue);
}

Could you describe again what you want to happen?

If the reading is over the threshold does the LED brightness keep getting set, or is it set once and stays at the same brightness?

If it goes under the threshold within ten seconds I get that the brightness of the other LED is set, but does the first one turn off?

Suppose the force goes over the threshold and stays there for 20 seconds, the second LED will never change even if the force goes under the threshold since it's been longer than 10 seconds. Does there need to be a reset at some point?