Skip to content

Instantly share code, notes, and snippets.

Created July 4, 2017 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5f3ff215a19af5d99a371ba73a5f2c65 to your computer and use it in GitHub Desktop.
Save anonymous/5f3ff215a19af5d99a371ba73a5f2c65 to your computer and use it in GitHub Desktop.
Example debouncing code for arduino
#define FIRST_PIN_TO_READ 2
#define LAST_PIN_TO_READ 12
#define COUNT_OF_PINS_READ 11 // There are 11 pins between pin 2 and including pin 12
char currentInputStates[COUNT_OF_PINS_READ], previousInputStates[COUNT_OF_PINS_READ];
// It would be a good idea to name functions in lowercase, i.e. inputs() as CONSTANTS are usually uppercase
void INPUTS() {
for (char pin = FIRST_PIN_TO_READ; pin <= LAST_PIN_TO_READ; pin++) {
char pinArrayIndex = pin - FIRST_PIN_TO_READ; // Arrays are zero based, the index for pin 2 is 0
currentInputStates[pinArrayIndex] = digitalRead(pin);
if (currentInputStates[pinArrayIndex] == LOW && currentInputStates[pinArrayIndex] != previousInputStates[pinArrayIndex]) {
switch (pin) {
case 12: //Toggles Comm1
Serial.println ("A45");
break;
case 11: //Toggles Comm2
Serial.println ("A46");
break;
case 10: //Toggle Comm1/2
Serial.println ("A47");
break;
case 9://Toggle Nav1
Serial.println ("A48");
break;
case 8: //Toggle Nav2
Serial.println ("A49");
break;
case 7: //Toggle ADF
Serial.println ("A52");
break;
}
}
previousInputStates[pinArrayIndex] = currentInputStates[pinArrayIndex];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment