First Draft, serial print change based on distance

Revision:
2:3ace5b4ae9a7
Parent:
1:a1795335ef8c
Child:
3:0b0fbddb6f51
--- a/main.cpp	Thu Dec 16 11:41:38 2021 +0000
+++ b/main.cpp	Fri Jan 21 13:51:49 2022 +0000
@@ -2,8 +2,63 @@
 #include "ultrasonic.h"
 #include "N5110.h"
 
- void dist(int distance)
+// LEDs on PCB to be connected (1 to 6) - active-low 0 = on and 1 = off
+BusOut output(PTA1,PTA2,PTC2,PTC3,PTC4,PTD3);
+
+// array of states in the FSM, each element is the output of the counter
+// set the output in binary to make it easier, 0 is LED on, 1 is LED off
+int fsm[6] = {0b111110, 0b111101, 0b111011, 0b110111, 0b101111, 0b011111};
+
+void dist(int distance);
+
+
+ultrasonic mu(PTD0, PTC12, .5, 1, &dist);    //Set the trigger pin to PTD0 and the echo pin to PTC12
+                                        //have updates every .5 seconds and a timeout after 1
+                                        //second, and call dist when the distance changes
+
+int main()
 {
+    // set inital state 
+    int state = 0;
+    
+    mu.startUpdates();//start mesuring the distance
+    while(1)
+    {
+output = fsm[state];  // output current state
+        // check which state we are in and see which the next state should be
+        switch(state) {
+            case 0:
+                state = 1;
+                break;
+            case 1:
+                state = 2;
+                break;
+            case 2:
+                state = 3;
+                break;
+            case 3:
+                state = 4;
+                break;
+            case 4:
+                state = 5;
+                break;
+            case 5:
+                state = 0;
+                break;
+            default:
+                error("Invalid state");  //invalid state - call error routine
+                // or could jump to starting state i.e. state = 0
+                break;  
+        }
+        ThisThread::sleep_for(500);
+        
+        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
+                                //the class checks if dist needs to be called.
+    }
+}
+    
+void dist(int distance){
+
     if (distance > 250){
     printf("Safe Distance %dmm\r\n", distance);
     }
@@ -20,21 +75,6 @@
     printf("Very Very Close %dmm\r\n", distance);
     }
     else if (distance <= 40 and distance > 0){
-    printf("You've Hit a Wall %dmm\r\n", distance);
+    printf("You've Hit a Wall %dmm\r\n", distance);  
     }
-}
-
-ultrasonic mu(PTD0, PTC12, .1, 1, &dist);    //Set the trigger pin to PTD0 and the echo pin to PTC12
-                                        //have updates every .1 seconds and a timeout after 1
-                                        //second, and call dist when the distance changes
-
-int main()
-{
-    mu.startUpdates();//start mesuring the distance
-    while(1)
-    {
-        //Do something else here
-        mu.checkDistance();     //call checkDistance() as much as possible, as this is where
-                                //the class checks if dist needs to be called.
-    }
-}
+    }
\ No newline at end of file