Assignment 3 and 4. Both in commits.

Dependencies:   MODSERIAL mbed

Revision:
0:86aaef96170e
Child:
1:958fbf7ad850
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 02 12:33:55 2015 +0000
@@ -0,0 +1,55 @@
+#include "mbed.h"
+#include "MODSERIAL.h"
+
+DigitalOut led(LED_GREEN);
+DigitalIn button(PTA4);
+MODSERIAL pc(USBTX, USBRX);
+
+const int baudrate = 115200;
+const int ms_wait = 100;
+
+const float period_led = 0.1;
+const float t_on = 0.1;
+const float t_off = period_led - t_on;
+
+const int led_on = 0;
+const int led_off = 1;
+
+const int button_pressed = 0;
+
+
+void blink(int number_of_blinks)
+{
+    for(int i = 0; i < 5 ; i++) {
+        led.write(led_on); //turn led on
+        wait(t_on);
+        led.write(led_off); // toggle led
+        wait(t_off);
+    }
+}
+
+int main()
+{
+    int waittime = 0;
+    led.write(led_off);
+    pc.baud(baudrate);
+    pc.printf("Hello World!\n");
+    
+    while (true) {
+        wait_ms(ms_wait); //const value
+        if(button.read() == button_pressed) //button pressed
+        {
+            waittime++;
+            pc.printf("Pressed for %d ms\n", waittime * ms_wait);  //const used again -> updating it at the top gives consistent results!!
+        }
+        else //button released
+        {
+            waittime = 0;
+        }
+        if(pc.readable()) //if character available. If expresseion is non-zero, it's true
+        {
+            pc.rxBufferFlush();    //flush the buffer. Otherwise pc.readable() will stay larger than zero
+            blink(1);
+        }
+    }
+}