Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@5:ac0eea152e50, 2019-09-18 (annotated)
- Committer:
- noutram
- Date:
- Wed Sep 18 10:45:11 2019 +0000
- Revision:
- 5:ac0eea152e50
- Parent:
- 4:f30ca79f4676
2019
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:397b84c74d17 | 1 | #include "mbed.h" |
| noutram | 0:397b84c74d17 | 2 | |
| noutram | 0:397b84c74d17 | 3 | #define N 1000000 |
| noutram | 0:397b84c74d17 | 4 | #define RELEASED 0 |
| noutram | 0:397b84c74d17 | 5 | #define PRESSED 1 |
| noutram | 0:397b84c74d17 | 6 | |
| noutram | 0:397b84c74d17 | 7 | //Hardware objects |
| noutram | 0:397b84c74d17 | 8 | DigitalOut red_led(PE_15); //CountUp is in its critical section |
| noutram | 0:397b84c74d17 | 9 | DigitalOut yellow_led(PB_10); //CountDown is in its critical section |
| noutram | 0:397b84c74d17 | 10 | DigitalOut green_led(PB_11); //counter != 0 |
| noutram | 0:397b84c74d17 | 11 | DigitalOut onboardLED(LED1); |
| noutram | 0:397b84c74d17 | 12 | |
| noutram | 0:397b84c74d17 | 13 | DigitalIn button(USER_BUTTON); |
| noutram | 0:397b84c74d17 | 14 | DigitalIn sw1(PE_12); |
| noutram | 0:397b84c74d17 | 15 | DigitalIn sw2(PE_14); |
| noutram | 0:397b84c74d17 | 16 | |
| noutram | 2:ca251bdda621 | 17 | //The code below is hugely flawed and is only to |
| noutram | 2:ca251bdda621 | 18 | //illustrate the problem of blocking hardware |
| noutram | 4:f30ca79f4676 | 19 | |
| noutram | 4:f30ca79f4676 | 20 | // TASK - add some code to address the problem of switch bounce |
| noutram | 4:f30ca79f4676 | 21 | |
| noutram | 0:397b84c74d17 | 22 | int main() { |
| noutram | 0:397b84c74d17 | 23 | |
| noutram | 2:ca251bdda621 | 24 | //Light up |
| noutram | 2:ca251bdda621 | 25 | red_led = 1; |
| noutram | 0:397b84c74d17 | 26 | yellow_led = 1; |
| noutram | 2:ca251bdda621 | 27 | green_led = 1; |
| noutram | 2:ca251bdda621 | 28 | onboardLED = 1; |
| noutram | 2:ca251bdda621 | 29 | |
| noutram | 0:397b84c74d17 | 30 | //Now loop forever |
| noutram | 0:397b84c74d17 | 31 | while(1) { |
| noutram | 3:a39db8aa11e8 | 32 | |
| noutram | 3:a39db8aa11e8 | 33 | while (sw1 == RELEASED) {}; |
| noutram | 3:a39db8aa11e8 | 34 | while (sw1 == PRESSED) {}; |
| noutram | 3:a39db8aa11e8 | 35 | red_led = !red_led; |
| noutram | 2:ca251bdda621 | 36 | |
| noutram | 3:a39db8aa11e8 | 37 | while (sw2 == RELEASED) {}; |
| noutram | 3:a39db8aa11e8 | 38 | while (sw2 == PRESSED) {}; |
| noutram | 3:a39db8aa11e8 | 39 | green_led = !green_led; |
| noutram | 2:ca251bdda621 | 40 | |
| noutram | 2:ca251bdda621 | 41 | //Flash the yellow |
| noutram | 2:ca251bdda621 | 42 | yellow_led = !yellow_led; |
| noutram | 2:ca251bdda621 | 43 | wait(0.5); |
| noutram | 0:397b84c74d17 | 44 | }; |
| noutram | 0:397b84c74d17 | 45 | } |
| noutram | 0:397b84c74d17 | 46 |