New

Dependencies:   SparkfunAnalogJoystick

Revision:
2:4f5a0c64d9cd
Parent:
1:17ea74f31633
Child:
3:3d54fd4109c0
--- a/main.cpp	Thu Jun 24 19:50:59 2021 +0000
+++ b/main.cpp	Fri Jun 25 00:34:17 2021 +0000
@@ -17,7 +17,7 @@
 int distance3 = 0; // distancia izquierda.
 int distance4 = 0; // distancia derecha. 
 
-int distanceLimit = 10;
+int distanceLimit = 15;
 
 DigitalIn modo1(D8);  //Modo manual
 DigitalIn modo2(D9);  //Modo comandos joystick
@@ -26,6 +26,16 @@
 
 Serial pc(USBTX, USBRX); //Para recibir los comandos de voz.
 
+void flushSerialBuffer(void)
+{
+    char char1 = 0;
+    while (pc.readable())
+    {
+        char1 = pc.getc();
+    }
+    return;
+}
+
 void thread1_HCSR04()
 {
     DigitalOut trigger(D0);
@@ -238,11 +248,11 @@
         {
             X = JoyStick.xAxis();
             Y = JoyStick.yAxis();
-            
+            /*
             printf("X-Axis: %f\n\r", X);
             printf("Y-Axis: %f\n\r", Y);
             printf(" \n\r");
-            
+            */
             if(X >= -0.60f && X  <= 0.60f && Y >= 0.90f && Y <= 1.00f )
             {        
                 if(distance1 > distanceLimit)
@@ -253,7 +263,7 @@
                 {
                     printf("Comandos joystick: Obstaculo adelante \r \n"); 
                 }
-                thread_sleep_for(1000);   
+                thread_sleep_for(500);   
             }
             if(X >= -0.60f && X  <= 0.60f && Y <= -0.90f && Y >= -1.00f)
             {
@@ -265,7 +275,7 @@
                 {
                     printf("Comandos joystick: Obstaculo atras \r \n"); 
                 } 
-                thread_sleep_for(1000); 
+                thread_sleep_for(500); 
             }
             if(Y >= -0.60f && Y  <= 0.60f && X <= -0.90f && X >= -1.00f)
             {
@@ -277,7 +287,7 @@
                 {
                     printf("Comandos joystick: Obstaculo izquierda \r \n"); 
                 }
-                thread_sleep_for(1000);    
+                thread_sleep_for(500);    
             }
             if(Y >= -0.60f && Y  <= 0.60f && X >= 0.90f && X <= 1.00f)
             {
@@ -289,7 +299,7 @@
                 {
                     printf("Comandos joystick: Obstaculo derecha \r \n"); 
                 }
-                thread_sleep_for(1000);   
+                thread_sleep_for(500);
             }
             thread_sleep_for(5); 
         }
@@ -298,60 +308,67 @@
 
 void thread6_ComandosVoz()
 {
-    char c = pc.getc(); 
     while(1)
     {
         if(!modo1 && !modo2 && modo3 && !modo4)
         {
+            flushSerialBuffer();
+            char c = pc.getc();
             if(c == 'w')
             {
-                if(distance1 > distanceLimit )
+                //printf("Distance1 - %d \r \n",distance1);
+                if(distance1 > distanceLimit)
                 {
                     pc.printf(" Comandos de voz: Hacia adelante \r \n");
-                    thread_sleep_for(500);
                 }
                 else
                 {
                     printf(" Comandos de voz: Obstaculo! No se puede ir hacia adelante. \r \n");      
-                }  
-            }        
-            if(c == 'd')
+                }
+                thread_sleep_for(1000);
+            }
+            if(c == 's')
             {
+                //printf("Distance2 - %d \r \n",distance2);
                 if(distance2 > distanceLimit)
                 {
-                    pc.printf(" Comandos de voz: Hacia la derecha \r \n");
-                    thread_sleep_for(500);
+                    pc.printf(" Comandos de voz: Hacia atras \r \n");
                 }
                 else
                 {
                     printf(" Comandos de voz: Obstaculo! No se puede ir hacia atras. \r \n");      
                 }
+                thread_sleep_for(1000);
             }                           
-            if(c == 's')
+            if(c == 'a')
             {
+                //printf("Distance3 - %d \r \n",distance3);
                 if(distance3 > distanceLimit)
                 {
-                    pc.printf(" Comandos de voz: Hacia atrás \r \n");
-                    thread_sleep_for(500);
+                    pc.printf(" Comandos de voz: Hacia la izquierda  \r \n");
                 }
                 else
                 {
                     printf(" Comandos de voz:  Obstaculo! No se puede ir hacia la izquierda. \r \n");      
                 }
+                thread_sleep_for(1000);
             }
-            if(c == 'a')
+            if(c == 'd')
             { 
+                //printf("Distance4 - %d \r \n",distance4);
                 if(distance4 > distanceLimit)
                 {
-                    pc.printf(" Comandos de voz: Hacia la izquierda \r \n");
-                    thread_sleep_for(500);
+                    pc.printf(" Comandos de voz: Hacia la derecha \r \n");
                 } 
                 else
                 {
                     printf(" Comandos de voz:  Obstaculo! No se puede ir hacia la derecha. \r \n");      
-                }               
-            }                 
-        }                        
+                }
+                thread_sleep_for(1000);
+            }
+            c = ' ';
+            thread_sleep_for(5);
+        }
     }
 }
 
@@ -401,10 +418,17 @@
 int main()
 {
     thread1.start(thread1_HCSR04);
+    thread_sleep_for(200);
     thread2.start(thread2_HCSR04);
+    thread_sleep_for(200);
     thread3.start(thread3_HCSR04);
+    thread_sleep_for(200);
     thread4.start(thread4_HCSR04);
+    thread_sleep_for(200);
     thread5.start(thread5_Joystick);
+    thread_sleep_for(200);
     thread6.start(thread6_ComandosVoz);
+    thread_sleep_for(200);
     thread7.start(thread7_IndicarModo);
+    thread_sleep_for(200);
 }
\ No newline at end of file