debounce 2  Digital inputs / 2 outputs.Please HELP

Hi There:

I'm new with Arduino and programing. I'm trying to have 2 independent switch digital inputs 2 and 3 with debounce of 2 sec and 2 outputs to command 2 different leds. I already have working the debounce on button2 to ledPin13, but button 3 is not debouncing output led12, i have same timing as the input. Please Help, will appreciate it. This is my programing:

// constants won't change. They're used here to // set pin numbers: const int buttonPin2 = 2; const int buttonPin3 = 3; // the number of the pushbutton pin const int ledPin13 = 13; const int ledPin12 = 12; // the number of the LED pin

// Variables will change: int ledState = HIGH; // the current state of the output pin int button2State; int button3State; // the current reading from the input pin int lastButton2State = LOW; // the previous reading from the input pin int lastButton3State = LOW; // the following variables are long's because the time, measured in miliseconds, // will quickly become a bigger number than can be stored in an int. long lastDebounceTime = 0; // the last time the output pin was toggled long debounceDelaybuttonPin2 = 2000; // the debounce time; increase if the output flickers long debounceDelaybuttonPin3 = 2000;

void setup() { pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(ledPin13, OUTPUT); pinMode(ledPin12, OUTPUT); }

void loop() { // read the state of the switch into a local variable: int reading = digitalRead(buttonPin2);

// check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited // long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing: if (reading != lastButton2State) {

// reset the debouncing timer lastDebounceTime = millis(); }

if ((millis() - lastDebounceTime) > debounceDelaybuttonPin2) { // whatever the reading is at, it's been there for longer // than the debounce delay, so take it as the actual current state: button2State = reading;

}

// read the state of the switch into a local variable:

// check to see if you just pressed the button // (i.e. the input went from LOW to HIGH), and you've waited // long enough since the last press to ignore any noise:

// If the switch changed, due to noise or pressing: if (reading != lastButton3State) { // reset the debouncing timer lastDebounceTime = millis();

}

