Liam Decaster
/
PM2_Example_IRSensor
Hello there
main.cpp@1:4e0e4d0363d9, 2021-03-12 (annotated)
- Committer:
- pmic
- Date:
- Fri Mar 12 15:50:14 2021 +0000
- Revision:
- 1:4e0e4d0363d9
- Parent:
- 0:5d4d21d56334
- Child:
- 2:4ba1937ce284
First commit.
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 | 1:4e0e4d0363d9 | 4 | #define pi 3.14159265358979323846 |
pmic | 1:4e0e4d0363d9 | 5 | |
pmic | 1:4e0e4d0363d9 | 6 | InterruptIn user_button(USER_BUTTON); |
pmic | 1:4e0e4d0363d9 | 7 | DigitalOut led(LED1); |
pmic | 1:4e0e4d0363d9 | 8 | Serial pc(SERIAL_TX, SERIAL_RX); |
boro | 0:5d4d21d56334 | 9 | |
pmic | 1:4e0e4d0363d9 | 10 | bool executeMainTask = false; |
pmic | 1:4e0e4d0363d9 | 11 | Timer user_button_timer, loop_timer; |
pmic | 1:4e0e4d0363d9 | 12 | int Ts_ms = 50; |
boro | 0:5d4d21d56334 | 13 | |
pmic | 1:4e0e4d0363d9 | 14 | void button_fall(); |
pmic | 1:4e0e4d0363d9 | 15 | void button_rise(); |
boro | 0:5d4d21d56334 | 16 | |
pmic | 1:4e0e4d0363d9 | 17 | AnalogIn analogIn(PA_0); |
pmic | 1:4e0e4d0363d9 | 18 | float dist = 0.0f; |
boro | 0:5d4d21d56334 | 19 | |
boro | 0:5d4d21d56334 | 20 | int main() |
boro | 0:5d4d21d56334 | 21 | { |
pmic | 1:4e0e4d0363d9 | 22 | pc.baud(115200); |
pmic | 1:4e0e4d0363d9 | 23 | user_button.fall(&button_fall); |
pmic | 1:4e0e4d0363d9 | 24 | user_button.rise(&button_rise); |
pmic | 1:4e0e4d0363d9 | 25 | loop_timer.reset(); |
boro | 0:5d4d21d56334 | 26 | |
boro | 0:5d4d21d56334 | 27 | while (true) { |
pmic | 1:4e0e4d0363d9 | 28 | |
pmic | 1:4e0e4d0363d9 | 29 | /* ------------- start hacking ------------- -------------*/ |
boro | 0:5d4d21d56334 | 30 | |
pmic | 1:4e0e4d0363d9 | 31 | if(executeMainTask) { |
pmic | 1:4e0e4d0363d9 | 32 | dist = analogIn.read()*3.3f; |
pmic | 1:4e0e4d0363d9 | 33 | printf("measurement: %9.6f\r\n", dist); |
boro | 0:5d4d21d56334 | 34 | } else { |
boro | 0:5d4d21d56334 | 35 | |
boro | 0:5d4d21d56334 | 36 | } |
pmic | 1:4e0e4d0363d9 | 37 | |
pmic | 1:4e0e4d0363d9 | 38 | /* ------------- stop hacking ------------- -------------*/ |
pmic | 1:4e0e4d0363d9 | 39 | |
pmic | 1:4e0e4d0363d9 | 40 | if(executeMainTask) { |
pmic | 1:4e0e4d0363d9 | 41 | led = !led; |
pmic | 1:4e0e4d0363d9 | 42 | } |
pmic | 1:4e0e4d0363d9 | 43 | int dT_loop = Ts_ms - loop_timer.read_ms(); |
pmic | 1:4e0e4d0363d9 | 44 | thread_sleep_for(dT_loop); |
boro | 0:5d4d21d56334 | 45 | } |
boro | 0:5d4d21d56334 | 46 | } |
pmic | 1:4e0e4d0363d9 | 47 | |
pmic | 1:4e0e4d0363d9 | 48 | void button_fall() |
pmic | 1:4e0e4d0363d9 | 49 | { |
pmic | 1:4e0e4d0363d9 | 50 | user_button_timer.reset(); |
pmic | 1:4e0e4d0363d9 | 51 | user_button_timer.start(); |
pmic | 1:4e0e4d0363d9 | 52 | } |
pmic | 1:4e0e4d0363d9 | 53 | |
pmic | 1:4e0e4d0363d9 | 54 | void button_rise() |
pmic | 1:4e0e4d0363d9 | 55 | { |
pmic | 1:4e0e4d0363d9 | 56 | int t_button = user_button_timer.read_ms(); |
pmic | 1:4e0e4d0363d9 | 57 | user_button_timer.stop(); |
pmic | 1:4e0e4d0363d9 | 58 | if(t_button > 200) executeMainTask = !executeMainTask; |
pmic | 1:4e0e4d0363d9 | 59 | } |