ultra-servo

Dependencies:   mbed mbed-rtos HCSR04

Files at this revision

API Documentation at this revision

Comitter:
Daniel_2001
Date:
Fri Nov 20 03:49:36 2020 +0000
Parent:
0:a2d6bebaf8ed
Commit message:
Ultra-servo

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
diff -r a2d6bebaf8ed -r 5b7333529d77 main.cpp
--- a/main.cpp	Thu Nov 19 14:23:27 2020 +0000
+++ b/main.cpp	Fri Nov 20 03:49:36 2020 +0000
@@ -1,39 +1,67 @@
 #include "mbed.h"
+#include "rtos.h"
 #include "HCSR04.h"
 
-//HCSR04 usensor(PTE1,PTE0);// (trig,echo)
+
 HCSR04 usensor(D15,D14);// (trig,echo)
+
 Serial pc(USBTX, USBRX); //comunicacion serial
-// Se encenderan Led's dependiendo de la distancia
-DigitalOut myled1(LED1);
-DigitalOut myled2(LED2);
-DigitalOut myled3(LED3);
+
+PwmOut servo(A2);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+
+Thread thread_servo;
+Thread thread_sensor;
+
+
+
+unsigned int distancia;//tomara el valor de la distancia
+
+void task_servo(){
+    while(true){
+        servo.pulsewidth_us(500);
+        Thread::wait(1000);
+        servo.pulsewidth_us(2000);
+        Thread::wait(1000);
+        }
+    }
 
-int distancia;//tomara el valor de la distancia
+void task_sensor(){
+    while(true){
+        distancia = usensor.distance(CM);
+        
+        if(distancia<10){
+            led1=0;
+            led2=1;
+            }
+            else if(distancia<20){
+                led1=1;
+                led2=0;
+                
+                     }
+             else{
+                led1=1;
+                led2=1;
+                } 
+        pc.printf("%u\n",distancia);
+        Thread::wait(100);
+        
+        }
+    }
 
 int main() {
+    
     pc.baud(9600);
-    while(1) {
-        distancia = usensor.distance(CM);//se toma la distancia en cm y se actua depediendo de esta
-        if(distancia<10){
-            
-            myled1=0;
-            myled2=1;
-            myled3=1;
-        }
-        else if(distancia<20){
-          
-            myled1=1;
-            myled2=0;
-            myled3=1;
-        }
-        else{
-          
-            myled1=1;
-            myled2=1;
-            myled3=0;
-        } pc.printf("distancia es (%d)\n\r",distancia);
-        wait(1);      
-    }
+    
+    thread_servo.start(task_servo);
+    thread_sensor.start(task_sensor);
+    
+    servo.period_ms(20);
+    
+    Thread thread_servo(osPriorityNormal);
+    Thread thread_sensor(osPriorityNormal);
+    
+    
 }
 
diff -r a2d6bebaf8ed -r 5b7333529d77 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Fri Nov 20 03:49:36 2020 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed-rtos/#5713cbbdb706