Brendon Ky / Mbed 2 deprecated cs_335_speedometer

Dependencies:   mbed mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 # include "mbed.h"
00002 # include "rtos.h"
00003 # include "Timer.h"
00004 # include "displayDriver.h"
00005 
00006 # include <time.h>
00007 # define NUM_HOLES 32
00008 # define CIRCUMFRENCE_CM 48.6946861
00009 
00010 # define LIGHT_SENSOR_PIN p18
00011 # define FLASH_THRESHOLD 0.02
00012 # define PERIOD_MS 500
00013 # define RUNTIME 120000 // 2 mins
00014 
00015 
00016 AnalogIn lightSensorVoltage(LIGHT_SENSOR_PIN);
00017 Serial pc(USBTX, USBRX);
00018 
00019 float velocity = 0.0; // used by display thread
00020 
00021 void sendFloat(float value) {
00022     for (int i=0; i<4; i++)
00023         pc.putc(*(((char*) &value)+i));
00024 }
00025 
00026 void displayThread_cb() {
00027     while(velocity >= 0) {
00028         display_num(velocity);
00029     }
00030     return;
00031 }
00032 
00033 int main() {
00034     Thread displayThread;
00035     displayThread.start(displayThread_cb);
00036     
00037     Timer t0;
00038     t0.start();
00039     
00040     while(t0.read_ms() < RUNTIME) {
00041         float minVoltage = 1.1; // voltage will never go above 1.0
00042         int voltagePeaks = 0;
00043         
00044         Timer t1;
00045         t1.start();
00046         
00047         while(t1.read_ms() < PERIOD_MS) {
00048             float currVoltage = lightSensorVoltage.read();
00049             if(currVoltage < minVoltage) {
00050                 minVoltage = currVoltage;
00051             } else if (currVoltage - minVoltage > FLASH_THRESHOLD) {
00052                 voltagePeaks++;
00053                 minVoltage = 1.1;
00054             }
00055             wait(0.01);
00056         }
00057         
00058         t1.stop();
00059         
00060         // calculate velocity
00061         velocity = (voltagePeaks * CIRCUMFRENCE_CM * 1000) / (NUM_HOLES * PERIOD_MS);
00062         
00063         //send 
00064         sendFloat(velocity);
00065     }
00066     
00067     t0.stop();
00068     velocity = -1; // tells the display thread to stop
00069     displayThread.join();
00070     
00071     return 0;    
00072 }