First Draft, serial print change based on distance

Revision:
4:77500a7f951d
Parent:
3:0b0fbddb6f51
Child:
5:98845ccaaacd
diff -r 0b0fbddb6f51 -r 77500a7f951d main.cpp
--- a/main.cpp	Fri Jan 21 14:25:05 2022 +0000
+++ b/main.cpp	Sat Jan 22 15:28:18 2022 +0000
@@ -8,6 +8,9 @@
 // LEDs on PCB to be connected (1 to 6) - active-low 0 = on and 1 = off
 BusOut output(PTA1,PTA2,PTC2,PTC3,PTC4,PTD3);
 
+// VCC,SCE,RST,D/C,MOSI,SCLK,LED
+N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3
+
 // 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};
@@ -25,48 +28,83 @@
     int state = 0;
     R.mode(PullDown);
     L.mode(PullDown);
+    lcd.init();
+    lcd.setContrast(0.4);
     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:
+    output = fsm[state];  // output current state
+    lcd.clear(); // clear buffer at start of every loop
+// can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
+    
+// check which state we are in and see which the next state should be
+    switch(state) {
+        case 0:
+        lcd.clear();
+        lcd.printString(" object at 0'",0,0);
+        lcd.printString("   R + 60'",0,2);
+        lcd.printString("   L - 60'",0,4);
+        lcd.refresh();
             if (R == 1){
                 state = 1;}
             else if (L == 1){
                 state = 5;}
             else state = 0;
                 break;
-            case 1:
+        case 1:
+        lcd.clear();
+        lcd.printString(" object at 60'",0,0);
+        lcd.printString("   R + 60'",0,2);
+        lcd.printString("   L - 60'",0,4);
+        lcd.refresh();
             if (R == 1){
                 state = 2;}
             else if (L == 1){
                 state = 0;}
             else state = 1;
                 break;
-            case 2:
+        case 2:
+        lcd.clear();
+        lcd.printString(" object at 120'",0,0);
+        lcd.printString("   R + 60'",0,2);
+        lcd.printString("   L - 60'",0,4);
+        lcd.refresh();
             if (R == 1){
                 state = 3;}
             else if (L == 1){
                 state = 1;}
             else state = 2;
                 break;
-            case 3:
+        case 3:
+        lcd.clear();
+        lcd.printString(" object at 180'",0,0);
+        lcd.printString("   R + 60'",0,2);
+        lcd.printString("   L - 60'",0,4);
+        lcd.refresh();
             if (R == 1){
                 state = 4;}
             else if (L == 1){
                 state = 2;}
             else state = 3;
                 break;
-            case 4:
+        case 4:
+        lcd.clear();
+        lcd.printString(" object at 240'",0,0);
+        lcd.printString("   R + 60'",0,2);
+        lcd.printString("   L - 60'",0,4);
+        lcd.refresh();
             if (R == 1){
                 state = 5;}
             else if (L == 1){
                 state = 3;}
             else state = 4;
                 break;
-            case 5:
+         case 5:
+        lcd.clear();
+        lcd.printString(" object at 300'",0,0);
+        lcd.printString("   R + 60'",0,2);
+        lcd.printString("   L - 60'",0,4);
+        lcd.refresh();
             if (R == 1){
                 state = 0;}
             else if (L == 1){
@@ -78,7 +116,7 @@
                 // or could jump to starting state i.e. state = 0
                 break;  
         }
-        ThisThread::sleep_for(500);
+        ThisThread::sleep_for(100);
         
         mu.checkDistance();     //call checkDistance() as much as possible, as this is where
                                 //the class checks if dist needs to be called.
@@ -90,19 +128,4 @@
     if (distance > 250){
     printf("Safe Distance %dmm\r\n", distance);
     }
-    else if (distance <= 250 and distance > 150){
-    printf("object detected %dmm\r\n", distance);
-    }
-    else if (distance <= 150 and distance > 80){
-    printf("object detected Closer %dmm\r\n", distance);
-    }
-    else if (distance <= 80 and distance > 60){
-    printf("Even Closer %dmm\r\n", distance);
-    }
-    else if (distance <= 60 and distance > 40){
-    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);  
-    }
     }
\ No newline at end of file