test reed switch for bike cadence & speed

Dependencies:   mbed

Fork of Blinking Led by Icarus Sensors

Committer:
balczezzz
Date:
Wed Apr 29 23:00:36 2015 +0000
Revision:
6:0ef5a242ed00
Parent:
5:af96df45a447
Child:
7:d6844dacbd2e
rev counter + speed indicator working

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 6:0ef5a242ed00 10 float currentSpeed=0;
balczezzz 6:0ef5a242ed00 11 unsigned int currentTime = 0;
balczezzz 6:0ef5a242ed00 12 const float wheelDiameter=0.662; //662mm rim + 2*20mm tire put in meters
balczezzz 5:af96df45a447 13
balczezzz 5:af96df45a447 14 void toggle(void); //function prototype
smigielski 0:b729782a3ad7 15
smigielski 0:b729782a3ad7 16 int main() {
balczezzz 4:1b63302b4008 17 //Configure boud rate
balczezzz 4:1b63302b4008 18 Serial s(USBTX, USBRX); //default for nrf51 is p0.09 p0.11
balczezzz 4:1b63302b4008 19 s.baud(BAUD_RATE);
balczezzz 5:af96df45a447 20
balczezzz 5:af96df45a447 21 timer1.start();
balczezzz 5:af96df45a447 22 debounce.start();
balczezzz 5:af96df45a447 23 button.rise(&toggle); // attach the addressof the toggle function to the rising edge
balczezzz 4:1b63302b4008 24 }
balczezzz 5:af96df45a447 25
balczezzz 5:af96df45a447 26 void toggle() {
balczezzz 5:af96df45a447 27 if (debounce.read_ms()>100) //only allow toggle if debounce timer has passed 200ms
balczezzz 5:af96df45a447 28 led=!led;
balczezzz 5:af96df45a447 29
balczezzz 5:af96df45a447 30 debounce.reset(); //restart timer when toggle is performed
balczezzz 6:0ef5a242ed00 31 currentTime=timer1.read_ms();
balczezzz 6:0ef5a242ed00 32 revMin = (60*1000)/currentTime;
balczezzz 6:0ef5a242ed00 33 currentSpeed = ((3.14159*wheelDiameter*1000)/(currentTime))*3.6; //km.h
balczezzz 5:af96df45a447 34 timer1.reset();
balczezzz 5:af96df45a447 35 counter = counter + 1;
balczezzz 5:af96df45a447 36 printf("id %i",counter);
balczezzz 6:0ef5a242ed00 37 printf(" - time: %i",currentTime);
balczezzz 6:0ef5a242ed00 38 printf(" - revs/min: %i",revMin);
balczezzz 6:0ef5a242ed00 39 printf(" - speed: %4.2f",currentSpeed);
balczezzz 6:0ef5a242ed00 40 printf(" km/h \n");
balczezzz 5:af96df45a447 41 //printf("switch \n\r");
balczezzz 5:af96df45a447 42 }