PARALLAX sonar threaded

Dependencies:   C12832_lcd Pulse RangeFinder mbed-rtos mbed

Fork of rtos_basic by mbed official

Revision:
7:cb446dbb54d9
Parent:
3:c92e21f305d8
--- a/main.cpp	Tue Jun 04 16:01:32 2013 +0100
+++ b/main.cpp	Mon Mar 23 14:58:40 2015 +0000
@@ -1,21 +1,78 @@
+#include "rtos.h"
 #include "mbed.h"
-#include "rtos.h"
- 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
- 
-void led2_thread(void const *args) {
+#include "C12832_lcd.h"
+#include "RangeFinder.h"
+
+// Seeed ultrasound range finder
+RangeFinder rf(p21, 10, 5800.0, 100000);
+BusOut myleds(LED1, LED2, LED3, LED4);
+C12832_LCD lcd;
+
+float sonar_data;
+
+
+void lcd_display(void const *args) {
     while (true) {
-        led2 = !led2;
-        Thread::wait(1000);
+        lcd.locate(0,0);
+        if (sonar_data == -1.0)  {
+            lcd.printf("Timeout Error.\n");   
+        } else if (sonar_data > 5.0) {  
+            lcd.printf("No object within detection range.\n");         
+        } else  {
+            lcd.printf("Distance = %f m.\n", sonar_data);
+        }
+        Thread::wait(250);
     }
 }
- 
-int main() {
-    Thread thread(led2_thread);
+
+void led_display(void const *args) {
+    while (true) {
+            float led_m = 0.790474;
+        if (sonar_data <= led_m)  
+            {
+             myleds = 15;
+             }
+        else if (sonar_data > led_m && sonar_data <= 2*led_m ) 
+            {
+            myleds = 7;      
+            }
+        else if (sonar_data > 2*led_m && sonar_data <= 3*led_m )  
+            {
+            myleds = 3;      
+            }
+        else  
+            {
+            myleds = 1;      
+            }
     
-    while (true) {
-        led1 = !led1;
-        Thread::wait(500);
+        Thread::wait(250);
     }
 }
+
+void serial_display(void const *args) {
+    while (true) {
+        lcd.locate(0,0);
+        if (sonar_data == -1.0)  {
+            printf("Timeout Error.\n");   
+        } else if (sonar_data > 5.0) {  
+            printf("No object within detection range.\n");         
+        } else  {
+            printf("Distance = %f m.\n", sonar_data);
+        }
+        Thread::wait(250);
+    }
+}
+
+
+int main()  {    
+    lcd.cls();
+        
+    Thread thread1(led_display);
+    Thread thread2(lcd_display);
+    Thread thread3(serial_display);
+    
+    while (1)   {
+        sonar_data = rf.read_m();
+        Thread::wait(250);
+    }
+}     
\ No newline at end of file