test reed switch for bike cadence & speed

Dependencies:   mbed

Fork of Blinking Led by Icarus Sensors

Committer:
balczezzz
Date:
Wed Apr 29 21:58:04 2015 +0000
Revision:
5:af96df45a447
Parent:
4:1b63302b4008
Child:
6:0ef5a242ed00
revolutions/min

Who changed what in which revision?

UserRevisionLine numberNew contents of line
smigielski 0:b729782a3ad7 1 #include "mbed.h"
balczezzz 4:1b63302b4008 2 #define BAUD_RATE 9600 //default baud rate
balczezzz 5:af96df45a447 3
balczezzz 5:af96df45a447 4 InterruptIn button(p1);
balczezzz 5:af96df45a447 5 DigitalOut led(LED1);
balczezzz 5:af96df45a447 6 Timer debounce; //define debundance timer
balczezzz 5:af96df45a447 7 Timer timer1; //define timer variable
balczezzz 4:1b63302b4008 8 unsigned int counter=0;
balczezzz 5:af96df45a447 9 unsigned int revMin=0;
balczezzz 5:af96df45a447 10
balczezzz 5:af96df45a447 11 void toggle(void); //function prototype
smigielski 0:b729782a3ad7 12
smigielski 0:b729782a3ad7 13 int main() {
balczezzz 4:1b63302b4008 14 //Configure boud rate
balczezzz 4:1b63302b4008 15 Serial s(USBTX, USBRX); //default for nrf51 is p0.09 p0.11
balczezzz 4:1b63302b4008 16 s.baud(BAUD_RATE);
balczezzz 5:af96df45a447 17
balczezzz 5:af96df45a447 18 timer1.start();
balczezzz 5:af96df45a447 19 debounce.start();
balczezzz 5:af96df45a447 20 button.rise(&toggle); // attach the addressof the toggle function to the rising edge
balczezzz 4:1b63302b4008 21 }
balczezzz 5:af96df45a447 22
balczezzz 5:af96df45a447 23 void toggle() {
balczezzz 5:af96df45a447 24 if (debounce.read_ms()>100) //only allow toggle if debounce timer has passed 200ms
balczezzz 5:af96df45a447 25 led=!led;
balczezzz 5:af96df45a447 26
balczezzz 5:af96df45a447 27 debounce.reset(); //restart timer when toggle is performed
balczezzz 5:af96df45a447 28 revMin = (60*1000)/timer1.read_ms();
balczezzz 5:af96df45a447 29 timer1.reset();
balczezzz 5:af96df45a447 30 counter = counter + 1;
balczezzz 5:af96df45a447 31 printf("id %i",counter);
balczezzz 5:af96df45a447 32 printf(" - revs/min: %i\n",revMin);
balczezzz 5:af96df45a447 33 //printf("switch \n\r");
balczezzz 5:af96df45a447 34 }