![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Debounce and latch a button input
Dependencies: mbed
Debounce.cpp
- Committer:
- ethanharstad
- Date:
- 2014-06-02
- Revision:
- 0:59aa41d04f51
File content as of revision 0:59aa41d04f51:
#include "mbed.h" Serial pc(USBTX, USBRX); DigitalIn btn(USER_BUTTON); DigitalOut led(LED1); Timer timer; Timer debounce; bool state = false; bool prevState = false; bool counting = false; int main() { while(true) { state = btn; if(state != prevState) { debounce.start(); } if(debounce.read_ms() > 20) { debounce.stop(); debounce.reset(); if(!state) { if(!counting) { counting = true; led = true; timer.start(); } else { counting = false; led = false; timer.stop(); pc.printf("%i ms\n", timer.read_ms()); timer.reset(); } } } prevState = state; } }