Pushbutton counter demo with debounce

Dependencies:   mbed DebounceIn

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DebounceIn.h"
00003 // must import Cookbook Debounce library into project
00004 // URL: http://mbed.org/users/AjK/libraries/DebounceIn/lky9pc
00005 
00006 DigitalOut myled(LED1);
00007 DigitalOut myled2(LED2);
00008 DigitalOut myled3(LED3);
00009 DigitalOut myled4(LED4);
00010 
00011 DebounceIn pb(p8);
00012 // SPST Pushbutton count demo using internal PullUp function
00013 // no external PullUp resistor needed
00014 // Pushbutton from P8 to GND.
00015 // Demonstrates need for debounce - will not count more than once per button hit
00016 // This ocuurs on all switches due to mechanical contact bounce
00017 int main()
00018 { 
00019 int count=0;
00020 int old_pb=0;
00021 int new_pb;
00022     // Use internal pullup for pushbutton
00023     pb.mode(PullUp);
00024     // Delay for initial pullup to take effect
00025     wait(.001);
00026     while(1) {
00027         new_pb = pb;
00028         if ((new_pb==0) && (old_pb==1)) count++;
00029         myled4 = count & 0x01;
00030         myled3 = (count & 0x02)>>1;
00031         myled2 = (count & 0x04)>>2;
00032         myled = (count & 0x08)>>3;
00033         old_pb = new_pb;
00034     }
00035 }