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);
}