I am trying to debounce switches in an existing sketch that I am wanting to use. Have tried a few methods of debouncing that I found on various places on the net but can't get it integrated in what I already have and as a newbie I am at a loss. The relevant parts of the sketch are:
void setup()
// For pushButtons:
int pushButton = 0;
void loop() {
if (digitalRead(22)) {
if (pushButton !=22) {
pushButton=22;
Serial.println("Input 1");
AtemSwitcher.changePreviewInput(1);
}
} else if (pushButton==22) {
pushButton = 0;
}
if (digitalRead(23)) {
if (pushButton !=23) {
pushButton=23;
Serial.println("Select 2");
AtemSwitcher.changePreviewInput(2);
}
} else if (pushButton==23) {
pushButton = 0;
}
}
I have shown only two of the fifteen switches. I get debounce errors both at the make and the break. Any and all help is appreciated.
kaaskop:
I have shown only two of the fifteen switches. I get debounce errors both at the make and the break. Any and all help is appreciated.
Here is some code you can use for any number of mechanical buttons and switches:
#define INPUTMODE INPUT_PULLUP // INPUT or INPUT_PULLUP
#define BOUNCETIME 5 // bouncing time in milliseconds
byte buttonPins[]={2, 3, 4, 5, 6};// pin numbers of all buttons
#define NUMBUTTONS sizeof(buttonPins) // number of buttons (automatically calculated)
byte buttonState[NUMBUTTONS]; // array holds the actual HIGH/LOW states
byte buttonChange[NUMBUTTONS]; // array holds the state changes when button is pressed or released
enum{UNCHANGED,BUTTONUP,BUTTONDOWN};
void input(){
// read the input state and state changes of all buttons
static unsigned long lastButtonTime; // time stamp for remembering the time when the button states were last read
memset(buttonChange,0,sizeof(buttonChange)); // reset all old state changes
if (millis()-lastButtonTime<BOUNCETIME) return; // within bounce time: leave the function
lastButtonTime=millis(); // remember the current time
for (int i=0;i<NUMBUTTONS;i++)
{
byte curState=digitalRead(buttonPins[i]); // current button state
if (INPUTMODE==INPUT_PULLUP) curState=!curState; // logic is inverted with INPUT_PULLUP
if (curState!=buttonState[i]) // state change detected
{
if (curState==HIGH) buttonChange[i]=BUTTONDOWN;
else buttonChange[i]=BUTTONUP;
}
buttonState[i]=curState; // save the current button state
}
}
void output(){
// send a message to Serial if a button state (pressed/released) has changed
// button pressed: Send button pin number with minus sign
// button released: Send button pin number
byte action;
for (int i=0;i<NUMBUTTONS;i++)
{
switch (buttonChange[i])
{
case BUTTONUP: Serial.println(buttonPins[i]);break;
case BUTTONDOWN: Serial.println(-buttonPins[i]);break;
}
}
}
void setup() {
Serial.begin(9600); // initialize Serial at 9600 baud
// then initialize all buttons
for (int i=0;i<NUMBUTTONS;i++) pinMode(buttonPins[i],INPUTMODE);
}
void loop() {
input();
// processing(); // if you need any processing, write a function for it!
output();
}
I'm always using the IPO programming model for arduino sketches in the loop:
input
processing
output
In the code define INPUTMODE accordingly to the usage of pull-resistors in your circuit:
when using no pull-resistors or external pull-up resistors ==> INPUT_PULLUP
when using external pull-down resistors ==> INPUT
In the demo sketch, input is taken from the buttons, not any processing is done, and the output is a short message on Serial when a button was pressed or released.