test reed switch for bike cadence & speed

Dependencies:   mbed

Fork of Blinking Led by Icarus Sensors

main.cpp

Committer:
balczezzz
Date:
2015-04-29
Revision:
5:af96df45a447
Parent:
4:1b63302b4008
Child:
6:0ef5a242ed00

File content as of revision 5:af96df45a447:

#include "mbed.h"
#define BAUD_RATE 9600 //default baud rate

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);
   
    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");    
}