First Draft, serial print change based on distance

Revision:
10:be53044119d1
Parent:
9:ada61082bbaa
Child:
13:cc99df342c7f
diff -r ada61082bbaa -r be53044119d1 main.cpp
--- a/main.cpp	Fri Jan 28 17:12:01 2022 +0000
+++ b/main.cpp	Tue Feb 01 23:10:26 2022 +0000
@@ -151,12 +151,11 @@
     while(1)
     {
     output = fsm[state];  // output current state
-    // printf("state = %d\r\n",state);
+     printf("state = %d\r\n",state);
     
     // check which state we are in and see which the next state should be
     switch(state) {
         case 0:{
-        int sense = 1;
         lcd.clear();
         lcd.printString(" object at 0'",0,0);
         lcd.printString("   R + 60'",0,2);
@@ -172,6 +171,7 @@
             }
 
                 break;}
+                
         case 1:{
         lcd.clear();
         lcd.printString(" object at 60'",0,0);
@@ -181,12 +181,13 @@
             if (R == 1){
                 state = 2;}
             else state = 1;
-                break;}
             
             if (Start_flag == 1) {
                 Start_flag = 0;
                 object_sense2();
             }
+            
+            break;}
         case 2:{
         lcd.clear();
         lcd.printString(" object at 120'",0,0);
@@ -196,12 +197,14 @@
             if (R == 1){
                 state = 3;}
             else state = 2;
-                break;}
                 
             if (Start_flag == 1) {
                 Start_flag = 0;
                 object_sense3();
             }
+            
+            break;}
+            
         case 3:{
         lcd.clear();
         lcd.printString(" object at 180'",0,0);
@@ -211,12 +214,14 @@
             if (R == 1){
                 state = 4;}
             else state = 3;
-                break;}
                 
             if (Start_flag == 1) {
                 Start_flag = 0;
                 object_sense4();
             }
+            
+            break;}
+            
         case 4:{
         lcd.clear();
         lcd.printString(" object at 240'",0,0);
@@ -226,12 +231,14 @@
             if (R == 1){
                 state = 5;}
             else state = 4;
-                break;}
                 
             if (Start_flag == 1) {
                 Start_flag = 0;
                 object_sense5();
             }
+            
+            break;}
+            
          case 5:{
         lcd.clear();
         lcd.printString(" object at 300'",0,0);
@@ -239,20 +246,15 @@
         lcd.printString("   L - 60'",0,4);
         lcd.refresh();
             if (R == 1){
-                state = 6;}
+                object_detection();}
             else state = 5;
-                break;}
+
             if (Start_flag == 1) {
                 Start_flag = 0;
                 object_sense6();
             }
-        case 6:{
-            if (buffer1 - 10 <= glass[1] >= buffer1 + 10 and buffer2 - 10 <= glass[2] >= buffer2 + 10 and buffer3 - 10 <= glass[3] >= buffer3 + 10 and buffer4 - 10 <= glass[4] >= buffer4 + 10 and buffer5 - 10 <= glass[5] >= buffer5 + 10 and buffer6 - 10 <= glass[6] >= buffer6 + 10){       
-                 printf("glass");}
-            else{
-             printf("nothing");}
-             }
-             
+            
+            break;}     
               
             default:{
                 error("Invalid state");  //invalid state - call error routine
@@ -298,61 +300,11 @@
         wait (1);
         }
         }
-        
-void object_sense1(){
-     
-        sense = 1;
-     
-        mu.checkDistance();
-            
-        }
 
