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:
- 10:8d86e59fb408
- Parent:
- 9:b7e8d0372f53
- Child:
- 11:dd860c2e12ec
--- a/main.cpp Tue Dec 01 00:20:30 2020 +0000
+++ b/main.cpp Tue Dec 01 02:05:33 2020 +0000
@@ -1,29 +1,25 @@
-#include "mbed.h"
+# include "mbed.h"
+# include "Timer.h"
-#include <time.h>
-# include <stdbool.h>
+# include <stdlib.h>
+# include <time.h>
+
+# define NUM_HOLES 32
+# define CIRCUMFRENCE_CM 48.6946861
# define LIGHT_SENSOR_PIN p15
+# define FLASH_THRESHOLD 0.93
-
AnalogIn lightSensorVoltage(LIGHT_SENSOR_PIN);
Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);
-float voltage0 = 0;
-float voltage1 = 0;
-
-bool flashDetected() {
- bool flashDetected = (voltage1 < voltage0) && (voltage0 > lightSensorVoltage.read());
-
- // update stored values
- voltage1 = voltage0;
- voltage0 = lightSensorVoltage;
-
- return flashDetected;
-}
+int lastFlash = 0;
+float prevVoltReading = 0.0;
+float currVoltReading = 0.0;
+float rotationsPerMili = 0.0;
void sendFloat(float value) {
for (int i=0; i<4; i++)
@@ -31,11 +27,26 @@
}
int main() {
- //int flashTime = 0;
- printf("starting\n");
+ Timer timer;
+ timer.start();
+ int currTime = 0;
+ int prevTime = 0;
+
+ float velocity = 0;
+
while(1) {
- sendFloat(lightSensorVoltage.read());
- wait(0.1);
+ currVoltReading = lightSensorVoltage.read();
+ if(prevVoltReading < FLASH_THRESHOLD && currVoltReading >= FLASH_THRESHOLD) {
+ currTime = timer.read_ms();
+ rotationsPerMili = (currTime - prevTime) / NUM_HOLES;
+
+ prevTime = currTime;
+ velocity = CIRCUMFRENCE_CM * rotationsPerMili * 1000;
+ }
+
+ prevVoltReading = currVoltReading;
+
+ sendFloat(velocity);
+ wait(0.05);
}
-}
-
+}
\ No newline at end of file
