test reed switch for bike cadence & speed

Dependencies:   mbed

Fork of Blinking Led by Icarus Sensors

Committer:
balczezzz
Date:
Tue May 05 07:00:50 2015 +0000
Revision:
7:d6844dacbd2e
Parent:
6:0ef5a242ed00
reed switch small tweaks

Who changed what in which revision?

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