-void object_sense2(){
-     
-        sense = 2;
-     
-        mu.checkDistance();
-            
-        }
-        
-void object_sense3(){
-     
-        sense = 3;
-     
-        mu.checkDistance();
-            
-        }
-        
-void object_sense4(){
-     
-        sense = 4;
-     
-        mu.checkDistance();
-            
-        }
-        
-void object_sense5(){
-     
-        sense = 5;
-     
-        mu.checkDistance();
-            
-        }
-        
-void object_sense6(){
-     
-        sense = 6;
-     
-        mu.checkDistance();
-            
-        }
-        
 void dist(int distance)
 {
-     printf("sense = %d", sense);
+     printf("sense = %d\n", sense);
     // printf("Distance changed to %dmm\r\n", distance);
-    switch(sense){
-        case 0:{
     
             lcd.clear();
      
@@ -364,72 +316,98 @@
             lcd.printString("    200mm",0,1);
             lcd.printString(buffer,24,3);
             lcd.refresh();
-            
-            break;}
-            
-        case 1:{
-        
-            printf("sens1");
-            char buffer1[14];
-            int length1 = sprintf(buffer1,"%dmm", distance);
-            if (length1 <= 14)
-            //printf("distance = %d", distance);
-            
-            break;}
-        case 2:{
-        
-            printf("sens2");
-            char buffer2[14];
-            int length2 = sprintf(buffer2,"%dmm", distance);
-            if (length2 <= 14)
-            //printf("distance = %d", distance);
-            
-            break;}
-        case 3:{
-        
-            printf("sens3");
-            char buffer3[14];
-            int length3 = sprintf(buffer3,"%dmm", distance);
-            if (length3 <= 14)
-            //printf("distance = %d", distance);
-            
-            break;}
-
-        case 4:{
-        
-            printf("sens4");
-            char buffer4[14];
-            int length4 = sprintf(buffer4,"%dmm", distance);
-            if (length4 <= 14)
-            //printf("distance = %d", distance);
-            
-            break;}
-            
-        case 5:{
-        
-            printf("sens5");
-            char buffer5[14];
-            int length5 = sprintf(buffer5,"%dmm", distance);
-            if (length5 <= 14)
-            //printf("distance = %d", distance);
-            
-            break;}
-            
-        case 6:{
-        
-            printf("sens6");
-            char buffer6[14];
-            int length6 = sprintf(buffer6,"%dmm", distance);
-            if (length6 <= 14)
-            //printf("distance = %d", distance);
-            
-            break;}
-
-        }         
+     
     }
 
 /***************************************************************************//**
 *
+* this is where the magic happens
+*
+*******************************************************************************/
+
+void object_sense1(){
+     
+        sense = 1;
+     
+        Distance1 = mu.getCurrentDistance();
+
+        printf("distance at sense 1 = %d\r\n", Distance1);
+
+        }
+
+void object_sense2(){
+     
+        sense = 2;
+     
+        Distance2 = mu.getCurrentDistance();
+
+        printf("distance at sense 2 = %d\r\n", Distance2);
+
+        }
+        
+void object_sense3(){
+     
+        sense = 3;
+     
+        Distance3 = mu.getCurrentDistance();
+
+        printf("distance at sense 3 = %d\r\n", Distance3);
+  
+        }
+        
+void object_sense4(){
+     
+        sense = 4;
+     
+        Distance4 = mu.getCurrentDistance();
+
+        printf("distance at sense 4 = %d\r\n", Distance4);
+     
+        }
+        
+void object_sense5(){
+     
+        sense = 5;
+     
+        Distance5 = mu.getCurrentDistance();
+        
+        printf("distance at sense 5 = %d\r\n", Distance5);
+     
+        }
+        
+void object_sense6(){
+     
+        sense = 6;
+     
+        Distance6 = mu.getCurrentDistance();
+        
+        printf("distance at sense 6 = %d\r\n", Distance6);
+ 
+        }
+        
+void object_detection(){
+            
+            lcd.clear();
+    
+            //printf("Distance 1 = %d\n", Distance1);
+            //printf("glass[0] = %d\n", glass[0]);
+            //printf("glass[6] = %d\n", glass[6]);
+            if (abs(Distance1 - glass[0]) <= 10 and abs(Distance2 - glass[1]) <= 10 and abs(Distance3 - glass[2]) <= 10 and abs(Distance4 - glass[3]) <= 10 and abs(Distance5 - glass[4]) <= 10 and abs(Distance6 - glass[5]) <= 10){       
+            printf("glass\n");
+            lcd.printString("Object Found!",0,1);
+            lcd.printString("Pint Glass",0,3);
+            lcd.refresh();}
+                     
+            else{
+            printf("nothing\n");
+            lcd.printString("No Object",0,1);
+            lcd.printString("Please Try",0,3);
+            lcd.printString("Again",24,4);
+            lcd.refresh();}
+            }
+    
+/***************************************************************************//**
+*
 * these functions are for setting the flags for the start and back butons which
 * are used in the other functions.
 *