Paso 3: Crear una biblioteca de botón
Este es el programa que contiene todo el código que determina el estado de botón y realiza un seguimiento de los niveles de confianza de la pulsación y liberación para eliminar el efecto debouncing. Este archivo de la biblioteca se denomina: "ButtonPress.h" que se puede ver en el archivo de inclusión del programa principal. Si copia y pega este código y guardarlo como otro nombre, necesita cambiar el archivo include para el programa principal.
#ifndef ButtonPress#define ButtonPressinclude <avr/io.h> char ButtonPressed(int buttonNumber, unsigned char pinOfButton, unsigned char portBit, int confidenceLevel);char Pressed[numberOfButtons];int Pressed_Confidence_Level[numberOfButtons]; //Measure button press cofidenceint Released_Confidence_Level[numberOfButtons]; //Measure button release confidencechar ButtonPressed(int buttonNumber, unsigned char pinOfButton, unsigned char portBit, int confidenceLevel){if (bit_is_clear(pinOfButton, portBit)) { Pressed_Confidence_Level[buttonNumber] ++; //Increase Pressed Conficence Released_Confidence_Level[buttonNumber] = 0; //Reset released button confidence since there is a button press if (Pressed_Confidence_Level[buttonNumber] > confidenceLevel) //Indicator of good button press { if (Pressed[buttonNumber] == 0) { Pressed[buttonNumber] = 1; return 1;} //Zero it so a new pressed condition can be evaluated Pressed_Confidence_Level[buttonNumber] = 0;}} else { Released_Confidence_Level[buttonNumber] ++; //This works just like the pressed Pressed_Confidence_Level[buttonNumber] = 0; //Reset pressed button confidence since the button is released if (Released_Confidence_Level[buttonNumber] > confidenceLevel) { Pressed[buttonNumber] = 0; Released_Confidence_Level[buttonNumber] = 0;}} return 0;}#endif