2.4.2 Interrupt Experiment with a Debounced Switch

Dependencies:   C12832 mbed

Committer:
PCStokes
Date:
Sun Jul 08 21:32:48 2018 +0000
Revision:
0:903862c2ac65
2.4.2	Interrupt Experiment with a Debounced Switch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
PCStokes 0:903862c2ac65 1 #include "mbed.h" // includes libraries for the mBed.
PCStokes 0:903862c2ac65 2 #include "C12832.h" // includes libraries for the mBed LCD Display.
PCStokes 0:903862c2ac65 3 // 2.5 Debounce Switch Experiment
PCStokes 0:903862c2ac65 4 // This is to show the effect of the introduction of an interrupt. the interrupt will be triggered using a button on the mbed and will set check to see if a variable has been set. if not it will set it.
PCStokes 0:903862c2ac65 5
PCStokes 0:903862c2ac65 6 C12832 lcd(p5, p7, p6, p8, p11); // declaration of the On board LCD display
PCStokes 0:903862c2ac65 7 DigitalOut led1(LED1); // declaration for the Digital output to LED 1
PCStokes 0:903862c2ac65 8 DigitalOut flash (LED4); // declaration for the Digital output to LED 1
PCStokes 0:903862c2ac65 9 InterruptIn button(p14); // declaration for the interrupt In button using Pin 14 which is the middle button on the joystick.
PCStokes 0:903862c2ac65 10
PCStokes 0:903862c2ac65 11 int Var_val = 0; //This is setting the value of the Variable value required to a known value.
PCStokes 0:903862c2ac65 12 int Counter = 0; //This is setting the value of the Variable value required to a known value, I have chosen to set the number of counts of the button presses to the LCD Screen.
PCStokes 0:903862c2ac65 13 Timer debounce; // declares the timer class debounce, the debounce is used to prevent false signals (false positives when a button is pressed) as interactions on the board are so fast the action of pressing the button is slow by comparison and may result in several button press detections.
PCStokes 0:903862c2ac65 14
PCStokes 0:903862c2ac65 15 void flip() //This function is activated by the button and flips the value of button press.
PCStokes 0:903862c2ac65 16 {
PCStokes 0:903862c2ac65 17 if (debounce.read_ms() >(500)) { //this conditional statement checks to see if the debounce time is greater than 500milliseconds. It ignores any detected presses inside of this time.
PCStokes 0:903862c2ac65 18 debounce.reset(); //this will reset the button press timer allowing for another press to happen and the debouncing timer to start again.
PCStokes 0:903862c2ac65 19 Var_val=1; // this resets the required variable value to logic 1
PCStokes 0:903862c2ac65 20 led1=!led1; // this toggles the led as it takes the value of Led1 and sets it to not Led1, when pressing the button this connects the 3.3v to the pin p14.
PCStokes 0:903862c2ac65 21 }
PCStokes 0:903862c2ac65 22 }
PCStokes 0:903862c2ac65 23 void printout() {//This function will print the values to the LCD.
PCStokes 0:903862c2ac65 24 if (Var_val==1) { //This is a conditional if statement. If the variable in the brackets equals 1 then it runs the print out code.
PCStokes 0:903862c2ac65 25 lcd.cls(); // clears the LCD.
PCStokes 0:903862c2ac65 26 lcd.locate(0,0); // sets the location on the LCD that will be used later.
PCStokes 0:903862c2ac65 27 lcd.printf("Variable Set & will Reset \n Count %d", Counter++); // uses the printf function to print the defined text and the placeholder for the integer (Counter++) which I am using to print out the count value of the number of flips.
PCStokes 0:903862c2ac65 28 } //closes out the if condition.
PCStokes 0:903862c2ac65 29 }
PCStokes 0:903862c2ac65 30 int main() // this is the main function in the code.
PCStokes 0:903862c2ac65 31 {
PCStokes 0:903862c2ac65 32 button.rise(&flip); //attach the function address to the rising edge of the button on pin p14, this value will then be flipped (change in logic state).
PCStokes 0:903862c2ac65 33 debounce.start(); //This starts the debounce timer, and will make the if statement above ignore subsequent signals until the debounce time has been met.
PCStokes 0:903862c2ac65 34 Counter = 1; //This is the initial counter state used to print out the button presses.
PCStokes 0:903862c2ac65 35
PCStokes 0:903862c2ac65 36 while(1) { // This is the while loop, while 1 is infinite, the interrupt will run inside this. the interrupt will be used to reset the variable value and print the count value of the button presses.
PCStokes 0:903862c2ac65 37 flash = !flash; // this turns led4 on and off every 1/4 of a second.
PCStokes 0:903862c2ac65 38 printout(); //runs the print to lcd function- Keeping the printout outside of the flip allows the test of the variable to be conducted as part of the conditional statement.
PCStokes 0:903862c2ac65 39 Var_val = 0; //resets the value of the variable to 0.
PCStokes 0:903862c2ac65 40 wait (.25); //wait period for the loop.
PCStokes 0:903862c2ac65 41 } //closes out the while 1 loop
PCStokes 0:903862c2ac65 42 } //closes out the main.