lab2-3

Dependencies:   mbed

Revision:
0:644ab7067a27
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Nov 23 14:50:07 2017 +0000
@@ -0,0 +1,78 @@
+#include "mbed.h"
+BusOut led0(D2, D3, D4, D5,D6, D7, D8, D9);
+BusOut myleds(D2,D3,D4,D5);
+BusOut myleda(D6,D7,D8,D9);
+AnalogIn analog_value(A1);
+DigitalIn B1(USER_BUTTON);
+int main()
+{
+    int state=0;
+    float meas;
+    int decimal; //หลักหน่วย
+    int unit; //หลักทศ
+    while(1) {
+        if(B1 == 0) {
+        //    wait_ms(10)
+            while(B1 !=1);
+
+            if(state <1) {
+                state++;
+            } 
+            else {
+                state =0;
+            }
+        }
+        if(state == 0) {
+            meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) แปลงแรงดันจาก 0-3.3 เป็นดิจิตอล 0.0-1.0
+            meas = meas * 3300; // Change the value to be in the 0 to 3300 range
+            //pc.printf("%f",meas);
+            //pc.printf("\n");
+             if (meas < 10 )
+             {
+                 led0 = 0;
+                 }
+            else if (meas > 50 && meas < 412.5 ) { // If the value is greater than 2V then switch the LED on
+              led0 = 1;
+              }
+            else if (meas >= 412.5 && meas < 825.0) { // If the value is greater than 2V then switch the LED on
+              led0 = 3;
+              }
+            else if (meas >= 825.0 && meas < 1237.5){
+              led0 = 7;
+              }
+            else if (meas >= 1650.0 && meas < 2062.5){
+              led0 = 15;  
+              }
+            else if (meas >= 2062.5 && meas < 2475.0){
+              led0 = 31;
+              }
+            else if (meas >=2475.0 && meas < 2887.5){
+              led0 = 63;
+              }
+            else if (meas >= 2887.5 && meas < 3300.0){
+              led0 = 127;
+              }
+            else if (meas >= 3300.0){
+              led0 = 255;
+            }
+            wait(0.2); // 200 ms
+            } 
+        else if(state ==1) {
+            meas = analog_value.read(); // Converts and read the analog input value (value from 0.0 to 1.0) แปลงแรงดันจาก 0-3.3 เป็นดิจิตอล 0.0-1.0
+            //meas = analog_value.read();
+            meas = meas * 33; // Change the value to be in the 0 to 3300 range
+            decimal = meas;
+            unit = meas;
+            decimal = decimal % 10;
+            unit = unit-decimal;
+            unit = unit % 100;
+            unit = unit / 10;
+            myleds = decimal;
+            myleda = unit;
+            //pc.printf("%d",unit); 
+            //pc.printf("%d",decimal);
+            //pc.printf("\n");
+            wait(0.1);
+            }
+    }
+}
\ No newline at end of file