Multiple Key debounce question

bit of a preface first off:

5 buttons (the volup/voldn is a rotary switch) all with a common pin (tied to +5v) and individual outputs tied to a 10k resistor then to ground. trigger is taken from the output of the switch

initial spec is to get all these buttons to toggle pin 13 ( I have a UNO) on/off.

that way I have the debounce routine correct.

using the debounce example sketch with added variables to account for the extra buttons and for some reason i cannot get it to compile.

from there I can move onto the bigger stuff below.

later spec is to get the VOLUP & VOLDN buttons to execute a serial command out to an external unit, generate a video display (I have the video overlay shield with a maxim 7456 on it) on an external display in the car.

the TUNE button to toggle between a LOW/HIGH output or via a timed loop send a serial command to an external unit

the rest are simply push buttons that toggle a transistor to ground so I still have my OE radio being controlled as it was originally

my background is in hardware so spinning a board is easy for me do this , not so much

  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to set pin numbers:
const int buttonVD = 2;    // the number of the Vol VD pin
const int buttonVU = 4;    // the number of the VOL VU pin
const int buttonTN = 7;    // the number of the TUNE pin
const int buttonRON = 8;    // the number of the RADIO ON pin
const int buttonSRC = 12;    // the number of the AM/FM/SOURCE pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = LOW;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonVD, INPUT);
  pinMode(buttonVU, INPUT);
  pinMode(buttonTN, INPUT);
  pinMode(buttonRON, INPUT);
  pinMode(buttonSRC, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonVD),
  int reading = digitalRead(buttonVU)
  int reading = digitalRead(buttonTN)
  int reading = digitalRead(buttonRON)
  int reading = digitalRead(buttonSRC);
  // 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 != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

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

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

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

You do not need a library to debounce switches.

Just sample them every ~50ms and look for a change in state.

larryd:
You do not need a library to debounce switches.

Just sample them every ~50ms and look for a change in state.

clarify please.

that's what I thought that sketch did at first.

Apologies, I was looking at someone else who used a debounce library in their sketch.

chrrroger:
for some reason i cannot get it to compile.

If you'd copy the error and put it in a post folks could see it and offer guidance.

Tell us what you think is happening here:
int reading = digitalRead(buttonVD),
int reading = digitalRead(buttonVU)
int reading = digitalRead(buttonTN)
int reading = digitalRead(buttonRON)
int reading = digitalRead(buttonSRC);

Is your syntax correct?

larryd:
Tell us what you think is happening here:
int reading = digitalRead(buttonVD),
int reading = digitalRead(buttonVU)
int reading = digitalRead(buttonTN)
int reading = digitalRead(buttonRON)
int reading = digitalRead(buttonSRC);

Is your syntax correct?

AH! that was me trying to work out why.

int reading = digitalRead(buttonVD),
  int reading = digitalRead(buttonVU)
  int reading = digitalRead(buttonTN)
  int reading = digitalRead(buttonRON)
  int reading = digitalRead(buttonSRC);

thought was to set up the switches to be read. I did omit the ';' by mistake

int reading = digitalRead(buttonVD);
  int reading = digitalRead(buttonVU);
  int reading = digitalRead(buttonTN);
  int reading = digitalRead(buttonRON);
  int reading = digitalRead(buttonSRC);

that's what I started with.

ERRORS:

Arduino: 1.8.9 (Windows 10), Board: "Arduino/Genuino Mega or Mega 2560, ATmega2560 (Mega 2560)"

C:\Users\HP PC\Documents\Arduino\HR31-audio-routines\radio-ctl-debounce\radio-ctl-debounce.ino: In function 'void loop()':

radio-ctl-debounce:64:7: error: redeclaration of 'int reading'

int reading = digitalRead(buttonVU);

^

C:\Users\HP PC\Documents\Arduino\HR31-audio-routines\radio-ctl-debounce\radio-ctl-debounce.ino:63:7: note: 'int reading' previously declared here

int reading = digitalRead(buttonVD);

^

