Emanuel Torres
/
Sensor_ultrasonico_Torres
Programacion y encendido de led mediante distancias por sensor de ultrasonido.
Revision 1:f05e5d64e2ab, committed 2020-02-05
- Comitter:
- emanueltorres
- Date:
- Wed Feb 05 21:31:02 2020 +0000
- Parent:
- 0:70927b032b2e
- Child:
- 2:6b67a253cfa1
- Commit message:
- Sensor ultrasonico para distancia
Changed in this revision
--- a/HCSR04.cpp Wed Feb 05 03:29:40 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,41 +0,0 @@ -#include "HCSR04.h" -#include "mbed.h" - - -HCSR04::HCSR04(PinName t, PinName e) : trig(t), echo(e) {} - -// Trigger Echo -// _______ _____________,,,,,,,,, -// ____| 10us |_________| 150us-25ms, or 38ms if no obstacle -// - -//return echo duration in us (refer to digram above) -long HCSR04::echo_duration() { - timer.reset(); - trig = 0; - wait_us(2); - trig = 1; - wait_us(10); - trig = 0; - while(echo == 0); - timer.start(); - while(echo == 1); - timer.stop(); - return timer.read_us(); -} - -//return distance to nearest obstacle or returns -1 -//if no obstacle within range -//set sys to cm or inch accordingly -long HCSR04::distance(int sys){ - duration = echo_duration(); - if(duration > 30000) - return -1; - distacne_cm = duration /29 / 2 ; - distance_inc = duration / 74 / 2; - if (sys) - return distacne_cm; - else - return distance_inc; -} -
--- a/HCSR04.h Wed Feb 05 03:29:40 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,39 +0,0 @@ -//Library for controlling ultrasonic module HCSR04 -//Ported by hiawoood from arduino library orgininally created by ITead studio. -//Instantiate object by supplying the proper pin numbers of "trigger" and "echo" -//e.g. -/* - int main() { - Ultrasonic sensor(p5, p6); - while(1){ - long distance = sensor.distance(CM); - printf("Distance:%d\n"); - wait(0.1); - } - } -*/ - - - -#ifndef HCSR04_H -#define HCSR04_H - -#include "mbed.h" - -#define CM 1 -#define INC 0 - -class HCSR04 { - public: - HCSR04(PinName t, PinName e); - long echo_duration(); - long distance(int sys); - - private: - DigitalOut trig; - DigitalIn echo; - Timer timer; - long duration,distacne_cm,distance_inc; -}; - -#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HCSR04.lib Wed Feb 05 21:31:02 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/aralshukaili/code/HCSR04/#0bda99bb39a4
--- a/main.cpp Wed Feb 05 03:29:40 2020 +0000 +++ b/main.cpp Wed Feb 05 21:31:02 2020 +0000 @@ -5,31 +5,42 @@ DigitalOut LedVerde(PD_12); //Salidad digitales, led de color verde en la placa DigitalOut LedNaranja(PD_13); // Salida digitales, led de color naranja en la placa -DigitalOut LedAzul(PD_14); // Salidad digital, led de color azul en la placa +DigitalOut LedAzul(PD_15); // Salidad digital, led de color azul en la placa +Serial device(PA_2,PA_3); -int main() { // Declaración de la función proncipal +int main() // Declaración de la función proncipal +{ + device.baud(115200); + device.printf("Start\n"); - while(1) { // Condicional establecido para que siempre sea verdadero - long d= sensor.distance(1); // comando para un numero largo de bits - printf("Distancia:%d",sensor.distance(1)); // comando de impresion para mostrar - - if (d<=10) { //condicional de led + while(1) // Condicional establecido para que siempre sea verdadero + { + int d=sensor.distance(1); // comando para un numero largo de bits + device.printf("Distancia:%d\r\n",d); // comando de impresion para mostrar + + if (d<10) //condicional de led + { LedVerde=0; LedNaranja=1; LedAzul=0; + device.printf("LedNaranja\n"); } - - if (d>=20); { + + else if (d>=11 && d<=19) + { LedVerde=1; LedNaranja=0; LedAzul=0; + device.printf("LedVerde\n"); } - - if (d>=11 && d<=19); { + + else + { LedVerde=0; LedNaranja=0; LedAzul=1; - } - - } + device.printf("LedAzul\n"); + } + wait(0.1); + } }