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@1:fdcb0bfe75b7, 2020-10-12 (annotated)
- Committer:
- markschwarzer
- Date:
- Mon Oct 12 23:22:33 2020 +0000
- Revision:
- 1:fdcb0bfe75b7
- Parent:
- 0:6b2314a40652
- Child:
- 2:39e295fcc23d
compiles
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
markschwarzer | 0:6b2314a40652 | 1 | //Mark Schwarzer Assignment 4 |
markschwarzer | 0:6b2314a40652 | 2 | |
markschwarzer | 0:6b2314a40652 | 3 | #include "mbed.h" |
markschwarzer | 0:6b2314a40652 | 4 | |
markschwarzer | 0:6b2314a40652 | 5 | Ticker tickerLED1; //creat ticker object |
markschwarzer | 0:6b2314a40652 | 6 | Ticker tickerLED3; |
markschwarzer | 0:6b2314a40652 | 7 | DigitalOut LEDOut1(LED1); |
markschwarzer | 0:6b2314a40652 | 8 | DigitalOut LEDOut3(LED3); |
markschwarzer | 0:6b2314a40652 | 9 | InterruptIn button(p18); |
markschwarzer | 0:6b2314a40652 | 10 | Timer debounce; |
markschwarzer | 0:6b2314a40652 | 11 | |
markschwarzer | 0:6b2314a40652 | 12 | void toggle(void); |
markschwarzer | 0:6b2314a40652 | 13 | |
markschwarzer | 0:6b2314a40652 | 14 | int main() { |
markschwarzer | 0:6b2314a40652 | 15 | debounce.start(); |
markschwarzer | 0:6b2314a40652 | 16 | button.rise(&toggle); //toggle function to rising edge |
markschwarzer | 0:6b2314a40652 | 17 | } |
markschwarzer | 0:6b2314a40652 | 18 | void toggle() { |
markschwarzer | 0:6b2314a40652 | 19 | if (debounce.read_ms()>200); //allow toggle if debounce timer has passed 200 ms |
markschwarzer | 0:6b2314a40652 | 20 | LEDOut1 = !LEDOut1; |
markschwarzer | 0:6b2314a40652 | 21 | debounce.reset(); //restart timer at toggle |
markschwarzer | 0:6b2314a40652 | 22 | |
markschwarzer | 0:6b2314a40652 | 23 | |
markschwarzer | 0:6b2314a40652 | 24 | } |
markschwarzer | 0:6b2314a40652 | 25 | void changeLED1() //the function that will be called by the ticker object. |
markschwarzer | 0:6b2314a40652 | 26 | { |
markschwarzer | 0:6b2314a40652 | 27 | LEDOut1 = !LEDOut1; |
markschwarzer | 0:6b2314a40652 | 28 | } |
markschwarzer | 0:6b2314a40652 | 29 | void changeLED3() //function called for other ticker object LED3 |
markschwarzer | 0:6b2314a40652 | 30 | { |
markschwarzer | 0:6b2314a40652 | 31 | LEDOut3 = !LEDOut3; |
markschwarzer | 0:6b2314a40652 | 32 | } |
markschwarzer | 0:6b2314a40652 | 33 | |
markschwarzer | 1:fdcb0bfe75b7 | 34 | int second() |
markschwarzer | 1:fdcb0bfe75b7 | 35 | { |
markschwarzer | 1:fdcb0bfe75b7 | 36 | tickerLED1.attach(&changeLED1,0.2); //the address of the function to call |
markschwarzer | 1:fdcb0bfe75b7 | 37 | //and the interval in seconds between |
markschwarzer | 1:fdcb0bfe75b7 | 38 | //calls to that function |
markschwarzer | 1:fdcb0bfe75b7 | 39 | tickerLED3.attach(&changeLED3,0.8); //changing the interval in seconds for LED3 |
markschwarzer | 0:6b2314a40652 | 40 | |
markschwarzer | 1:fdcb0bfe75b7 | 41 | while(1) { |
markschwarzer | 1:fdcb0bfe75b7 | 42 | wait(0.1); |
markschwarzer | 1:fdcb0bfe75b7 | 43 | wait(0.1); |
markschwarzer | 1:fdcb0bfe75b7 | 44 | wait(0.1); |
markschwarzer | 1:fdcb0bfe75b7 | 45 | wait(0.1); |
markschwarzer | 1:fdcb0bfe75b7 | 46 | wait(0.1); |
markschwarzer | 1:fdcb0bfe75b7 | 47 | //the main loop is spinning every 500ms, but the LED needs to go faster! |
markschwarzer | 1:fdcb0bfe75b7 | 48 | } //while |
markschwarzer | 1:fdcb0bfe75b7 | 49 | } |
markschwarzer | 1:fdcb0bfe75b7 | 50 | |
markschwarzer | 1:fdcb0bfe75b7 | 51 |