Hello
I am working on a project that includes a flex sensor. The goal of the code that I have written is to recognize when the signal from the flex sensor is not changing. I would like to start a timer when the change in flex value(dF) is <10 and then perform an action when 3000ms has elapsed while the dF value is maintained as <10. If the dF value becomes >10 I would like the timer to stop and have no action is performed.
here is the start of my code, this works fine for me
float flex = 0;
float Lastflex = 0;
float rate = 0;
float dF = 0;
unsigned long lastTime = 0;
unsigned long dt = 100; // dt in milliseconds
long startTime;//value recorded from millis when dF<5
long duration;//variable to store the duration
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (millis() - lastTime >= dt) // wait for dt milliseconds
{
lastTime = millis();
int FlexValue = analogRead(A0);
rate = (FlexValue-Lastflex);
dF = 100*rate/dt;
Lastflex = FlexValue;
Serial.print("FlexValue: ");
Serial.println(FlexValue);
Serial.print("Lastflex: ");
Serial.println(Lastflex, 4);
Serial.print("dF: ");
Serial.println(dF, 4);
Serial.println();
}
}
If I then add this section to the code I have a problem and the serial monitor does not print any values
if(abs(dF)<10)
{
// here if the switch is pressed
startTime = millis();
while(abs(dF)< 10); // wait while flex is not moving
{
duration = millis() - startTime;
Serial.print("duration: ");
Serial.println(duration);
}
}
Any help on how to recognize when the dF value is static would be greatly appreciated.