Demo code minor BioRobotics 2016 University of Twente, Enschede, NL Homework set 1, Exercise 8, Example solution
main.cpp@0:cdb3d484a242, 2016-09-08 (annotated)
- Committer:
- megrootens
- Date:
- Thu Sep 08 14:15:15 2016 +0000
- Revision:
- 0:cdb3d484a242
working answer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
megrootens | 0:cdb3d484a242 | 1 | /** |
megrootens | 0:cdb3d484a242 | 2 | * Minor BioRobotics 2016 |
megrootens | 0:cdb3d484a242 | 3 | * |
megrootens | 0:cdb3d484a242 | 4 | * Programming Homework Set 1 |
megrootens | 0:cdb3d484a242 | 5 | * Exercise 8, example solution |
megrootens | 0:cdb3d484a242 | 6 | * |
megrootens | 0:cdb3d484a242 | 7 | * M.E. Grootens [at] utwente.nl, |
megrootens | 0:cdb3d484a242 | 8 | * v0.1, 08.09.2016 |
megrootens | 0:cdb3d484a242 | 9 | */ |
megrootens | 0:cdb3d484a242 | 10 | |
megrootens | 0:cdb3d484a242 | 11 | #include "mbed.h" |
megrootens | 0:cdb3d484a242 | 12 | |
megrootens | 0:cdb3d484a242 | 13 | #define SERIAL_BAUD 115200 // baud rate for serial communication |
megrootens | 0:cdb3d484a242 | 14 | |
megrootens | 0:cdb3d484a242 | 15 | // Serial communication with PC |
megrootens | 0:cdb3d484a242 | 16 | Serial pc(USBTX,USBRX); |
megrootens | 0:cdb3d484a242 | 17 | |
megrootens | 0:cdb3d484a242 | 18 | // The LED that is to flash |
megrootens | 0:cdb3d484a242 | 19 | DigitalOut led(LED_RED); |
megrootens | 0:cdb3d484a242 | 20 | |
megrootens | 0:cdb3d484a242 | 21 | // Number of times to flash; global and volatile, 'cause modified by interrupt |
megrootens | 0:cdb3d484a242 | 22 | volatile int n_times = 0; |
megrootens | 0:cdb3d484a242 | 23 | |
megrootens | 0:cdb3d484a242 | 24 | /** |
megrootens | 0:cdb3d484a242 | 25 | * Flash the LED |
megrootens | 0:cdb3d484a242 | 26 | * @ensure n_times is increased and then the LED flashes n_times |
megrootens | 0:cdb3d484a242 | 27 | */ |
megrootens | 0:cdb3d484a242 | 28 | void FlashLed() |
megrootens | 0:cdb3d484a242 | 29 | { |
megrootens | 0:cdb3d484a242 | 30 | n_times++; |
megrootens | 0:cdb3d484a242 | 31 | |
megrootens | 0:cdb3d484a242 | 32 | pc.printf("\r\nFlashing %d times:\r\n",n_times); |
megrootens | 0:cdb3d484a242 | 33 | |
megrootens | 0:cdb3d484a242 | 34 | for (int i=0; i<n_times; i++) { |
megrootens | 0:cdb3d484a242 | 35 | pc.printf("[%d]",i); |
megrootens | 0:cdb3d484a242 | 36 | |
megrootens | 0:cdb3d484a242 | 37 | led.write(0); // on |
megrootens | 0:cdb3d484a242 | 38 | wait(0.1f); // wait 0.1 s |
megrootens | 0:cdb3d484a242 | 39 | led.write(1); // off |
megrootens | 0:cdb3d484a242 | 40 | wait(0.4f); // wait 0.4 s |
megrootens | 0:cdb3d484a242 | 41 | } |
megrootens | 0:cdb3d484a242 | 42 | } |
megrootens | 0:cdb3d484a242 | 43 | |
megrootens | 0:cdb3d484a242 | 44 | |
megrootens | 0:cdb3d484a242 | 45 | /** |
megrootens | 0:cdb3d484a242 | 46 | * Main function. |
megrootens | 0:cdb3d484a242 | 47 | */ |
megrootens | 0:cdb3d484a242 | 48 | int main() |
megrootens | 0:cdb3d484a242 | 49 | { |
megrootens | 0:cdb3d484a242 | 50 | // Serial communication |
megrootens | 0:cdb3d484a242 | 51 | pc.baud(SERIAL_BAUD); |
megrootens | 0:cdb3d484a242 | 52 | pc.printf("\r\n**BOARD RESET**\r\n"); |
megrootens | 0:cdb3d484a242 | 53 | |
megrootens | 0:cdb3d484a242 | 54 | // LED off |
megrootens | 0:cdb3d484a242 | 55 | led.write(1); |
megrootens | 0:cdb3d484a242 | 56 | |
megrootens | 0:cdb3d484a242 | 57 | // Interrupt; call FlashLed when button pressed |
megrootens | 0:cdb3d484a242 | 58 | InterruptIn sw2(SW2); |
megrootens | 0:cdb3d484a242 | 59 | sw2.fall(&FlashLed); |
megrootens | 0:cdb3d484a242 | 60 | |
megrootens | 0:cdb3d484a242 | 61 | // Infinite loop; does nothing but needs to prevent termination |
megrootens | 0:cdb3d484a242 | 62 | while (true); |
megrootens | 0:cdb3d484a242 | 63 | } |