AUP_Lab3_Interrupt

Dependencies:   mbed

Fork of Lab3_Interrupt by 璇 李

Revision:
0:37b9817db00a
Child:
1:6acc584b5a38
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu May 21 02:23:41 2015 +0000
@@ -0,0 +1,54 @@
+#include "mbed.h"
+
+PwmOut led(D5);
+InterruptIn button_up(A2);
+InterruptIn button_center(D4);
+InterruptIn button_down(A3);
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+double brightness = 1.0;
+double brightness_inc = 0.1;
+
+void press_up()
+{
+    brightness -= brightness_inc;
+}
+
+void press_down()
+{
+    brightness += brightness_inc;
+}
+
+void press_center()
+{
+    brightness = (brightness>0.5)?1.0:0.0;
+}
+
+void serial_in()
+{
+    char ch = getc(pc);
+    if(ch < '0' || ch > '9')
+        pc.printf("Please input a number between 0 to 9.\r\n");
+    else
+        brightness = 1 - (ch - '0') / 10.0;
+}
+
+int main()
+{
+    pc.baud(9600);
+    pc.attach(&serial_in);
+    pc.printf("Hello PWM!\r\n");
+    pc.printf("Please input a number between 0 to 9.\r\n");
+    // Set PWM
+    led.write(brightness);
+    
+    button_up.fall(&press_up);
+    button_down.fall(&press_down);
+    button_center.fall(&press_center);
+
+    while (1) {
+        brightness = (brightness>1.0)?0.0:brightness;
+        brightness = (brightness<0.0)?1.0:brightness;
+        led.write(brightness);
+    }
+}