ver 2 ultrasonic

Dependencies:   mbed mbed-rtos HCSR04

Committer:
oxochimitl
Date:
Mon Mar 11 19:25:58 2019 +0000
Revision:
0:de082043a69d
Child:
1:a38d3531c822
ver 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oxochimitl 0:de082043a69d 1 #include "mbed.h"
oxochimitl 0:de082043a69d 2 #include "hcsr04.h"
oxochimitl 0:de082043a69d 3 float a=0;
oxochimitl 0:de082043a69d 4 float b=0;
oxochimitl 0:de082043a69d 5 float x=0;
oxochimitl 0:de082043a69d 6 float y=0;
oxochimitl 0:de082043a69d 7 float z=0;
oxochimitl 0:de082043a69d 8 unsigned int dist;
oxochimitl 0:de082043a69d 9
oxochimitl 0:de082043a69d 10 void avanza(float);
oxochimitl 0:de082043a69d 11 void espera(float);
oxochimitl 0:de082043a69d 12 void ireversa(float);
oxochimitl 0:de082043a69d 13 void reversa(float);
oxochimitl 0:de082043a69d 14 void derecha(float);
oxochimitl 0:de082043a69d 15
oxochimitl 0:de082043a69d 16
oxochimitl 0:de082043a69d 17 HCSR04 usensor(p25,p6);
oxochimitl 0:de082043a69d 18 DigitalOut pin1(p21);
oxochimitl 0:de082043a69d 19 DigitalOut pin2(p22);
oxochimitl 0:de082043a69d 20 DigitalOut pin3(p23);
oxochimitl 0:de082043a69d 21 DigitalOut pin4(p24);
oxochimitl 0:de082043a69d 22
oxochimitl 0:de082043a69d 23 int main() {
oxochimitl 0:de082043a69d 24 usensor.start();
oxochimitl 0:de082043a69d 25 dist=usensor.get_dist_cm();
oxochimitl 0:de082043a69d 26
oxochimitl 0:de082043a69d 27 if(dist<=10){
oxochimitl 0:de082043a69d 28 reversa(2);
oxochimitl 0:de082043a69d 29 }
oxochimitl 0:de082043a69d 30 if(dist>10){
oxochimitl 0:de082043a69d 31 avanza(1);
oxochimitl 0:de082043a69d 32 }
oxochimitl 0:de082043a69d 33 }
oxochimitl 0:de082043a69d 34 void avanza(float a)
oxochimitl 0:de082043a69d 35 {
oxochimitl 0:de082043a69d 36 pin1 = 1;
oxochimitl 0:de082043a69d 37 pin2 = 0;
oxochimitl 0:de082043a69d 38 pin3 = 1;
oxochimitl 0:de082043a69d 39 pin4 = 0;
oxochimitl 0:de082043a69d 40 wait(a);
oxochimitl 0:de082043a69d 41 }
oxochimitl 0:de082043a69d 42 void espera(float b){
oxochimitl 0:de082043a69d 43 pin1 = 0;
oxochimitl 0:de082043a69d 44 pin2 = 0;
oxochimitl 0:de082043a69d 45 pin3 = 0;
oxochimitl 0:de082043a69d 46 pin4 = 0;
oxochimitl 0:de082043a69d 47 wait(b);
oxochimitl 0:de082043a69d 48 }
oxochimitl 0:de082043a69d 49 void ireversa(float x){
oxochimitl 0:de082043a69d 50 pin1 = 1;
oxochimitl 0:de082043a69d 51 pin2 = 0;
oxochimitl 0:de082043a69d 52 pin3 = 0;
oxochimitl 0:de082043a69d 53 pin4 = 0;
oxochimitl 0:de082043a69d 54 wait(x);
oxochimitl 0:de082043a69d 55 }
oxochimitl 0:de082043a69d 56 void reversa(float y){
oxochimitl 0:de082043a69d 57 pin1 = 0;
oxochimitl 0:de082043a69d 58 pin2 = 1;
oxochimitl 0:de082043a69d 59 pin3 = 0;
oxochimitl 0:de082043a69d 60 pin4 = 1;
oxochimitl 0:de082043a69d 61 wait(y);
oxochimitl 0:de082043a69d 62 }
oxochimitl 0:de082043a69d 63 void derecha(float z){
oxochimitl 0:de082043a69d 64 pin1 = 0;
oxochimitl 0:de082043a69d 65 pin2 = 0;
oxochimitl 0:de082043a69d 66 pin3 = 1;
oxochimitl 0:de082043a69d 67 pin4 = 0;
oxochimitl 0:de082043a69d 68 wait(z);
oxochimitl 0:de082043a69d 69 }