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.
main.cpp
- Committer:
- bky
- Date:
- 2020-12-01
- Revision:
- 14:8ceaa97cc85c
- Parent:
- 13:a1a9f2282f15
- Child:
- 16:def6abcb4294
File content as of revision 14:8ceaa97cc85c:
# include "mbed.h"
# include "Timer.h"
# include <time.h>
# define NUM_HOLES 32
# define CIRCUMFRENCE_CM 48.6946861
# define LIGHT_SENSOR_PIN p15
# define FLASH_THRESHOLD 0.02
# define PERIOD_MS 500
AnalogIn lightSensorVoltage(LIGHT_SENSOR_PIN);
Serial pc(USBTX, USBRX);
void sendFloat(float value) {
for (int i=0; i<4; i++)
pc.putc(*(((char*) &value)+i));
}
void displayThread() {
}
int main() {
while(true) {
float minVoltage = 1.1; // voltage will never go above 1.0
int voltagePeaks = 0;
Timer t;
t.start();
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);
}
t.stop();
// calculate velocity
float velocity = (voltagePeaks * CIRCUMFRENCE_CM * 1000) / (NUM_HOLES * PERIOD_MS);
//send
sendFloat(velocity);
}
return 0;
}
