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:
- 10:8d86e59fb408
- Parent:
- 9:b7e8d0372f53
- Child:
- 11:dd860c2e12ec
File content as of revision 10:8d86e59fb408:
# include "mbed.h"
# include "Timer.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);
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++)
pc.putc(*(((char*) &value)+i));
}
int main() {
Timer timer;
timer.start();
int currTime = 0;
int prevTime = 0;
float velocity = 0;
while(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);
}
}
