Workshop 1

Dependencies:   PM2_Libary

Committer:
pmic
Date:
Thu Feb 10 12:13:50 2022 +0000
Revision:
15:a2c188ed294f
Parent:
14:db46f47b0480
Child:
16:2de2a437afdc
Minor adjustments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
boro 0:5d4d21d56334 1 #include "mbed.h"
pmic 13:5d689f89d794 2
pmic 15:a2c188ed294f 3 InterruptIn user_button(PC_13);
pmic 13:5d689f89d794 4 DigitalOut led(LED1);
pmic 13:5d689f89d794 5
pmic 13:5d689f89d794 6 bool executeMainTask = false;
pmic 13:5d689f89d794 7 Timer user_button_timer, loop_timer;
pmic 13:5d689f89d794 8 int Ts_ms = 50;
pmic 5:887081decd5c 9
pmic 13:5d689f89d794 10 /* declaration of custom button functions */
pmic 13:5d689f89d794 11 void button_fall();
pmic 13:5d689f89d794 12 void button_rise();
pmic 11:cfc9b8e963db 13
pmic 13:5d689f89d794 14 /* create analog input object */
pmic 15:a2c188ed294f 15 AnalogIn analogIn(PC_2);
pmic 15:a2c188ed294f 16 float dist_IRSensor = 0.0f;
pmic 13:5d689f89d794 17
boro 0:5d4d21d56334 18 int main()
boro 0:5d4d21d56334 19 {
pmic 1:4e0e4d0363d9 20 user_button.fall(&button_fall);
pmic 1:4e0e4d0363d9 21 user_button.rise(&button_rise);
pmic 6:6c1c38d4faa4 22 loop_timer.start();
pmic 13:5d689f89d794 23
boro 0:5d4d21d56334 24 while (true) {
pmic 13:5d689f89d794 25
pmic 6:6c1c38d4faa4 26 loop_timer.reset();
pmic 13:5d689f89d794 27
pmic 13:5d689f89d794 28 /* ------------- start hacking ------------- -------------*/
pmic 13:5d689f89d794 29
pmic 13:5d689f89d794 30 if (executeMainTask) {
pmic 13:5d689f89d794 31
pmic 13:5d689f89d794 32 /* read analog input */
pmic 15:a2c188ed294f 33 dist_IRSensor = analogIn.read() * 3.3f;
pmic 14:db46f47b0480 34
pmic 13:5d689f89d794 35 /* do only output what's really necessary, outputting "Measured value in mV: "" within the loop is no good solution */
pmic 15:a2c188ed294f 36 printf("Measured value in mV: %d\r\n", (static_cast<int>(dist_IRSensor * 1e3)));
pmic 11:cfc9b8e963db 37
pmic 6:6c1c38d4faa4 38 /* visual feedback that the main task is executed */
pmic 6:6c1c38d4faa4 39 led = !led;
pmic 13:5d689f89d794 40
boro 0:5d4d21d56334 41 } else {
pmic 6:6c1c38d4faa4 42 led = 0;
boro 0:5d4d21d56334 43 }
pmic 13:5d689f89d794 44
pmic 1:4e0e4d0363d9 45 /* ------------- stop hacking ------------- -------------*/
pmic 13:5d689f89d794 46
pmic 15:a2c188ed294f 47 int T_loop_ms = std::chrono::duration_cast<std::chrono::milliseconds>(loop_timer.elapsed_time()).count();
pmic 6:6c1c38d4faa4 48 int dT_loop_ms = Ts_ms - T_loop_ms;
pmic 7:dc463bf54be6 49 thread_sleep_for(dT_loop_ms);
boro 0:5d4d21d56334 50 }
boro 0:5d4d21d56334 51 }
pmic 13:5d689f89d794 52
pmic 1:4e0e4d0363d9 53 void button_fall()
pmic 1:4e0e4d0363d9 54 {
pmic 1:4e0e4d0363d9 55 user_button_timer.reset();
pmic 1:4e0e4d0363d9 56 user_button_timer.start();
pmic 1:4e0e4d0363d9 57 }
pmic 13:5d689f89d794 58
pmic 1:4e0e4d0363d9 59 void button_rise()
pmic 1:4e0e4d0363d9 60 {
pmic 15:a2c188ed294f 61 int t_button_ms = std::chrono::duration_cast<std::chrono::milliseconds>(user_button_timer.elapsed_time()).count();
pmic 1:4e0e4d0363d9 62 user_button_timer.stop();
pmic 13:5d689f89d794 63 if (t_button_ms > 200) {
pmic 13:5d689f89d794 64 executeMainTask = !executeMainTask;
pmic 13:5d689f89d794 65 }
pmic 6:6c1c38d4faa4 66 }