Liam Decaster
/
PM2_Example_IRSensor
Hello there
main.cpp@8:49f2b6a515ac, 2021-03-13 (annotated)
- Committer:
- pmic
- Date:
- Sat Mar 13 16:53:22 2021 +0100
- Revision:
- 8:49f2b6a515ac
- Parent:
- 7:dc463bf54be6
- Child:
- 9:c5e1e1facb02
Solution with printf (blocking for approx. 11 ms) .
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
boro | 0:5d4d21d56334 | 1 | #include "mbed.h" |
boro | 0:5d4d21d56334 | 2 | #include "platform/mbed_thread.h" |
pmic | 1:4e0e4d0363d9 | 3 | |
pmic | 3:aa1d854807fe | 4 | using namespace std::chrono; |
pmic | 3:aa1d854807fe | 5 | |
pmic | 7:dc463bf54be6 | 6 | InterruptIn user_button(USER_BUTTON); |
pmic | 7:dc463bf54be6 | 7 | DigitalOut led(LED1); |
boro | 0:5d4d21d56334 | 8 | |
pmic | 7:dc463bf54be6 | 9 | bool executeMainTask = false; |
pmic | 7:dc463bf54be6 | 10 | Timer user_button_timer, loop_timer; |
pmic | 7:dc463bf54be6 | 11 | int Ts_ms = 50; |
boro | 0:5d4d21d56334 | 12 | |
pmic | 7:dc463bf54be6 | 13 | void button_fall(); |
pmic | 7:dc463bf54be6 | 14 | void button_rise(); |
pmic | 5:887081decd5c | 15 | |
pmic | 7:dc463bf54be6 | 16 | AnalogIn analogIn(PA_0); |
pmic | 7:dc463bf54be6 | 17 | float dist = 0.0f; |
pmic | 6:6c1c38d4faa4 | 18 | |
boro | 0:5d4d21d56334 | 19 | int main() |
boro | 0:5d4d21d56334 | 20 | { |
pmic | 1:4e0e4d0363d9 | 21 | user_button.fall(&button_fall); |
pmic | 1:4e0e4d0363d9 | 22 | user_button.rise(&button_rise); |
pmic | 6:6c1c38d4faa4 | 23 | loop_timer.start(); |
boro | 0:5d4d21d56334 | 24 | |
boro | 0:5d4d21d56334 | 25 | while (true) { |
pmic | 1:4e0e4d0363d9 | 26 | |
pmic | 6:6c1c38d4faa4 | 27 | loop_timer.reset(); |
pmic | 6:6c1c38d4faa4 | 28 | |
pmic | 1:4e0e4d0363d9 | 29 | /* ------------- start hacking ------------- -------------*/ |
boro | 0:5d4d21d56334 | 30 | |
pmic | 1:4e0e4d0363d9 | 31 | if(executeMainTask) { |
pmic | 4:dcdcb25d1069 | 32 | |
pmic | 6:6c1c38d4faa4 | 33 | dist = analogIn.read()*3.3f; |
pmic | 4:dcdcb25d1069 | 34 | |
pmic | 8:49f2b6a515ac | 35 | printf("Measval: %d\r\n", (static_cast<int>(dist * 1000))); |
pmic | 4:dcdcb25d1069 | 36 | |
pmic | 6:6c1c38d4faa4 | 37 | /* visual feedback that the main task is executed */ |
pmic | 6:6c1c38d4faa4 | 38 | led = !led; |
pmic | 4:dcdcb25d1069 | 39 | |
boro | 0:5d4d21d56334 | 40 | } else { |
pmic | 6:6c1c38d4faa4 | 41 | led = 0; |
boro | 0:5d4d21d56334 | 42 | } |
pmic | 1:4e0e4d0363d9 | 43 | |
pmic | 1:4e0e4d0363d9 | 44 | /* ------------- stop hacking ------------- -------------*/ |
pmic | 1:4e0e4d0363d9 | 45 | |
pmic | 6:6c1c38d4faa4 | 46 | int T_loop_ms = duration_cast<milliseconds>(loop_timer.elapsed_time()).count(); |
pmic | 6:6c1c38d4faa4 | 47 | int dT_loop_ms = Ts_ms - T_loop_ms; |
pmic | 7:dc463bf54be6 | 48 | thread_sleep_for(dT_loop_ms); |
boro | 0:5d4d21d56334 | 49 | } |
boro | 0:5d4d21d56334 | 50 | } |
pmic | 1:4e0e4d0363d9 | 51 | |
pmic | 1:4e0e4d0363d9 | 52 | void button_fall() |
pmic | 1:4e0e4d0363d9 | 53 | { |
pmic | 1:4e0e4d0363d9 | 54 | user_button_timer.reset(); |
pmic | 1:4e0e4d0363d9 | 55 | user_button_timer.start(); |
pmic | 1:4e0e4d0363d9 | 56 | } |
pmic | 1:4e0e4d0363d9 | 57 | |
pmic | 1:4e0e4d0363d9 | 58 | void button_rise() |
pmic | 1:4e0e4d0363d9 | 59 | { |
pmic | 3:aa1d854807fe | 60 | int t_button = duration_cast<milliseconds>(user_button_timer.elapsed_time()).count(); |
pmic | 1:4e0e4d0363d9 | 61 | user_button_timer.stop(); |
pmic | 1:4e0e4d0363d9 | 62 | if(t_button > 200) executeMainTask = !executeMainTask; |
pmic | 6:6c1c38d4faa4 | 63 | } |