sonar sensor is working in the version of code but the reading are not correct

Dependencies:   C12832 Pulse RangeFinder Servo mbed rtos

Fork of Team_Sprint2 by WIT_EmbOS_Gr1

Revision:
19:582c5a0c868c
Parent:
18:974430ee2fbb
--- a/main.cpp	Mon Apr 20 13:31:33 2015 +0000
+++ b/main.cpp	Thu Apr 23 14:34:02 2015 +0000
@@ -14,8 +14,6 @@
 Mutex mutexOut;// protect globel variables
 Mutex mutex_sonar;
 AnalogIn sonar(p19); // temporary changed to potmeter
-Mutex mutexIn;
-Mutex mutexOut;
 
 // Global variables
 float corHoriz = 0; // horizontal variable arrives from OpenCV
@@ -36,13 +34,24 @@
 100000 Time to wait for a pulse from the range finder before giving up
 */
 
-RangeFinder rf(p26, 10, 5800.0, 100000);
+RangeFinder rf(p21, 10, 5800.0, 100000);
 /* Thread Serial 1 - handles the output data from the control thread, and pass to the servo.
     @update s1, s2 */
 void serial_thread(void const *args)
 {
     while (true) {
+     //    mutexIn.lock();
+      //   mutexOut.lock();
+      
+       //if not range finding
+       //mutex_sonar.lock();
+       if(pc.readable())
         pc.scanf("%f,%f", &corHoriz, &corVert);// read from serial port the data
+   
+       //mutex_sonar.unlock();
+       //mutexIn.unlock();
+       //mutexOut.unlock();
+        Thread::wait(1000);
     }
 }
 
@@ -53,12 +62,18 @@
     while (true) {
         mutex_sonar.lock();
         // Display values on the LCD screen
-        lcd.cls();          // clear the display
-        lcd.locate(0,5);    // the location where you want your charater to be displayed
-
+       // lcd.cls();          // clear the display
+         lcd.locate(0,0);    // the location where you want your charater to be displayed
         lcd.printf("differ: %0.3f, OutTilt: %0.3f", differ, outTilt);
+        
+       // lcd.cls();          // clear the display
+       lcd.locate(0,10);    // the location where you want your charater to be displayed
+        lcd.printf("Vert: %0.3f, OutVert: %0.3f", corVert, outVert);
+        
+        //lcd.cls();          // clear the display
         lcd.locate(0,20);    // the location where you want your charater to be displayed
-        lcd.printf("Vert: %0.3f, OutVert: %0.3f", corVert, outVert);
+        lcd.printf("dis: %0.2f\n\r", distance);// Display the distance in meters from the sonar
+        mutex_sonar.unlock();
         Thread::wait(250);
     }
 }
@@ -113,19 +128,21 @@
         mutex_sonar.lock();
         distance = rf.read_m(); // read the distance from the sonar sensor in meter
         norm= distance/3.3;     // normalised value from the sonar sensor
-        printf("dis: %0.2f", distance);// Display the distance in meters from the sonar
+       // lcd.cls();          // clear the display
+       // lcd.locate(0,5);    // the location where you want your charater to be displayed
+        printf("dis: %0.2f\n\r", distance);// Display the distance in meters from the sonar
         mutex_sonar.unlock();
         Thread::wait(250);
     }
 }
 
 int main() {
-    Thread thread_1(serial_thread); // Start Serial Thread
-    Thread thread_2(lcd_thread); // Start LCD Thread
+  Thread thread_1(serial_thread); // Start Serial Thread
+   Thread thread_2(lcd_thread); // Start LCD Thread
     Thread thread_3(control_thread); // Start Servo Thread
-    Thread thread_4(servo_thread); // Start Servo Thread
+   Thread thread_4(servo_thread); // Start Servo Thread
     Thread thread_5(sonar_thread); // Start Servo Thread
     while(1) {     
-        wait(1);
+        Thread::wait(1);
     }
 }