if ((millis() - lastDebounceTime) > debounceDelaybuttonPin3) { // whatever the reading is at, it's been there for longer int reading = digitalRead(buttonPin3);

if ((millis() - lastDebounceTime) > debounceDelaybuttonPin2) // than the debounce delay, so take it as the actual current state: button3State = reading; } // set the LED using the state of the button: digitalWrite(ledPin13, button2State); digitalWrite(ledPin12, button3State);

// save the reading. Next time through the loop, // it'll be the lastButtonState: lastButton2State = reading; lastButton3State = reading; }

 if ((millis() - lastDebounceTime) > debounceDelaybuttonPin3) {
    // whatever the reading is at, it's been there for longer
            int reading = digitalRead(buttonPin3); 

             if ((millis() - lastDebounceTime) > debounceDelaybuttonPin2)   // than the debounce delay, so take it as the actual current state:
    button3State = reading;
  }

I don't understand why the state of button3 should depend upon the state of button2 -are they mechanically linked in some way?

Please use the Code (#) button when posting code.

Hi AWOL

Thanks for your Help. Maybe i didn't express my self. button 3 does NOT depend upon the state of button 2. Button 2 works perfect, i get a debounce output on ledPin13. Button 3 works, but my output on ledPin12 is not debounced, i get the same as the input (no delay) appreciate if you can give me a hand Thanks Ruben

There is only one statement to read the state of a button - button2. You also need to read the state of button3.

Hi Pauls

Thanks for your Reply. I have been trying this for hours. Could you repair my code and post here? or show me what to do? I'm new on Arduino and i don't find where to read the state of button3. Thanks Ruben

You have this comment and code to read the state of button 2:

// read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin2);

You have this comment and code to read the state of button 3:

// read the state of the switch into a local variable:

Add this:

int reading = digitalRead(buttonPin3);

after the 2nd comment.

Hi Pauls

I have try it but i get this error:

error: redeclaration of 'int reading'

In function 'void loop()':

Thanks Ruben

Remove the int declaration on the 2nd occurence.

Thanks, i don't have an error anymore, but now both leds lit at the same time. I have 2 inputs blinking on and off every 1 second (from Spykee Robot). I want to do a code that disregards any blinking 1 or 0 on the inputs, and only give me an output when i have an steady input.

This will be the table for it:

input 2 input3 output 13 output 12

steady 1 x 1 x steady 0 x 0 x Blink 1-0 x 0 x x steady 1 x 1 x steady 0 x 0 x Blink 1-0 x 0

Both inputs have to be independent one from another. The blink rate to the input is about 1 sec. That is way i used the debounce code in order to disregard any blinks below 2 sec., and it works great with input 2 and output 13, but i haven't been able to make to work to input 3 yet. Please, any ideas?

Thanks Ruben

At the end of loop, unless you changed it, you set lastButton2State and lastButton3State to the same value.

I suspect that you need to add another variable. Change the "int reading" to "int reading2" and the 2nd use of reading to reading3. Then, set the last button states appropriately.

WoW Pauls

That was it ;D Thank you, thank you very much.
I have been battle with it for more than 6 hours. This is the final code in case anybody may need it:

 // constants won't change. They're used here to 
 // set pin numbers:
 const int buttonPin2 = 2; 
 const int buttonPin3 = 3; // the number of the pushbutton pin
 const int ledPin13 =  13; 
 const int ledPin12 =  12; // the number of the LED pin

 // Variables will change:
 int ledState = HIGH;         // the current state of the output pin
 int button2State; 
 int button3State; // the current reading from the input pin
 int lastButton2State = LOW;   // the previous reading from the input pin
 int lastButton3State = LOW;
 // the following variables are long's because the time, measured in miliseconds,
 // will quickly become a bigger number than can be stored in an int.
 long lastDebounceTime = 0;  // the last time the output pin was toggled
 long debounceDelaybuttonPin2 = 2000;    // the debounce time; increase if the output flickers
 long debounceDelaybuttonPin3 = 2000; 
 
 void setup() {
   pinMode(buttonPin2, INPUT);
    pinMode(buttonPin3, INPUT);
   pinMode(ledPin13, OUTPUT);
      pinMode(ledPin12, OUTPUT);
 }

 void loop() {
   // read the state of the switch into a local variable:
   int reading2 = digitalRead(buttonPin2); 


   // check to see if you just pressed the button 
   // (i.e. the input went from LOW to HIGH),  and you've waited 
   // long enough since the last press to ignore any noise:  

   // If the switch changed, due to noise or pressing:
   if (reading2 != lastButton2State) {

   // reset the debouncing timer
     lastDebounceTime = millis();
   } 

   if ((millis() - lastDebounceTime) > debounceDelaybuttonPin2) {
     // whatever the reading is at, it's been there for longer
     // than the debounce delay, so take it as the actual current state:
     button2State = reading2;

   }
   

   // read the state of the switch into a local variable:

   int reading3 = digitalRead(buttonPin3);

   // check to see if you just pressed the button 
   // (i.e. the input went from LOW to HIGH),  and you've waited 
   // long enough since the last press to ignore any noise:  

   // If the switch changed, due to noise or pressing:
   if (reading3 != lastButton3State) {
     // reset the debouncing timer
     lastDebounceTime = millis();

   } 

   if ((millis() - lastDebounceTime) > debounceDelaybuttonPin3) {
     // whatever the reading is at, it's been there for longer
             int reading = digitalRead(buttonPin3); 

              if ((millis() - lastDebounceTime) > debounceDelaybuttonPin3)   // than the debounce delay, so take it as the actual current state:
     button3State = reading3;
   }
   // set the LED using the state of the button:
   digitalWrite(ledPin13, button2State);
      digitalWrite(ledPin12, button3State);

   // save the reading.  Next time through the loop,
   // it'll be the lastButtonState:
   lastButton2State = reading2;
   lastButton3State = reading3;
 }
[glow][/glow]