Workshop 1

Dependencies:   PM2_Libary

Committer:
kiefesev
Date:
Wed Mar 16 07:44:32 2022 +0000
Revision:
19:ac26dab01633
Parent:
18:21de1a131213
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kiefesev 19:ac26dab01633 1 //Workshop1
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 18:21de1a131213 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 user_button_timer.start();
pmic 16:2de2a437afdc 39 main_task_timer.start();
pmic 16:2de2a437afdc 40
pmic 16:2de2a437afdc 41 // set pullup mode: add resistor between pin and 3.3 V, so that there is a defined potential
pmic 16:2de2a437afdc 42 mechanical_button.mode(PullUp);
pmic 16:2de2a437afdc 43
pmic 16:2de2a437afdc 44 while (true) { // this loop will run forever
pmic 16:2de2a437afdc 45
pmic 16:2de2a437afdc 46 main_task_timer.reset();
pmic 16:2de2a437afdc 47
pmic 16:2de2a437afdc 48 if (do_execute_main_task) {
pmic 13:5d689f89d794 49
pmic 16:2de2a437afdc 50 // read analog input
pmic 18:21de1a131213 51 // ???
pmic 13:5d689f89d794 52
pmic 17:085fff8287d0 53 // if the mechanical button is pressed the extra led is blinking
pmic 16:2de2a437afdc 54 if (mechanical_button) {
pmic 17:085fff8287d0 55 // visual feedback that the main task is executed
pmic 17:085fff8287d0 56 extra_led = !extra_led;
pmic 16:2de2a437afdc 57 } else {
pmic 17:085fff8287d0 58 extra_led = 1;
pmic 16:2de2a437afdc 59 }
pmic 14:db46f47b0480 60
boro 0:5d4d21d56334 61 } else {
pmic 16:2de2a437afdc 62
pmic 16:2de2a437afdc 63 ir_distance_mV = 0.0f;
pmic 16:2de2a437afdc 64
pmic 16:2de2a437afdc 65 extra_led = 0;
boro 0:5d4d21d56334 66 }
pmic 13:5d689f89d794 67
pmic 16:2de2a437afdc 68 user_led = !user_led;
pmic 13:5d689f89d794 69
pmic 16:2de2a437afdc 70 // do only output via serial what's really necessary (this makes your code slow)
pmic 17:085fff8287d0 71 printf("IR sensor (mV): %3.3f\r\n", ir_distance_mV);
pmic 16:2de2a437afdc 72
pmic 16:2de2a437afdc 73 // read timer and make the main thread sleep for the remaining time span (non blocking)
pmic 16:2de2a437afdc 74 int main_task_elapsed_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(main_task_timer.elapsed_time()).count();
pmic 16:2de2a437afdc 75 thread_sleep_for(main_task_period_ms - main_task_elapsed_time_ms);
boro 0:5d4d21d56334 76 }
boro 0:5d4d21d56334 77 }
pmic 13:5d689f89d794 78
pmic 16:2de2a437afdc 79 void user_button_pressed_fcn()
pmic 1:4e0e4d0363d9 80 {
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 }