radio-ctl-debounce:65:7: error: redeclaration of 'int reading'

int reading = digitalRead(buttonTN);

^

C:\Users\HP PC\Documents\Arduino\HR31-audio-routines\radio-ctl-debounce\radio-ctl-debounce.ino:63:7: note: 'int reading' previously declared here

int reading = digitalRead(buttonVD);

^

radio-ctl-debounce:66:7: error: redeclaration of 'int reading'

int reading = digitalRead(buttonRON);

^

C:\Users\HP PC\Documents\Arduino\HR31-audio-routines\radio-ctl-debounce\radio-ctl-debounce.ino:63:7: note: 'int reading' previously declared here

int reading = digitalRead(buttonVD);

^

radio-ctl-debounce:67:7: error: redeclaration of 'int reading'

int reading = digitalRead(buttonSRC);

^

C:\Users\HP PC\Documents\Arduino\HR31-audio-routines\radio-ctl-debounce\radio-ctl-debounce.ino:63:7: note: 'int reading' previously declared here

int reading = digitalRead(buttonVD);

^

exit status 1
redeclaration of 'int reading'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

that may be the multiple times I tried to compile it. the error is constant though.

int reading = digitalRead(buttonVD);
int reading = digitalRead(buttonVU);
int reading = digitalRead(buttonTN);
int reading = digitalRead(buttonRON);
int reading = digitalRead(buttonSRC);

Tell us what you think is happening for each line of code.

BTW, “ error: redeclaration of 'int reading'” Dose this give you a hint?

  int reading = digitalRead(buttonVD),
  int reading = digitalRead(buttonVU)
  int reading = digitalRead(buttonTN)
  int reading = digitalRead(buttonRON)
  int reading = digitalRead(buttonSRC);

"int reading;" declares an integer variable with the name 'reading'.
The " = digitalRead(buttonVU)" initializes the variable with the value returned by the function call.

First, you can't declare the same variable more then once in the same block:

  int reading = digitalRead(buttonVD);
  reading = digitalRead(buttonVU);
  reading = digitalRead(buttonTN);
  reading = digitalRead(buttonRON);
  reading = digitalRead(buttonSRC);

Second, you can't assign a variable more than one value so this bit of code is equivalent to:

  int reading = digitalRead(buttonSRC);

thanks for the help. still getting my head around a lot of this.
will keep fiddling with it.

Here are some more hints:

First read this: loop() - Arduino Reference to understand setup() and loop().

I saw you were defining and initializing variable in your void loop().

First have your setup which runs once:
void setup() {
int reading = digitalRead(buttonVD);
...
}

Then your loop which runs forever: (note, I’m not defining reading, just assigning a value to it)

void loop() {
reading = digitalRead(buttonVD);
...
}

As to debouncing: my experience is that if the button has been consistent for 30ms, (perhaps read every 10ms and sample 3 times) then you can use it. If it changes then start sampling again.

You can also put the variable definition before the setup(). See this example:

https://www.arduino.cc/reference/en/language/functions/digital-io/digitalread/

rjj100:
Here are some more hints:

First read this: loop() - Arduino Reference to understand setup() and loop().

I saw you were defining and initializing variable in your void loop().

First have your setup which runs once:

void setup() {

int reading = digitalRead(buttonVD);
 ...
}




Then your loop which runs forever: (note, I’m not defining reading, just assigning a value to it)



void loop() {
reading = digitalRead(buttonVD);
...
}

Nope, that won't work. If you declare 'reading' as a local variable in setup() you can't use that variable in loop().
Since the variable 'reading' is never used outside loop() you can, and should, make it local to loop():

void setup() {
}

void loop() {
  int reading ;


  reading = digitalRead(buttonVD);
  ...
}

If the value 'reading' is shared across multiple functions, make it global:

int Reading;  // Globals default to 0
void setup() {
}

void loop() {
  Reading = digitalRead(buttonVD);
  ...
}

Note that I use an initial capital 'R' to help keep track of which variables are global.

Right, should put it before setup. Thats how I always do it. My mistake.