Creating a CoxBox with the mbed
Dependencies: mbed C12832 SMARTGPU2
Diff: HallEffect.cpp
- Revision:
- 8:ae9f6033daa0
- Parent:
- 2:552b4c2844ca
diff -r 8815cc152875 -r ae9f6033daa0 HallEffect.cpp --- a/HallEffect.cpp Sun May 04 19:21:45 2014 +0000 +++ b/HallEffect.cpp Sun May 04 20:39:32 2014 +0000 @@ -2,21 +2,24 @@ HallEffect::HallEffect(){ strokeCount = 0; - timeBetweenStrokes[0] = 0; -// interrupt = in; -// interrupt.rise(&hallISR); + for(int i = 0; i < DATA_STROKE_COUNT; i++){ + timeBetweenStrokes[i] = 1; + } + timeIndex = 0; + timer.start(); } -//HallEffect::HallEffect(){ -// strokeCount = 0; -// timeBetweenStrokes[0] = 0; -//// InterruptIn in(p5); -//// interrupt = in; -//// interrupt.rise(&hallISR); -//} - int HallEffect::getSPM(){ - return -1; + int spm = -1; + double sum = 0; + for(int i = 0; i < DATA_STROKE_COUNT; i++){ + sum += timeBetweenStrokes[i]; + } + double secPerStroke = (sum * 2) / DATA_STROKE_COUNT; + double strokesPerSec = 1/secPerStroke; + double strokesPerMin = strokesPerSec*60; + spm = (int)strokesPerMin; + return spm; } int HallEffect::getCount(){ @@ -29,8 +32,9 @@ void HallEffect::recordStroke(){ strokeCount++; -} - -//HallEffect::hallISR(){ -// strokeCount++; -//} \ No newline at end of file + timer.stop(); + timeBetweenStrokes[timeIndex] = timer.read(); + timer.reset(); + timer.start(); + timeIndex = (timeIndex + 1) % DATA_STROKE_COUNT; +} \ No newline at end of file