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.
Diff: main.cpp
- Revision:
- 0:f1c27b98a650
- Child:
- 1:1ce199c334c2
diff -r 000000000000 -r f1c27b98a650 main.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Mon Mar 09 18:23:33 2020 +0000
@@ -0,0 +1,69 @@
+#include "mbed.h"
+
+// Sample answer for lab 2
+// -----------------------
+// Poll button
+// Vary period of flashing LED
+
+DigitalIn button(PTD0, PullUp);
+DigitalOut led(LED_RED);
+
+Thread pollT ; // thread to poll
+Thread flashT ; // thread to flash light
+volatile int event = 0 ;
+
+enum switchPos { up, down, bounce };
+void polling() {
+ switchPos pos = up ;
+ int bcounter = 0 ;
+ while (true) {
+ switch (pos) {
+ case up :
+ if (button == 1) {
+ event = 1 ; // transition occurred
+ pos = down ;
+ }
+ break ;
+ case down :
+ if (button == 0) {
+ bcounter = 3 ; // wait four cycles
+ pos = bounce ;
+ }
+ break ;
+ case bounce :
+ if (button == 1) { // button has bounced
+ pos = down ; // no event
+ } else if (bcounter == 0) {
+ pos = up ;
+ } else {
+ bcounter-- ;
+ }
+ break ;
+ }
+ ThisThread::sleep_for(100) ; // delay for 100ms
+ }
+}
+
+// Main thread
+// Start polling thread
+// Flash light with a period that varies with each button press
+int main()
+{
+ pollT.start(&polling) ; // start the polling thread running
+ int delay = 1 ; // range 1, 2, 3, 4, 5
+ int counter = 0 ; // counter for cycles since last flash
+ while(true) {
+ counter++ ; // increment the counter
+ if (event) {
+ delay = (delay % 5) + 1 ;
+ event = 0 ; // clear the event
+ }
+ if (counter >= 2 * delay) { // 2, 4, 6, 8, 10
+ led = !led ;
+ counter = 0 ;
+ }
+ ThisThread::sleep_for(100) ; // delay for 100ms
+ }
+
+
+}
\ No newline at end of file