Debounce and more!

Hi there to all! I am just a new guy that the last 3 months on his free time is "fighting" to learn how to work with arduino! In my last try i was on Debounce sketch and i was tring to put one more function on the same button. What i mean? I wanna make one button dual functionally ! i wanna have the all debounce and if i am pushing the button longer than 2 sec to get on another function to open one more led for couple seconds! (the rest of debounce sketch to remail as it is) Is there anyone to give a small help????

Do you understand the debounce sketch? It reads the state of a button and stores that state in a temporary variable, then it lets some time pass (50 milliseconds or so) and reads the state again. If the previous state matches the new state, then it does something to indicate that the two states match.

However what you want to do, is extend that time to [u]2000[/u] milliseconds or 2 seconds and check if the two states are the same. I would recommend you check to see if the button is still held down the during the duration of this process, otherwise you could press the button then wait 2 seconds and press it again, and it will return true. You should have it check to see if the button is held down and if it is not, reset the timer back to 0.

May I wasnt so able to make you understand on my first msg.
So i want to blink 2 leds from the same button with 2 diferent functions: one by on/off (as the debounce) and the second by keeping the button for more than 2 seconds that is gonna open a second LED together with the first but the second one for couple seconds only!
the problem gets that i am tring on and i make faults all the time! that is why i ask for hepl! possibly i didnt get the debounce well, maybe not also!!! the theme is that i would like to put it into s atiny85 in the end…

Post your code. Please use code tags, they are the # symbol above the smiley face. [ code ] ... [/ code]

i make faults all the time!

What faults? Can you post the errors your getting.

May I wasnt so able to make you understand on my first msg. So i want to blink 2 leds from the same button with 2 diferent functions: one by on/off (as the debounce) and the second by keeping the button for more than 2 seconds that is gonna open a second button together with the first but the second one for couple seconds only! the problem gets that i am tring on and i make faults all the time! that is why i ask for hepl! possibly i didnt get the debounce well, maybe not also!!! the theme is that i would like to put it into s atiny85 in the end......

Rephrasing ... so you have one button that needs to access 2 different functions, one function for ON/OFF (toggle) and one function that runs if the button is pressed for 2 seconds or longer.

Firstly, I would make sure that your code works OK with the on/off function with debounce set to 50-100 ms or so. You'll need to use a variable that toggles (1-0-1..., or TRUE-FALSE-TRUE...) for each button press.

Then you could test what happens if the button is held longer than the debounce timeout ... does the LED operate correctly. Better yet, you could view the status by using some print statements and opening the print monitor.

The problem with extending the debounce interval to 2 seconds, is that it will also limit your valid on/off presses to 2 seconds max speed. So I would keep the debounce timing set to what works well for your on/off function, then develop the code to detect if the button is pressed for 2 seconds duration or longer.

johnie_Lougos:
May I wasnt so able to make you understand on my first msg.
So i want to blink 2 leds from the same button with 2 diferent functions: one by on/off (as the debounce) and the second by keeping the button for more than 2 seconds that is gonna open a second LED together with the first but the second one for couple seconds only!
the problem gets that i am tring on and i make faults all the time! that is why i ask for hepl! possibly i didnt get the debounce well, maybe not also!!! the theme is that i would like to put it into s atiny85 in the end…

So what you need is to figure out how long the button is pressed down for. You do that by recording the time that is changes from NOT PRESSED to PRESSED. When the button is released (PRESSED to NOT PRESSED), you subtract the recorded time from the current time to get the amount of time is was pressed down for. You can then compare that to a constant value and take action based on that.

Thanks Dlloyd you gave me guide!

I stuck on a begginers for code !!!
I want to build an if else status but for the timing differance i’ve made this code and i think that i dont have it right
#(analogRead(button1)==HIGH && digitalRead(button1<=2000))
is right to make the code understand that when i am on HIGH on the button for more than 2 seconds (here comes the if and the else as resaults)

What you have is wrong, but please post your full code anyways.

What you need to do is: 1) debounce the button 2) record the state and time "millis()" of that button [u]after[/u] it is debounced 3) continuously check the state of the button and see if the currentTime - oldTime >= 2000 4) if the button is HIGH or Pressed and time difference is equal to or greater than 2 seconds, do your function, else keep checking the difference.

Here is a working example you can try.

 const byte buttonPin = 2;     
const byte ledPin =  13;

boolean buttonState = 0;
boolean  lastReading = 0;
unsigned long onTime = 0;

void setup() { 
  pinMode(ledPin, OUTPUT);      
  pinMode(buttonPin, INPUT);     
}

void loop(){ 
  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH && lastReading == LOW) // first check to see if the button is pressed ie. HIGH and it is different from lastReading 
  {
    onTime = millis(); // record time
    lastReading = HIGH; // prevents the code from entering this IF statement, until lastReading is set to LOW again.
  }

  if (buttonState == HIGH && lastReading == HIGH) // button is still held down 
  {
    if ((millis() - onTime) > 3000) // check to see if button is held down for 3 seconds 
    {
      digitalWrite(ledPin, HIGH); // if button is held down for 3 seconds, LED on
      lastReading = LOW;
    } 
    else 
    {
      digitalWrite(ledPin, LOW); // otherwise LED remains off
    }
  }
}

CODE:

const int buttonPin = 2;
const int ledPin = 7;
const int ledPin1 = 8;

int ledState = LOW;
int buttonState;
int buttonState1 = 0;
int lastButtonState = LOW;

long lastDebounceTime = 0;
long debounceDelay = 50;
long xronos = 81250;

void setup() {

pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(ledPin1, OUTPUT);
digitalWrite(ledPin, ledState);

}

void loop()

{ buttonState1 = digitalRead(buttonPin);
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}

if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}

digitalWrite(ledPin, ledState);

lastButtonState = reading;
buttonState1 = digitalRead(buttonPin);

if (millis() - lastDebounceTime >xronos && buttonState1 == HIGH) {

digitalWrite(ledPin1, HIGH);
}
else {

digitalWrite(ledPin1, LOW);
}}

Finnaly i make it!!!!!! Now its time to put it into a Atiny85 to work!!!!!!