test reed switch for bike cadence & speed
Dependencies: mbed
Fork of Blinking Led by
Diff: main.cpp
- Revision:
- 5:af96df45a447
- Parent:
- 4:1b63302b4008
- Child:
- 6:0ef5a242ed00
diff -r 1b63302b4008 -r af96df45a447 main.cpp --- a/main.cpp Tue Apr 28 06:25:30 2015 +0000 +++ b/main.cpp Wed Apr 29 21:58:04 2015 +0000 @@ -1,29 +1,34 @@ #include "mbed.h" - #define BAUD_RATE 9600 //default baud rate -DigitalOut led(LED1); //P0_18 in NRF51822_MKIT -DigitalIn reed_speed(p1, PullDown); //P0_01 in NRF51822_MKIT + +InterruptIn button(p1); +DigitalOut led(LED1); +Timer debounce; //define debundance timer +Timer timer1; //define timer variable unsigned int counter=0; +unsigned int revMin=0; + +void toggle(void); //function prototype int main() { //Configure boud rate Serial s(USBTX, USBRX); //default for nrf51 is p0.09 p0.11 s.baud(BAUD_RATE); - counter = 0; - - while(1) { - if (reed_speed) { - counter = counter + 1; - led = 1; // LED is ON - printf("number of revs: %i\n",counter); - while(reed_speed){ //display dots to check if counting works - printf("."); - } - - } - else { - led = 0; //LED is OFF - } - wait(0.100); // 100 ms - } + + timer1.start(); + debounce.start(); + button.rise(&toggle); // attach the addressof the toggle function to the rising edge } + +void toggle() { +if (debounce.read_ms()>100) //only allow toggle if debounce timer has passed 200ms + led=!led; + + debounce.reset(); //restart timer when toggle is performed + revMin = (60*1000)/timer1.read_ms(); + timer1.reset(); + counter = counter + 1; + printf("id %i",counter); + printf(" - revs/min: %i\n",revMin); + //printf("switch \n\r"); +}