Problem timing duration of event

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.

You don't say where you added the extra code but

while(abs(dF)< 10); // wait while flex is not moving
                   {
                   duration = millis() - startTime;
                   Serial.print("duration: ");
                   Serial.println(duration);
                   }

Nothing in the while loop changes the value of dF so how can the while loop ever terminate ?
Mind you, it never actually starts because the semi-colon on the end of the while statement causes the loop to terminate immediately.

int a=450;
int b=280;
int ledBlue= 13;
int ledRed = 12;
int vibMotor = 3;

unsigned long lastMillis = 0; 



void setup() {
 
  Serial.begin(9600);
  pinMode(vibMotor,HIGH);
}


void loop() {
  
int sensorValue1 = analogRead(A0); //(33)
int sensorValue2 = analogRead(A1); //(28)
int sensorValue3 = analogRead(A2); // sensor 2inch
  
  
  Serial.print(sensorValue1);
  Serial.print("   ");
  Serial.print(sensorValue2);
  Serial.print("   ");
  Serial.println(sensorValue3);
  delay(10);      
  
  if (sensorValue1 <= a && sensorValue2 <=a && sensorValue3 <= b)
  {digitalWrite(ledBlue,HIGH);
    digitalWrite(ledRed,LOW);
    digitalWrite(vibMotor,LOW);
  }
  
  else
  {
    
    digitalWrite(ledBlue,LOW);
    digitalWrite(ledRed,HIGH);
     
    while(millis()- lastMillis >= 15000)  
    {
     digitalWrite(vibMotor,HIGH) ;
     lastMillis = millis();
    }
    
  
  }
  
}

hi , currently im doing abit similar project . if all the sensor reach certain value it suppose to wait for 15 second to trigger the motor and stop if met if condition and continue the loop. the problem im facing is the timer does not count to 15 second for the other loop if the timer sent back to if. how to make it always count for 15 second when met with else.

the problem im facing is the timer does not count to 15 second for the other loop if the timer sent back to if. how to make it always count for 15 second when met with else.

Feelfreetopostsomecodeandusesomepunctuation.

PaulS:
Feelfreetopostsomecodeandusesomepunctuation.

Paul, did you miss the code in the new, improved, scrollable window in post #2 ?

Paul, did you miss the code in the new, improved, scrollable window in post #2 ?

Guilty as charged, your honor.

I still don't understand the problem statement. OP needs to take a deep breath and blow out all his/her frustrations, and then attempt, in a logical fashion (which involves using punctuation) exactly what the code does, what he/she wants, and how the two differ.

 while(millis()- lastMillis >= 15000)  
    {
     digitalWrite(vibMotor,HIGH) ;
     lastMillis = millis();
    }

Hmm.

int a=450;  // sensor threshold (value when flex sensor bend)
int b=280;   // sensor threshold
int ledBlue= 13;
int ledRed = 12;
int vibMotor = 3;

unsigned long lastMillis = 0; 



void setup() {
 
  Serial.begin(9600);
  pinMode(vibMotor,OUTPUT);
}


void loop() {
  
int sensorValue1 = analogRead(A0); //(33)
int sensorValue2 = analogRead(A1); //(28)
int sensorValue3 = analogRead(A2); // sensor 2inch
  
  
  Serial.print(sensorValue1);
  Serial.print("   ");
  Serial.print(sensorValue2);
  Serial.print("   ");
  Serial.println(sensorValue3);
  delay(10);      
  
  // if the sensor value less than the threshold  
  // blue led turns on 

  if (sensorValue1 <= a && sensorValue2 <=a && sensorValue3 <= b)
  {digitalWrite(ledBlue,HIGH);
    digitalWrite(ledRed,LOW);
    digitalWrite(vibMotor,LOW);
 
    
  }
  
  else // when flex sensor not bended
  
 
  {
    // red led turns on 

    digitalWrite(ledBlue,LOW);
    digitalWrite(ledRed,HIGH);
    
    
    //wait untill 15 sencond then turns on vibrator motor

    while(millis()- lastMillis >= 15000)  
    {
     digitalWrite(vibMotor,HIGH) ;
     lastMillis = millis();
     
    }
    
  
  }
  
}

When the flex sensor not bended , the timer start to count to 15 before turning on the motor.

so if the timer started , than the sensor is bend and unbend again , the timer count from the previous

time where it left not from the start. so for the next loop it will not be 15 second.

sorry for my english.

When will your while loop actually end? That problem was pointed out in reply #6.

Why are you re-inventing delay()? It is NOT necessary to keep setting the pin HIGH. It's not stupid. It can remember that it was set to HIGH.

sorry i still dont understand what you mean by

When will your while loop actually end?

and

setting the pin HIGH

. im not trying to invent delay . i want to make a timer that will reset when the condition changes.

i changed my coding to this but still not working

   do 
    {
     
      digitalWrite(vibMotor,HIGH) ;
     lastMillis = millis();
     
    }while(millis()- lastMillis >= 15000);

i changed my coding to this but still not working

Well, of course not. Let's look at why.

   do 
    {
     
      digitalWrite(vibMotor,HIGH) ;
     lastMillis = millis();
     
    }while(millis()- lastMillis >= 15000);

First, you don't need to keep setting the pin high, so do that first. Then put the do/while loop.

    digitalWrite(vibMotor,HIGH) ;
    do 
    {
       lastMillis = millis();
    }while(millis()- lastMillis >= 15000);

Now, lets suppose that millis() wold have returned 2704 before the do statement, and that lastMillis is 0. In the body of the while loop, you set lastMillis to 2704, and compare 2704 (or maybe it's ticked over to 2705) minus 2704 to 15000. 0 (or 1) is NOT greater than or equal to 15000, so the loop repeats.

When, following what you have there, will the while loop ever end? On EVERY pass through the loop, you GUARANTEE that the while clause will compare a value of 1 or less to 15000. Hell will freeze over before 1 gets to equal to 15000.

Now, lets imagine that you pull your head out of your ass, and move the lastMillis assignment statement out of the loop, so your code looks like this:

    digitalWrite(vibMotor,HIGH) ;
    lastMillis = millis();
    do 
    {
    }while(millis()- lastMillis >= 15000);

Now, the while loop will block for 15 seconds. That is EXACTLY what delay() is doing. So, you HAVE reinvented delay(). Congratulations.

Why?

The difference between a while loop and a do..while is that a while loop may not execute the body of the loop at all if the condition is not met, but a do..while will ALWAYS execute the body of the loop at least once.

sorry , but how to turn on the motor without setting the pin high ?

PaulS is suggesting turning it on once, not every time through the loop. Look, do you turn your lights on a thousand times a second? Or just once, and they stay on?

 else // when flex sensor not bended
  {
    x++;
    if(x<2)
    {
      lastMillis=millis();
    }
    // red led turns on 
    digitalWrite(ledBlue,LOW);
    digitalWrite(ledRed,HIGH);
    interval = millis()-lastMillis;
    if(interval>10000)
    {
      digitalWrite(vibMotor,HIGH);
      x=0;
    }
  }

i got the result that i wanted using an increment..