ultrasonido
Dependencies: mbed mbed-rtos HCSR04
main.cpp@1:5b7333529d77, 2020-11-20 (annotated)
- Committer:
- Daniel_2001
- Date:
- Fri Nov 20 03:49:36 2020 +0000
- Revision:
- 1:5b7333529d77
- Parent:
- 0:a2d6bebaf8ed
Ultra-servo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Alejandro10 | 0:a2d6bebaf8ed | 1 | #include "mbed.h" |
Daniel_2001 | 1:5b7333529d77 | 2 | #include "rtos.h" |
Alejandro10 | 0:a2d6bebaf8ed | 3 | #include "HCSR04.h" |
Alejandro10 | 0:a2d6bebaf8ed | 4 | |
Daniel_2001 | 1:5b7333529d77 | 5 | |
Alejandro10 | 0:a2d6bebaf8ed | 6 | HCSR04 usensor(D15,D14);// (trig,echo) |
Daniel_2001 | 1:5b7333529d77 | 7 | |
Alejandro10 | 0:a2d6bebaf8ed | 8 | Serial pc(USBTX, USBRX); //comunicacion serial |
Daniel_2001 | 1:5b7333529d77 | 9 | |
Daniel_2001 | 1:5b7333529d77 | 10 | PwmOut servo(A2); |
Daniel_2001 | 1:5b7333529d77 | 11 | DigitalOut led1(LED1); |
Daniel_2001 | 1:5b7333529d77 | 12 | DigitalOut led2(LED2); |
Daniel_2001 | 1:5b7333529d77 | 13 | |
Daniel_2001 | 1:5b7333529d77 | 14 | Thread thread_servo; |
Daniel_2001 | 1:5b7333529d77 | 15 | Thread thread_sensor; |
Daniel_2001 | 1:5b7333529d77 | 16 | |
Daniel_2001 | 1:5b7333529d77 | 17 | |
Daniel_2001 | 1:5b7333529d77 | 18 | |
Daniel_2001 | 1:5b7333529d77 | 19 | unsigned int distancia;//tomara el valor de la distancia |
Daniel_2001 | 1:5b7333529d77 | 20 | |
Daniel_2001 | 1:5b7333529d77 | 21 | void task_servo(){ |
Daniel_2001 | 1:5b7333529d77 | 22 | while(true){ |
Daniel_2001 | 1:5b7333529d77 | 23 | servo.pulsewidth_us(500); |
Daniel_2001 | 1:5b7333529d77 | 24 | Thread::wait(1000); |
Daniel_2001 | 1:5b7333529d77 | 25 | servo.pulsewidth_us(2000); |
Daniel_2001 | 1:5b7333529d77 | 26 | Thread::wait(1000); |
Daniel_2001 | 1:5b7333529d77 | 27 | } |
Daniel_2001 | 1:5b7333529d77 | 28 | } |
Alejandro10 | 0:a2d6bebaf8ed | 29 | |
Daniel_2001 | 1:5b7333529d77 | 30 | void task_sensor(){ |
Daniel_2001 | 1:5b7333529d77 | 31 | while(true){ |
Daniel_2001 | 1:5b7333529d77 | 32 | distancia = usensor.distance(CM); |
Daniel_2001 | 1:5b7333529d77 | 33 | |
Daniel_2001 | 1:5b7333529d77 | 34 | if(distancia<10){ |
Daniel_2001 | 1:5b7333529d77 | 35 | led1=0; |
Daniel_2001 | 1:5b7333529d77 | 36 | led2=1; |
Daniel_2001 | 1:5b7333529d77 | 37 | } |
Daniel_2001 | 1:5b7333529d77 | 38 | else if(distancia<20){ |
Daniel_2001 | 1:5b7333529d77 | 39 | led1=1; |
Daniel_2001 | 1:5b7333529d77 | 40 | led2=0; |
Daniel_2001 | 1:5b7333529d77 | 41 | |
Daniel_2001 | 1:5b7333529d77 | 42 | } |
Daniel_2001 | 1:5b7333529d77 | 43 | else{ |
Daniel_2001 | 1:5b7333529d77 | 44 | led1=1; |
Daniel_2001 | 1:5b7333529d77 | 45 | led2=1; |
Daniel_2001 | 1:5b7333529d77 | 46 | } |
Daniel_2001 | 1:5b7333529d77 | 47 | pc.printf("%u\n",distancia); |
Daniel_2001 | 1:5b7333529d77 | 48 | Thread::wait(100); |
Daniel_2001 | 1:5b7333529d77 | 49 | |
Daniel_2001 | 1:5b7333529d77 | 50 | } |
Daniel_2001 | 1:5b7333529d77 | 51 | } |
Alejandro10 | 0:a2d6bebaf8ed | 52 | |
Alejandro10 | 0:a2d6bebaf8ed | 53 | int main() { |
Daniel_2001 | 1:5b7333529d77 | 54 | |
Alejandro10 | 0:a2d6bebaf8ed | 55 | pc.baud(9600); |
Daniel_2001 | 1:5b7333529d77 | 56 | |
Daniel_2001 | 1:5b7333529d77 | 57 | thread_servo.start(task_servo); |
Daniel_2001 | 1:5b7333529d77 | 58 | thread_sensor.start(task_sensor); |
Daniel_2001 | 1:5b7333529d77 | 59 | |
Daniel_2001 | 1:5b7333529d77 | 60 | servo.period_ms(20); |
Daniel_2001 | 1:5b7333529d77 | 61 | |
Daniel_2001 | 1:5b7333529d77 | 62 | Thread thread_servo(osPriorityNormal); |
Daniel_2001 | 1:5b7333529d77 | 63 | Thread thread_sensor(osPriorityNormal); |
Daniel_2001 | 1:5b7333529d77 | 64 | |
Daniel_2001 | 1:5b7333529d77 | 65 | |
Alejandro10 | 0:a2d6bebaf8ed | 66 | } |
Alejandro10 | 0:a2d6bebaf8ed | 67 |