Skip to main content
added 75 characters in body
Source Link
ratchet freak
  • 3.3k
  • 1
  • 13
  • 13

Don't act immediately when a button gets pressed, instead when a button is pushed all you do is set the timestamp on that button and set a flag.

Then when it is released check that timestamp against millis() andor the time elapsed is large enough do the appropriate thing.

void loop(){
    int btn0Statebtn1State = digitalRead(btn0btn1);
    static bool button1 = false;
    static unsigned long time1 = 0;
    
    if (btn0State == LOW && !button1) {
        button1 = true;
        time1 = millis();
    }
    
    if (btn0State == HIGHLOW && button1) {
        button1 = false;
        if(millis() - time1  <> SHORT){
            //do shortlong action
            time1 = millis(); //reset timer
        } 
 else{   }
     
    if (btn0State == HIGH //do&& longbutton1) action{
        }button1 = false;
        //do short action
    }

}

Don't act immediately when a button gets pressed, instead when a button is pushed all you do is set the timestamp on that button and set a flag.

Then when it is released check that timestamp against millis() and do the appropriate thing.

void loop(){
    int btn0State = digitalRead(btn0);
    static bool button1 = false;
    static unsigned long time1 = 0;
    
    if (btn0State == LOW && !button1) {
        button1 = true;
        time1 = millis();
    }
    
    if (btn0State == HIGH && button1) {
        button1 = false;
        if(millis() - time1  < SHORT){
            //do short action
        } else{
            //do long action
        }
    }

}

Don't act immediately when a button gets pressed, instead when a button is pushed all you do is set the timestamp on that button and set a flag.

Then when it is released or the time elapsed is large enough do the appropriate thing.

void loop(){
    int btn1State = digitalRead(btn1);
    static bool button1 = false;
    static unsigned long time1 = 0;
    
    if (btn0State == LOW && !button1) {
        button1 = true;
        time1 = millis();
    }

    if (btn0State == LOW && button1) {
        if(millis() - time1  > SHORT){
            //do long action
            time1 = millis(); //reset timer
        } 
    }
     
    if (btn0State == HIGH && button1) {
        button1 = false;
        //do short action
    }

}
Source Link
ratchet freak
  • 3.3k
  • 1
  • 13
  • 13

Don't act immediately when a button gets pressed, instead when a button is pushed all you do is set the timestamp on that button and set a flag.

Then when it is released check that timestamp against millis() and do the appropriate thing.

void loop(){
    int btn0State = digitalRead(btn0);
    static bool button1 = false;
    static unsigned long time1 = 0;
    
    if (btn0State == LOW && !button1) {
        button1 = true;
        time1 = millis();
    }
    
    if (btn0State == HIGH && button1) {
        button1 = false;
        if(millis() - time1  < SHORT){
            //do short action
        } else{
            //do long action
        }
    }

}