Hello there

Dependencies:   PM2_Libary

Committer:
pmic
Date:
Tue Mar 15 10:40:22 2022 +0100
Revision:
18:21de1a131213
Parent:
17:085fff8287d0
Child:
19:22e264bd960d
Version for students.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boro 0:5d4d21d56334 1 #include "mbed.h"
pmic 16:2de2a437afdc 2 #include "PM2_Libary.h"
pmic 16:2de2a437afdc 3
pmic 16:2de2a437afdc 4 // logical variable main task
pmic 16:2de2a437afdc 5 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 6
pmic 16:2de2a437afdc 7 // user button on nucleo board
pmic 16:2de2a437afdc 8 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 9 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 10 void user_button_pressed_fcn(); // custom functions which gets executed when user button gets pressed and released, definition below
pmic 16:2de2a437afdc 11 void user_button_released_fcn();
pmic 16:2de2a437afdc 12
pmic 16:2de2a437afdc 13 // while loop gets executed every main_task_period_ms milliseconds
pmic 16:2de2a437afdc 14 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 15 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 16
pmic 16:2de2a437afdc 17 // led on nucleo board
pmic 16:2de2a437afdc 18 DigitalOut user_led(LED1); // create DigitalOut object to command user led
pmic 16:2de2a437afdc 19
pmic 16:2de2a437afdc 20 // additional Led
pmic 16:2de2a437afdc 21 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 22
pmic 16:2de2a437afdc 23 // mechanical button
pmic 16:2de2a437afdc 24 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 25
pmic 16:2de2a437afdc 26 // Sharp GP2Y0A41SK0F, 4-40 cm IR Sensor
pmic 16:2de2a437afdc 27 float ir_distance_mV = 0.0f; // define variable to store measurement
pmic 18:21de1a131213 28 // ??? // create AnalogIn object to read in infrared distance sensor, 0...3.3V are mapped to 0...1
pmic 16:2de2a437afdc 29
boro 0:5d4d21d56334 30 int main()
boro 0:5d4d21d56334 31 {
pmic 16:2de2a437afdc 32 // attach button fall and rise functions to user button object
pmic 16:2de2a437afdc 33 user_button.fall(&user_button_pressed_fcn);
pmic 16:2de2a437afdc 34 user_button.rise(&user_button_released_fcn);
pmic 16:2de2a437afdc 35
pmic 16:2de2a437afdc 36 // start timers
pmic 16:2de2a437afdc 37 user_button_timer.start();
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 18:21de1a131213 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 1:4e0e4d0363d9 80 user_button_timer.reset();
pmic 1:4e0e4d0363d9 81 }
pmic 13:5d689f89d794 82
pmic 16:2de2a437afdc 83 void user_button_released_fcn()
pmic 1:4e0e4d0363d9 84 {
pmic 16:2de2a437afdc 85 // read timer and toggle do_execute_main_task if the button was pressed longer than the below specified time
pmic 16:2de2a437afdc 86 int user_button_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 87 user_button_timer.stop();
pmic 16:2de2a437afdc 88 if (user_button_elapsed_time_ms > 200) {
pmic 16:2de2a437afdc 89 do_execute_main_task = !do_execute_main_task;
pmic 13:5d689f89d794 90 }
pmic 6:6c1c38d4faa4 91 }