Two pushbuttons with two callbacks

Dependencies:   PinDetect mbed

Fork of Pushbutton_Debounce_Interrupt by jim hamblen

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PinDetect.h"
00003 // must import Cookbook PinDetct library into project
00004 // URL: http://mbed.org/users/AjK/libraries/PinDetect/lkyxpw
00005 
00006 DigitalOut myled(LED1);
00007 DigitalOut myled2(LED2);
00008 DigitalOut myled3(LED3);
00009 DigitalOut myled4(LED4);
00010 
00011 PinDetect pb1(p8);
00012 PinDetect pb2(p7);
00013 // SPST Pushbutton debounced count demo using interrupts and callback
00014 // no external PullUp resistor needed
00015 // Pushbutton from P8 to GND.
00016 // Second Pushbutton from P7 to GND.
00017 // A pb hit generates an interrupt and activates the callback function
00018 // after the switch is debounced
00019 
00020 // Global count variable
00021 int volatile count=0;
00022 
00023 // Callback routine is interrupt activated by a debounced pb1 hit
00024 void pb1_hit_callback (void) {
00025     count++;
00026     myled4 = count & 0x01;
00027     myled3 = (count & 0x02)>>1;
00028     myled2 = (count & 0x04)>>2;
00029 }
00030 // Callback routine is interrupt activated by a debounced pb2 hit
00031 void pb2_hit_callback (void) {
00032     count--;
00033     myled4 = count & 0x01;
00034     myled3 = (count & 0x02)>>1;
00035     myled2 = (count & 0x04)>>2;
00036 }
00037 int main() {
00038 
00039     // Use internal pullups for pushbutton
00040     pb1.mode(PullUp);    
00041     pb2.mode(PullUp);
00042     // Delay for initial pullup to take effect
00043     wait(.01);
00044     // Setup Interrupt callback functions for a pb hit
00045     pb1.attach_deasserted(&pb1_hit_callback);
00046     pb2.attach_deasserted(&pb2_hit_callback);
00047     // Start sampling pb inputs using interrupts
00048     pb1.setSampleFrequency();
00049     pb2.setSampleFrequency();
00050     //Blink myled in main routine forever while responding to pb changes
00051     // via interrupts that activate the callback counter function
00052     while (1) {
00053         myled = !myled;
00054         wait(.5);
00055     }
00056 
00057 }