ECEM119 assignment 2 problem 4

Revision:
0:44f80f935e35
Child:
1:eee108fd0b4b
diff -r 000000000000 -r 44f80f935e35 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Apr 17 21:40:16 2019 +0000
@@ -0,0 +1,51 @@
+/* mbed Microcontroller Library
+ * Copyright (c) 2018 ARM Limited
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include "mbed.h"
+#include "stats_report.h"
+//
+PwmOut output(PTA10);
+DigitalIn input(PTD9);
+Timer timer1;
+Timer timer2;
+
+Serial pc(USBTX, USBRX); //create a Serial object
+
+int main() {
+    int period_ms = 500;
+    output.period_ms(period_ms);
+    output.write(0.5);
+    timer1.start();
+    timer2.start();
+     
+    int prev = 0;
+    int count = 0;
+    int t = 0;
+    float d = 0;
+    int time_on = 0;
+   
+    while(1) {
+        pc.baud(115200);
+        int curr = input.read();
+        if (curr) {
+            if (prev == 0) {
+                timer2.reset();
+                if (count == 0) {
+                    t = timer1.read_us();
+                    d = float(time_on) / t;
+                    pc.printf("mbed> %i, %f\n", t, d);
+                }
+                count = (count + 1) % 10;
+                timer1.reset();
+            }
+            prev = 1;
+        } else {
+            if (prev == 1) {
+                time_on = timer2.read_us();
+            }
+            prev = 0;
+        }
+    }
+}