Created
July 4, 2017 19:14
-
-
Save anonymous/5f3ff215a19af5d99a371ba73a5f2c65 to your computer and use it in GitHub Desktop.
Example debouncing code for arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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