Brendon Ky / Mbed 2 deprecated cs_335_speedometer

Dependencies:   mbed mbed-rtos

Revision:
11:dd860c2e12ec
Parent:
10:8d86e59fb408
Child:
12:cb9118a081fa
--- a/main.cpp	Tue Dec 01 02:05:33 2020 +0000
+++ b/main.cpp	Tue Dec 01 04:29:39 2020 +0000
@@ -3,12 +3,14 @@
 
 # include <stdlib.h>
 # include <time.h>
+# include <limits>
+# include <queue>
 
 # define NUM_HOLES 32
 # define CIRCUMFRENCE_CM 48.6946861
 
 # define LIGHT_SENSOR_PIN p15
-# define FLASH_THRESHOLD 0.93
+# define FLASH_THRESHOLD 0.97
 
 
 AnalogIn lightSensorVoltage(LIGHT_SENSOR_PIN);
@@ -19,7 +21,19 @@
 int lastFlash = 0;
 float prevVoltReading = 0.0;
 float currVoltReading = 0.0;
-float rotationsPerMili = 0.0;
+float miliPerRotation = std::numeric_limits<float>::max();
+
+//float getAverage(queue<float> q) {
+//printf("g");
+//    float sum = 0;
+//    while(!q.empty()) {
+//printf("h");
+//        sum += q.front();
+//        q.pop();
+//    }
+//    
+//    return sum / q.size();
+//}
 
 void sendFloat(float value) {
     for (int i=0; i<4; i++)
@@ -34,19 +48,25 @@
     
     float velocity = 0;
     
+    sendFloat(0);
     while(1) {
         currVoltReading = lightSensorVoltage.read();
-        if(prevVoltReading < FLASH_THRESHOLD && currVoltReading >= FLASH_THRESHOLD) {
+//printf("volt %f\n", currVoltReading);
+        if(currVoltReading - prevVoltReading > 0.02) {
             currTime = timer.read_ms();
-            rotationsPerMili = (currTime - prevTime) / NUM_HOLES;
+//printf("currTime %i\n", currTime);
+//printf("prevTime %i\n", prevTime);
+            miliPerRotation = (currTime - prevTime) * NUM_HOLES;
+//printf("time diff %i\n", currTime - prevTime);
+//printf("MPRs %f\n", miliPerRotation);
             
             prevTime = currTime;
-            velocity = CIRCUMFRENCE_CM * rotationsPerMili * 1000;
+            velocity = CIRCUMFRENCE_CM * 1000 / miliPerRotation;
+            
+            sendFloat(velocity);
         }
-        
+//printf("velocity %f\n", velocity);
         prevVoltReading = currVoltReading;
-        
-        sendFloat(velocity);
-        wait(0.05);
+        //wait(0.05);
     }
 }
\ No newline at end of file