entering a function only after a long press

I want to make something like a reset button - that when I press it for 3 seconds in a row it will go to a function

maybe like this :

void loop()
{
int time = millis();
int button=analogRead(0);
while ((button<20)&&(millis-time>3000))// the button will need to be connected to ground for 3 sec 
{
digitalWrite(13,HIGH)
Serial.println("the button was press at least 3 seconds." )
NewFunction();
delay(500)
digitalWrite(13,LOW)
}

}

is this a good way to do this ? or is there a better way?

Thanks ,

My DebouncedInput library would do it for you - just set the debounce time to 3 seconds.

david1234:
is this a good way to do this ? or is there a better way?

Declare a global (or static) variable as such:

unsigned long lastTimeSwitchWasNotPressed;

Update the value when you read that the switch is not pressed:

if (switchIsNotPressed)
{
  lastTimeSwitchWasNotPressed = millis();
}

In your loop function, without any other conditions, check to see if it has been three seconds.

if (millis() - lastTimeSwitchWasNotPressed >= 3000)
{
   // The switched has been pressed for 3 seconds
}

At this point you have to decide on whether you want to continually do this action while the button is pressed down, repeat every three seconds, or not repeat until the switch is unpressed again. No additional logic is needed to continually perform the action. To have it repeat every three seconds, simply reset our variable:

if (millis() - lastTimeSwitchWasNotPressed >= 3000)
{
   // The switched has been pressed for at least 3 seconds
  lastTimeSwitchWasNotPressed  = millis();
}

If you want to only run the action once while the switch is pressed, then simply add a latch:

unsigned long lastTimeSwitchWasNotPressed;
boolean actionPerformed = false;

...

if (millis() - lastTimeSwitchWasNotPressed >= 3000)
{
   // The switched has been pressed for at least 3 seconds
  if (!actionPerformed)
  {
    // do something once
    actionPerformed = true;
  }
  lastTimeSwitchWasNotPressed  = millis();
}

Then you can go back to your first if statement to reset the latch:

if (switchIsNotPressed)
{
  lastTimeSwitchWasNotPressed = millis();
  actionPerformed = false;
}

I think(know) I miss something ..
I can see it printing "not press" ,but when I do press the switch - it's stop printing but does nothing
why ?

unsigned long lastTimeSwitchWasNotPressed;
boolean actionPerformed = false;
int reset=4;
void setup ()
{
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  pinMode(reset,INPUT);
  delay(100);
  Serial.println("please wait while loading....");
  for (int i=0;i<5;i++)
  {
    digitalWrite(13, HIGH);
    Serial.print("**---");
    delay(1000);
  }
  digitalWrite(13,LOW);
  Serial.println(".");

}

void loop()
{

  if (digitalRead(reset)==LOW )
  {//The switched hasn't been pressed 
    lastTimeSwitchWasNotPressed = millis();
    actionPerformed = false;
    Serial.println("Not press");
  }
 
else 
{
  if (millis() - lastTimeSwitchWasNotPressed >= 3000)
  {
    // The switched has been pressed for at least 3 seconds
    Serial.println("Press");
    TestFunction();
    actionPerformed = true;
  }
  lastTimeSwitchWasNotPressed  = millis();
}

}

void TestFunction()
{
  for (int i=0;i<5;i++)
  {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }

  Serial.println("Done.....");

}

never mind found my problem

Thanks for the help

this is the code

unsigned long lastTimeSwitchWasNotPressed;
boolean actionPerformed = false;
int reset=4;
void setup ()
{
  Serial.begin(9600);
  pinMode(13,OUTPUT);
  pinMode(reset,INPUT);
  delay(100);
  Serial.println("please wait while loading....");
  for (int i=0;i<5;i++)
  {
    digitalWrite(13, HIGH);
    Serial.print("**---");
    delay(1000);
  }
  digitalWrite(13,LOW);
  Serial.println(".");

}

void loop()
{

  if (digitalRead(reset)==LOW )
  {//The switched hasn't been pressed 
    lastTimeSwitchWasNotPressed = millis();
    actionPerformed = false;
    Serial.print("Not press");
    int t=digitalRead(reset);
    Serial.println(t);
  }
 
else 
{
    if (millis() - lastTimeSwitchWasNotPressed >= 2500)
  {
    // The switched has been pressed for at least 3 seconds
    Serial.println("Press");
    TestFunction();
    actionPerformed = true;
  }
  else
  {
  Serial.print("Not yet  - ");
  Serial.println(millis() - lastTimeSwitchWasNotPressed);
  }
}

}

void TestFunction()
{
  for (int i=0;i<5;i++)
  {
    digitalWrite(13,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }

  Serial.println("Done.....");


}