button press

Hi guy's..

Can someone help me on a dead man switch concept..

If I press a button it must wait 5 seconds before a led goes from low to high.
If I stop pressing the button it must go back to low immediately.

I have tried the delay function, it works fine for going from low to high, but if I leave the button it also waits to go back to low...

I know there must be a simple solution to the problem..

Please help

The begiiner's guide to millis() is a place to start to learn about non-blocking timing. Another tutorial that will help is the state change detection tutorial. That will show how to detect when the button becomes pressed or released (as opposed to when the button is pressed or released). In other words, you look for the transition, not the level. So, when the button becomes pressed, save the time (millis()) in a variable and, in loop(), check if the button is still pressed and the difference between the current millis() and the recorded millis(). If the button is still pressed after 5000 milliseconds turn on the LED.

This sort of code will detect if something is in a particular state throughout a period

buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {          // assumes LOW when pressed
   lastTimeButtonWasNotPressed = millis();
}

if (millis() - lastTimeButtonWasNotPressed >= interval) {
   // do stuff
}

...R

Another take on the idea:

const byte pinLED = LED_BUILTIN;
const byte pinDeadmanSw = 2;
byte lastSw;
bool bState;

void setup( void )
{
    pinMode( pinLED, OUTPUT );
    pinMode( pinDeadmanSw, INPUT_PULLUP );
    lastSw = digitalRead( pinDeadmanSw );

    bState = false;
    
}//setup

void loop( void )
{
    static unsigned long
        timeSw;
    byte
        currSw;
        
    currSw = digitalRead( pinDeadmanSw );
    if( currSw != lastSw )
    {   
        lastSw = currSw;
        if( currSw == HIGH )
        {
            //button was released; turn LED off
            //bState set to false in case system was timing a press
            bState = false;
            digitalWrite( pinLED, LOW );
            
        }//if
        else
        {
            //bState true tells logic below we're timing the LED
            bState = true;
            timeSw = millis();
            
        }//else
        
    }//if

    //if timing the LED...
    if( bState )
    {
        //if 5-sec has elapsed...
        if( millis() - timeSw >= 5000 )
        {
            //stop timing the LED and turn it on
            bState = false;
            digitalWrite( pinLED, HIGH );
            
        }//if
        
    }//if
    
}//loop

thanks for all the help.. I need to test this and see if it will work.. it's a different way, to how I saw the solution..

I think it will work..

Again .. Thanks..

I know it might be a small solution, but if I don't ask, I will never know..

...

Robin2:
This sort of code will detect if something is in a particular state throughout a period

buttonState = digitalRead(buttonPin);

if (buttonState == HIGH) {          // assumes LOW when pressed
  lastTimeButtonWasNotPressed = millis();
}

if (millis() - lastTimeButtonWasNotPressed >= interval) {
  // do stuff
}




...R

That's clever, continually resetting the clock while the thing you want to time isn't happening.

this last code returns an error..

The buttons I am using is "capacitive touch" buttons. I need to make sure that someone doesn't press a button by mistake, so the solution is that they need to press it for at least 3 seconds before it respond. But if the button is "unpressed" the servo or LED must go to a previous state. and there are 16 buttons on the panel.

Is there any way not to repeat the code over and over for every button??

Mighty-Eagle:
this last code returns an error..

Which? Blackfin's? It compiles for me. (Robin2's isn't a sketch, it's a snippet to show a principle.)

What error?

I didn't say its wrong.. in my sketch there is an error.. don't know how to solve it yet.. Sorry.. I did not mean to be rude... I'm new at this... relax please...

Mighty-Eagle:
Sorry.. I did not mean to be rude...

I don't think that anyone thought you were rude - just unclear.

If you want help with your error then post the latest version of your program and also post the error message.

...R