Different led blinks based on different position of the board

Dependencies:   BSP_B-L475E-IOT01 mbed

Files at this revision

API Documentation at this revision

Comitter:
iandil
Date:
Wed Nov 14 13:08:58 2018 +0000
Parent:
0:fe91cf93b0c9
Commit message:
Finished Exercise

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Nov 14 12:13:53 2018 +0000
+++ b/main.cpp	Wed Nov 14 13:08:58 2018 +0000
@@ -4,6 +4,21 @@
 DigitalOut led1(LED1);
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
+
+void blink_led(int led1On, int led2On, int led3On, int reset, DigitalOut led = LED4) {
+    led1 = led1On;
+    led2 = led2On;
+    led3 = led3On;
+    wait(1);
+    if(reset) {
+        led1 = 0;    
+        led2 = 0;
+        led3 = 0;
+    } else {
+        led = !led;
+    }
+}
+
 int main()
 {
     int16_t pDataXYZ[3] = {0};
@@ -16,32 +31,14 @@
         printf("ACCELERO_Y = %d\n", pDataXYZ[1]);
         printf("ACCELERO_Z = %d\n", pDataXYZ[2]);
         
-        if(pDataXYZ[2] > 900 || pDataXYZ[2] < -900) {
-            led1 = 1;
-            led2 = 0;
-            led3 = 0;
-            wait(1);
-            led1 = 0;
-        } else if(pDataXYZ[1] > 900 || pDataXYZ[1] < -900) {
-            led2 = 1;
-            led1 = 0;
-            led3 = 0;
-            wait(1);
-            led2 = 0;
-        } else if(pDataXYZ[0] > 900 || pDataXYZ[0] < -900) {
-            led3 = 1;
-            led2 = 0;
-            led1 = 0;
-            wait(1);
-            led3 = 0;
-        } else {
-            led1 = 1;
-            led2 = 1;
-            led3 = 1;
-            wait(1);
-            led1 = 0;
-            led2 = 0;
-            led3 = 0;   
+        if(pDataXYZ[2] > 900 || pDataXYZ[2] < -900) { //Horizontal State
+            blink_led(1, 0, 0, 0, led1);
+        } else if(pDataXYZ[1] > 900 || pDataXYZ[1] < -900) { //Long Edge State
+            blink_led(0, 1, 0, 0, led2);
+        } else if(pDataXYZ[0] > 900 || pDataXYZ[0] < -900) { // Short Edge State
+            blink_led(0, 0, 1, 0, led3);
+        } else { // all other positions
+            blink_led(1, 1, 1, 1);
         }
     }
 }
\ No newline at end of file