Demo code minor BioRobotics 2016 University of Twente, Enschede, NL Homework set 1, Exercise 8, Example solution

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
megrootens
Date:
Thu Sep 08 14:15:15 2016 +0000
Commit message:
working answer

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r cdb3d484a242 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Sep 08 14:15:15 2016 +0000
@@ -0,0 +1,63 @@
+/**
+ * Minor BioRobotics 2016
+ *
+ * Programming Homework Set 1
+ * Exercise 8, example solution
+ *
+ * M.E. Grootens [at] utwente.nl,
+ * v0.1, 08.09.2016
+ */
+
+#include "mbed.h"
+
+#define SERIAL_BAUD 115200  // baud rate for serial communication
+
+// Serial communication with PC
+Serial pc(USBTX,USBRX);
+
+// The LED that is to flash
+DigitalOut led(LED_RED);
+
+// Number of times to flash; global and volatile, 'cause modified by interrupt
+volatile int n_times = 0;
+
+/**
+ * Flash the LED
+ * @ensure n_times is increased and then the LED flashes n_times
+ */
+void FlashLed()
+{
+    n_times++;
+    
+    pc.printf("\r\nFlashing %d times:\r\n",n_times);
+    
+    for (int i=0; i<n_times; i++) {
+        pc.printf("[%d]",i);
+        
+        led.write(0);   // on
+        wait(0.1f);     // wait 0.1 s
+        led.write(1);   // off
+        wait(0.4f);     // wait 0.4 s
+    }
+}
+
+
+/**
+ * Main function.
+ */
+int main()
+{
+    // Serial communication
+    pc.baud(SERIAL_BAUD);
+    pc.printf("\r\n**BOARD RESET**\r\n");
+    
+    // LED off
+    led.write(1);
+    
+    // Interrupt; call FlashLed when button pressed
+    InterruptIn sw2(SW2);
+    sw2.fall(&FlashLed);
+    
+    // Infinite loop; does nothing but needs to prevent termination
+    while (true);
+}
\ No newline at end of file
diff -r 000000000000 -r cdb3d484a242 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Sep 08 14:15:15 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/2e9cc70d1897
\ No newline at end of file