Liam Decaster
/
PM2_Example_IRSensor
Hello there
Diff: main.cpp
- Revision:
- 13:5d689f89d794
- Parent:
- 11:cfc9b8e963db
- Child:
- 14:db46f47b0480
--- a/main.cpp Fri Apr 02 10:24:30 2021 +0200 +++ b/main.cpp Tue Apr 06 12:49:57 2021 +0200 @@ -1,61 +1,69 @@ #include "mbed.h" #include "platform/mbed_thread.h" - + using namespace std::chrono; - -InterruptIn user_button(USER_BUTTON); -DigitalOut led(LED1); - -bool executeMainTask = false; -Timer user_button_timer, loop_timer; -int Ts_ms = 50; - -void button_fall(); -void button_rise(); + +InterruptIn user_button(USER_BUTTON); +DigitalOut led(LED1); + +bool executeMainTask = false; +Timer user_button_timer, loop_timer; +int Ts_ms = 50; -/* input your stuff here */ +/* declaration of custom button functions */ +void button_fall(); +void button_rise(); - +/* create analog input object */ +AnalogIn analogIn(PA_0); +float dist = 0.0f; + int main() { user_button.fall(&button_fall); user_button.rise(&button_rise); loop_timer.start(); - + while (true) { - + loop_timer.reset(); + + /* ------------- start hacking ------------- -------------*/ + + if (executeMainTask) { + + /* read analog input */ + dist = analogIn.read()*3.3f; - /* ------------- start hacking ------------- -------------*/ - - if(executeMainTask) { - - + /* do only output what's really necessary, outputting "Measured value in mV: "" within the loop is no good solution */ + printf("Measured value in mV: %d\r\n", (static_cast<int>(dist * 1e3))); /* visual feedback that the main task is executed */ led = !led; - + } else { led = 0; } - + /* ------------- stop hacking ------------- -------------*/ - + int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); int dT_loop_ms = Ts_ms - T_loop_ms; thread_sleep_for(dT_loop_ms); } } - + void button_fall() { user_button_timer.reset(); user_button_timer.start(); } - + void button_rise() { - int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); + int t_button_ms = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); user_button_timer.stop(); - if(t_button > 200) executeMainTask = !executeMainTask; + if (t_button_ms > 200) { + executeMainTask = !executeMainTask; + } } \ No newline at end of file