Quick help with millis()

Ok. But in this case i assume it will quit no matter to situation(canno checkit... work time) Meaning that it will only give me 10 seconds to do things. the score is to reset that clock everytime human input is detected otherwise quit.

So are you trying to say that if the user presses the button AND holds it for 3 seconds THEN activeTouch loops for 10 seconds HOWEVER if the user presses the button within those 10 seconds, it resets back to 10 seconds?

So basically during activeTouch, as long as the button is not being pressed for 10 seconds, it will exit the function?

yup... otherwise if you have to do multitask( we not saying this is complete sketch) than 10 seconds will not be enough to complete the task. ex. say you have 10 sensor that you have to preset,you will only have a 1sec each.

Regarding last post you entered

if(isButtonHeld && millis() - switchOn > holdTime)

i was going in the right direction but i coudlnt compile as i was doing that

if(digitalRead(button1 == LOW && millis() - switchOn > holdTime)

Not so sure if i put the brackets in the right place now, but is there any reason why i wont compile??

Your missing a right parenthesis after button1

ok im writhing it from head now. ill check it when i get back home, which is another 10h fixing trains.

in the evening im going to try implement the while loop to exit, as its looping without ending.

Already ahead of you. Read the comment in the code

void activeTouch()
{
  boolean firstRun = false;
  boolean deactivated = false;
  unsigned long activeTime = 0;

  while(!deactivated)
  {
    digitalWrite(led1, !digitalRead(button1));
    digitalWrite(led2, !digitalRead(button2));
    if(!firstRun)
      activeTime = millis();
    firstRun = true;
    if(millis() - activeTime > deactivateTime)
      deactivate = true; //if you just want to exit the function, you can use "return;" or if you want to exit the loop, you can use "break;"
    Serial.println("active");
    delay(100); //add a little delay
  }
}

/if you just want to exit the function, you can use "return;" or if you want to exit the loop, you can use "break;"

so if i get it right, i can instead

 if(millis() - activeTime > deactivateTime)
      deactivate = true;

do

if(millis() - activeTime > deactivateTime)
      break;

?

That will exit the while loop, and because you have nothing after that while loop, it will essentially exit the function

you should actually do this instead, its just a little more efficient

void activeTouch()
{
  unsigned long activeTime = millis(); //this is when the function was called

  while(true)
  {
    digitalWrite(led1, !digitalRead(button1));
    digitalWrite(led2, !digitalRead(button2));
    if(millis() - activeTime > deactivateTime)
      break;
    Serial.println("active");
    delay(100); //add a little delay
  }
//break will take you here!
}

//break will take you here!

i`m confused

return; will exit while() and go back to void loop() or break; will exit while and go back to void loop()??

void loop()
{
  foo();
  //return; will take you here NO MATTER WHERE it was called in foo()
}

void foo()
{
  while(1)
  {
    break;
  }
  //break will take you here
  
  while(1)
  {
    return;
  }
  Serial.print("This will never print because return; was called and will exit the entire function");
}

Aaa :) i like that

thanks a lot for explanation.

im still could not sort that out, im trying different bool flags etc and still don`t get much result on that

void activeTouch()
{
  unsigned long activeTime = millis(); //this is when the function was called

  while(true)
  {
    digitalWrite(led1, !digitalRead(button1));

////////////////i pressed a button and i want activeTime to reset countdown
    
      if( millis() - activeTime > deactivateTime) // i want this to interact only if no human input
   
      return;
    Serial.println("active");
    delay(100); //add a little delay
  }

}

return gets you immediately out of any function, to the calling function. break gets you out of the innermost loop, whether it be a for loop a while loop or a do..while loop.

Sorry this one goes into the loop and stays forever no matter of the input.

void activeTouch()
{
  unsigned long activeTime = 0; //this is when the function was called

  while(true)
  {
    digitalWrite(led1, !digitalRead(button1));
    {
      activeTime = millis();
    }
    
    
////////////////i pressed a button and i want activeTime to reset countdown
    
      if( millis() - activeTime > deactivateTime) // i want this to interact only if no human input
   
      return;
    Serial.println("active");
    delay(100); //add a little delay
  }
void activeTouch()
{
  unsigned long activeTime = millis(); //this is when the function was called

boolean pressed = false;

  while(true)
  {
    digitalWrite(led1, !digitalRead(button1));
    {
      pressed = true;
    }
    
    
////////////////i pressed a button and i want activeTime to reset countdown
    
      if(pressed == false && millis() - activeTime > deactivateTime) // i want this to interact only if no human input
   
      return;
    Serial.println("active");
    delay(100); //add a little delay
  }

}

and this one i`ve tried with boolean. also goes around.without stopping

digitalWrite(led1, !digitalRead(button1));
    {
      activeTime = millis();
    }

Is something missing there? I don't see the point of the braces.

Probably if i know it will be there already :)

if(pressed == false && millis() - activeTime > deactivateTime)

pressed is always true, so the rest of the conditional will never be evaluated.

ive tried to invert flags but no result. ill comeback to that tomorrow with a bit more fresher brain. Thanks a lot for your time.