Our Programm to build up from the PES Board Example

Dependencies:   PM2_Libary

Committer:
oneelia01
Date:
Fri Mar 25 21:13:49 2022 +0000
Revision:
23:4a0912c80739
Parent:
21:f13e39291d80
Child:
24:b45ebc28cdf0
First Commit Test

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