Simon Barker
/
RPMMonitor
Mbed Fan RPM/Speed meter using QTR-1A Reflectance Sensor
main.cpp@0:812fda2beeaa, 2013-06-23 (annotated)
- Committer:
- simonbarker
- Date:
- Sun Jun 23 10:21:15 2013 +0000
- Revision:
- 0:812fda2beeaa
Commented RPM meter using QTR-1A Reflectance Sensor sensor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simonbarker | 0:812fda2beeaa | 1 | #include "mbed.h" |
simonbarker | 0:812fda2beeaa | 2 | |
simonbarker | 0:812fda2beeaa | 3 | AnalogIn input(p20); |
simonbarker | 0:812fda2beeaa | 4 | DigitalOut led1(LED1); |
simonbarker | 0:812fda2beeaa | 5 | Serial pc(USBTX, USBRX); // tx, rx |
simonbarker | 0:812fda2beeaa | 6 | Timer t; |
simonbarker | 0:812fda2beeaa | 7 | |
simonbarker | 0:812fda2beeaa | 8 | int main() { |
simonbarker | 0:812fda2beeaa | 9 | float sample; |
simonbarker | 0:812fda2beeaa | 10 | bool newDetection = true; //need this as multiple reads happen across white spot |
simonbarker | 0:812fda2beeaa | 11 | int samples[120]; //higher number = greater accuracy but longer read time |
simonbarker | 0:812fda2beeaa | 12 | int sampleCount = 0; |
simonbarker | 0:812fda2beeaa | 13 | |
simonbarker | 0:812fda2beeaa | 14 | pc.printf("Starting:\n\r"); |
simonbarker | 0:812fda2beeaa | 15 | |
simonbarker | 0:812fda2beeaa | 16 | //start initial timer |
simonbarker | 0:812fda2beeaa | 17 | t.start(); |
simonbarker | 0:812fda2beeaa | 18 | |
simonbarker | 0:812fda2beeaa | 19 | while(1) { |
simonbarker | 0:812fda2beeaa | 20 | //poll analogue in |
simonbarker | 0:812fda2beeaa | 21 | sample = input.read(); |
simonbarker | 0:812fda2beeaa | 22 | if(sample < 0.1 && newDetection == true){ //tune 0.1 to appropriate level |
simonbarker | 0:812fda2beeaa | 23 | //detected white spot so stop timer |
simonbarker | 0:812fda2beeaa | 24 | samples[sampleCount] = t.read_us(); |
simonbarker | 0:812fda2beeaa | 25 | t.reset(); |
simonbarker | 0:812fda2beeaa | 26 | //reset flag |
simonbarker | 0:812fda2beeaa | 27 | newDetection = false; |
simonbarker | 0:812fda2beeaa | 28 | sampleCount++; |
simonbarker | 0:812fda2beeaa | 29 | |
simonbarker | 0:812fda2beeaa | 30 | //change for shorter/longer read times - must be less than array length (set in ling 11) |
simonbarker | 0:812fda2beeaa | 31 | if(sampleCount == 100){ |
simonbarker | 0:812fda2beeaa | 32 | //total up time and average across number of readings taken |
simonbarker | 0:812fda2beeaa | 33 | int total = 0; |
simonbarker | 0:812fda2beeaa | 34 | for(int i = 1; i < 99; i++){ //start at 1 as the first one will be a junk reading |
simonbarker | 0:812fda2beeaa | 35 | total += samples[i]; |
simonbarker | 0:812fda2beeaa | 36 | } |
simonbarker | 0:812fda2beeaa | 37 | float ave = total/98; |
simonbarker | 0:812fda2beeaa | 38 | float rpm = (1000000/ave)*60; //convert from us to rpm |
simonbarker | 0:812fda2beeaa | 39 | pc.printf("RPM = %f\r\n",rpm); |
simonbarker | 0:812fda2beeaa | 40 | sampleCount = 0; |
simonbarker | 0:812fda2beeaa | 41 | } |
simonbarker | 0:812fda2beeaa | 42 | } |
simonbarker | 0:812fda2beeaa | 43 | else if(sample > 0.9){ //tune 0.9 to appropriate level |
simonbarker | 0:812fda2beeaa | 44 | newDetection = true; |
simonbarker | 0:812fda2beeaa | 45 | } |
simonbarker | 0:812fda2beeaa | 46 | } |
simonbarker | 0:812fda2beeaa | 47 | } |