Samuel Rusterholz
/
PM2_Example_IRSensor
Workshop 1
main.cpp@21:4b708e9bfbf4, 2022-03-18 (annotated)
- Committer:
- ruesti1299
- Date:
- Fri Mar 18 07:42:39 2022 +0000
- Revision:
- 21:4b708e9bfbf4
- Parent:
- 20:c52e148d933f
First commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ruesti1299 | 21:4b708e9bfbf4 | 1 | // Workshop 1 |
boro | 0:5d4d21d56334 | 2 | #include "mbed.h" |
pmic | 16:2de2a437afdc | 3 | #include "PM2_Libary.h" |
pmic | 16:2de2a437afdc | 4 | |
pmic | 16:2de2a437afdc | 5 | // logical variable main task |
pmic | 16:2de2a437afdc | 6 | bool do_execute_main_task = false; // this variable will be toggled via the user button (blue button) to or not to execute the main task |
pmic | 16:2de2a437afdc | 7 | |
pmic | 16:2de2a437afdc | 8 | // user button on nucleo board |
pmic | 16:2de2a437afdc | 9 | Timer user_button_timer; // create Timer object which we use to check if user button was pressed for a certain time (robust against signal bouncing) |
pmic | 16:2de2a437afdc | 10 | InterruptIn user_button(PC_13); // create InterruptIn interface object to evaluate user button falling and rising edge (no blocking code in ISR) |
pmic | 16:2de2a437afdc | 11 | void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below |
pmic | 16:2de2a437afdc | 12 | void user_button_released_fcn(); |
pmic | 16:2de2a437afdc | 13 | |
pmic | 16:2de2a437afdc | 14 | // while loop gets executed every main_task_period_ms milliseconds |
pmic | 16:2de2a437afdc | 15 | int main_task_period_ms = 50; // define main task period time in ms e.g. 50 ms -> main task runns 20 times per second |
pmic | 16:2de2a437afdc | 16 | Timer main_task_timer; // create Timer object which we use to run the main task every main task period time in ms |
pmic | 13:5d689f89d794 | 17 | |
pmic | 16:2de2a437afdc | 18 | // led on nucleo board |
pmic | 16:2de2a437afdc | 19 | DigitalOut user_led(LED1); // create DigitalOut object to command user led |
pmic | 16:2de2a437afdc | 20 | |
pmic | 16:2de2a437afdc | 21 | // additional Led |
pmic | 16:2de2a437afdc | 22 | DigitalOut extra_led(PB_9); // create DigitalOut object to command extra led (do add an aditional resistor, e.g. 220...500 Ohm) |
pmic | 16:2de2a437afdc | 23 | |
pmic | 16:2de2a437afdc | 24 | // mechanical button |
pmic | 16:2de2a437afdc | 25 | DigitalIn mechanical_button(PC_5); // create DigitalIn object to evaluate extra mechanical button, you need to specify the mode for proper usage, see below |
pmic | 16:2de2a437afdc | 26 | |
pmic | 16:2de2a437afdc | 27 | // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor |
pmic | 16:2de2a437afdc | 28 | float ir_distance_mV = 0.0f; // define variable to store measurement |
pmic | 20:c52e148d933f | 29 | // ??? // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1 |
pmic | 16:2de2a437afdc | 30 | |
boro | 0:5d4d21d56334 | 31 | int main() |
boro | 0:5d4d21d56334 | 32 | { |
pmic | 16:2de2a437afdc | 33 | // attach button fall and rise functions to user button object |
pmic | 16:2de2a437afdc | 34 | user_button.fall(&user_button_pressed_fcn); |
pmic | 16:2de2a437afdc | 35 | user_button.rise(&user_button_released_fcn); |
pmic | 16:2de2a437afdc | 36 | |
pmic | 16:2de2a437afdc | 37 | // start timers |
pmic | 16:2de2a437afdc | 38 | main_task_timer.start(); |
pmic | 16:2de2a437afdc | 39 | |
pmic | 16:2de2a437afdc | 40 | // set pullup mode: add resistor between pin and 3.3 V, so that there is a defined potential |
pmic | 16:2de2a437afdc | 41 | mechanical_button.mode(PullUp); |
pmic | 16:2de2a437afdc | 42 | |
pmic | 16:2de2a437afdc | 43 | while (true) { // this loop will run forever |
pmic | 16:2de2a437afdc | 44 | |
pmic | 16:2de2a437afdc | 45 | main_task_timer.reset(); |
pmic | 16:2de2a437afdc | 46 | |
pmic | 16:2de2a437afdc | 47 | if (do_execute_main_task) { |
pmic | 13:5d689f89d794 | 48 | |
pmic | 16:2de2a437afdc | 49 | // read analog input |
pmic | 20:c52e148d933f | 50 | // ??? |
pmic | 13:5d689f89d794 | 51 | |
pmic | 17:085fff8287d0 | 52 | // if the mechanical button is pressed the extra led is blinking |
pmic | 16:2de2a437afdc | 53 | if (mechanical_button) { |
pmic | 17:085fff8287d0 | 54 | // visual feedback that the main task is executed |
pmic | 17:085fff8287d0 | 55 | extra_led = !extra_led; |
pmic | 16:2de2a437afdc | 56 | } else { |
pmic | 17:085fff8287d0 | 57 | extra_led = 1; |
pmic | 16:2de2a437afdc | 58 | } |
pmic | 14:db46f47b0480 | 59 | |
boro | 0:5d4d21d56334 | 60 | } else { |
pmic | 16:2de2a437afdc | 61 | |
pmic | 16:2de2a437afdc | 62 | ir_distance_mV = 0.0f; |
pmic | 16:2de2a437afdc | 63 | |
pmic | 16:2de2a437afdc | 64 | extra_led = 0; |
boro | 0:5d4d21d56334 | 65 | } |
pmic | 13:5d689f89d794 | 66 | |
pmic | 16:2de2a437afdc | 67 | user_led = !user_led; |
pmic | 13:5d689f89d794 | 68 | |
pmic | 16:2de2a437afdc | 69 | // do only output via serial what's really necessary (this makes your code slow) |
pmic | 17:085fff8287d0 | 70 | printf("IR sensor (mV): %3.3f\r\n", ir_distance_mV); |
pmic | 16:2de2a437afdc | 71 | |
pmic | 16:2de2a437afdc | 72 | // read timer and make the main thread sleep for the remaining time span (non blocking) |
pmic | 16:2de2a437afdc | 73 | int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count(); |
pmic | 16:2de2a437afdc | 74 | thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms); |
boro | 0:5d4d21d56334 | 75 | } |
boro | 0:5d4d21d56334 | 76 | } |
pmic | 13:5d689f89d794 | 77 | |
pmic | 16:2de2a437afdc | 78 | void user_button_pressed_fcn() |
pmic | 1:4e0e4d0363d9 | 79 | { |
pmic | 19:22e264bd960d | 80 | user_button_timer.start(); |
pmic | 1:4e0e4d0363d9 | 81 | user_button_timer.reset(); |
pmic | 1:4e0e4d0363d9 | 82 | } |
pmic | 13:5d689f89d794 | 83 | |
pmic | 16:2de2a437afdc | 84 | void user_button_released_fcn() |
pmic | 1:4e0e4d0363d9 | 85 | { |
pmic | 16:2de2a437afdc | 86 | // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time |
pmic | 16:2de2a437afdc | 87 | int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 1:4e0e4d0363d9 | 88 | user_button_timer.stop(); |
pmic | 16:2de2a437afdc | 89 | if (user_button_elapsed_time_ms > 200) { |
pmic | 16:2de2a437afdc | 90 | do_execute_main_task = !do_execute_main_task; |
pmic | 13:5d689f89d794 | 91 | } |
pmic | 6:6c1c38d4faa4 | 92 | } |