Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 14:8ceaa97cc85c
- Parent:
- 13:a1a9f2282f15
- Child:
- 16:def6abcb4294
--- a/main.cpp Tue Dec 01 05:17:25 2020 +0000
+++ b/main.cpp Tue Dec 01 09:08:37 2020 +0000
@@ -1,81 +1,53 @@
# include "mbed.h"
# include "Timer.h"
-# 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.97
+# define FLASH_THRESHOLD 0.02
+# define PERIOD_MS 500
AnalogIn lightSensorVoltage(LIGHT_SENSOR_PIN);
Serial pc(USBTX, USBRX);
-DigitalOut myled(LED1);
-
-int lastFlash = 0;
-float prevVoltReading = 0.0;
-float currVoltReading = 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++)
pc.putc(*(((char*) &value)+i));
}
-int main() {
- Timer timer;
- timer.start();
- int currTime = 0;
- int prevTime = 0;
+void displayThread() {
- float currVelocity = 0;
- float prevVelocity = 0;
-
- sendFloat(0);
- while(1) {
- currVoltReading = lightSensorVoltage.read();
+}
+
+int main() {
+ while(true) {
+ float minVoltage = 1.1; // voltage will never go above 1.0
+ int voltagePeaks = 0;
- prevVelocity = currVelocity;
+ Timer t;
+ t.start();
-//printf("volt %f\n", currVoltReading);
- if(currVoltReading - prevVoltReading > 0.02) {
- currTime = timer.read_ms();
-//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;
- currVelocity = CIRCUMFRENCE_CM * 1000 / miliPerRotation;
+ while(t.read_ms() < PERIOD_MS) {
+ float currVoltage = lightSensorVoltage.read();
+ if(currVoltage < minVoltage) {
+ minVoltage = currVoltage;
+ } else if (currVoltage - minVoltage > FLASH_THRESHOLD) {
+ voltagePeaks++;
+ minVoltage = 1.1;
+ }
+ wait(0.01);
}
- if(currVelocity != 0 && currVelocity == prevVelocity) {
- currVelocity = 0;
- }
+ t.stop();
- sendFloat(currVelocity);
+ // calculate velocity
+ float velocity = (voltagePeaks * CIRCUMFRENCE_CM * 1000) / (NUM_HOLES * PERIOD_MS);
-//printf("velocity %f\n", velocity);
- prevVoltReading = currVoltReading;
- wait(0.05);
+ //send
+ sendFloat(velocity);
}
+ return 0;
}
\ No newline at end of file
