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:
- 11:dd860c2e12ec
- Parent:
- 10:8d86e59fb408
- Child:
- 12:cb9118a081fa
File content as of revision 11:dd860c2e12ec:
# 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
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;
float velocity = 0;
sendFloat(0);
while(1) {
currVoltReading = lightSensorVoltage.read();
//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;
velocity = CIRCUMFRENCE_CM * 1000 / miliPerRotation;
sendFloat(velocity);
}
//printf("velocity %f\n", velocity);
prevVoltReading = currVoltReading;
//wait(0.05);
}
}
