ultimaact

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
davidmateos
Date:
Mon Dec 13 11:00:09 2021 +0000
Revision:
4:aa8a5a606384
Parent:
3:2685cbff6832
Child:
5:0e6b049208d8
proyecto avance dia13 dic

Who changed what in which revision?

UserRevisionLine numberNew contents of line
javiervicente 0:8a1a447db446 1 #include "mbed.h"
javiervicente 0:8a1a447db446 2 #include "hcsr04.h"
javiervicente 0:8a1a447db446 3
daniferca 1:1e68e882342d 4
daniferca 1:1e68e882342d 5
javiervicente 0:8a1a447db446 6 Serial pc(USBTX, USBRX); // tx, rx
javiervicente 0:8a1a447db446 7 Ticker tickerMideDistancia;
davidmateos 4:aa8a5a606384 8 unsigned distancia=20;
javiervicente 0:8a1a447db446 9
daniferca 1:1e68e882342d 10 HCSR04 usensor(D7,D8); //(PinName TrigPin,PinName EchoPin):
daniferca 2:828e6ac4800a 11 DigitalIn finalDer(D2);
daniferca 2:828e6ac4800a 12 DigitalIn finalIzq(D3);
javiervicente 0:8a1a447db446 13 DigitalOut step(D13);
javiervicente 0:8a1a447db446 14 DigitalOut dir(D12);
javiervicente 0:8a1a447db446 15 DigitalOut enable(D11);
javiervicente 0:8a1a447db446 16
javiervicente 0:8a1a447db446 17 enum estados {cerrada, abriendose, cerrandose, abierta};
javiervicente 0:8a1a447db446 18 estados estado;
javiervicente 0:8a1a447db446 19
javiervicente 0:8a1a447db446 20
javiervicente 0:8a1a447db446 21 void mideDistancia()
javiervicente 0:8a1a447db446 22 {
javiervicente 0:8a1a447db446 23 usensor.start();
javiervicente 0:8a1a447db446 24 }
javiervicente 0:8a1a447db446 25
javiervicente 0:8a1a447db446 26 void paso(int d)
javiervicente 0:8a1a447db446 27 {
javiervicente 0:8a1a447db446 28 dir=d;
javiervicente 0:8a1a447db446 29 step=1;
daniferca 2:828e6ac4800a 30 wait_us(100); // Este es el cambio de paso
javiervicente 0:8a1a447db446 31 step=0;
daniferca 2:828e6ac4800a 32 wait_us(900); //Este hay que cambiar
javiervicente 0:8a1a447db446 33 }
javiervicente 0:8a1a447db446 34
javiervicente 0:8a1a447db446 35 void estadoCerrada()
javiervicente 0:8a1a447db446 36 {
davidmateos 4:aa8a5a606384 37 if( (finalIzq == 1) && finalDer ==1) {
davidmateos 4:aa8a5a606384 38 wait(2.0);
javiervicente 0:8a1a447db446 39 enable=1;
javiervicente 0:8a1a447db446 40 estado=abriendose;
davidmateos 4:aa8a5a606384 41 // pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 42 pc.printf("Final2 pulsado\r\n");
daniferca 2:828e6ac4800a 43
davidmateos 4:aa8a5a606384 44
davidmateos 4:aa8a5a606384 45 //distancia < 5
daniferca 3:2685cbff6832 46
daniferca 3:2685cbff6832 47
javiervicente 0:8a1a447db446 48 }
javiervicente 0:8a1a447db446 49 }
javiervicente 0:8a1a447db446 50
javiervicente 0:8a1a447db446 51 void estadoAbriendose()
javiervicente 0:8a1a447db446 52 {
davidmateos 4:aa8a5a606384 53 pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 54 if((finalDer==1)) {
daniferca 1:1e68e882342d 55
javiervicente 0:8a1a447db446 56 estado=abierta;
javiervicente 0:8a1a447db446 57 enable=0;
daniferca 2:828e6ac4800a 58
daniferca 1:1e68e882342d 59 } else {
daniferca 2:828e6ac4800a 60
daniferca 2:828e6ac4800a 61 paso(1);
daniferca 2:828e6ac4800a 62 //pc.printf("paso\n");
javiervicente 0:8a1a447db446 63
javiervicente 0:8a1a447db446 64 }
javiervicente 0:8a1a447db446 65 }
javiervicente 0:8a1a447db446 66
javiervicente 0:8a1a447db446 67 void estadoCerrandose(){
davidmateos 4:aa8a5a606384 68 //pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 69 if((finalIzq==1)) {
daniferca 1:1e68e882342d 70
javiervicente 0:8a1a447db446 71 estado=cerrada;
javiervicente 0:8a1a447db446 72 enable=0;
daniferca 1:1e68e882342d 73 } else {
daniferca 2:828e6ac4800a 74
daniferca 2:828e6ac4800a 75 paso(0);
daniferca 2:828e6ac4800a 76 //pc.printf("paso\n");
daniferca 1:1e68e882342d 77
javiervicente 0:8a1a447db446 78 }
javiervicente 0:8a1a447db446 79 }
javiervicente 0:8a1a447db446 80
javiervicente 0:8a1a447db446 81 void estadoAbierta()
javiervicente 0:8a1a447db446 82 {
davidmateos 4:aa8a5a606384 83 //pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 84 if (finalDer==1) {
daniferca 1:1e68e882342d 85
daniferca 2:828e6ac4800a 86
javiervicente 0:8a1a447db446 87 enable=1;
javiervicente 0:8a1a447db446 88 estado=cerrandose;
daniferca 2:828e6ac4800a 89 pc.printf("Final1 pulsado\r\n");
daniferca 1:1e68e882342d 90
javiervicente 0:8a1a447db446 91 }
javiervicente 0:8a1a447db446 92 }
javiervicente 0:8a1a447db446 93
javiervicente 0:8a1a447db446 94 int main()
javiervicente 0:8a1a447db446 95 {
javiervicente 0:8a1a447db446 96 pc.baud(115200);
javiervicente 0:8a1a447db446 97 tickerMideDistancia.attach(&mideDistancia, 0.5);
javiervicente 0:8a1a447db446 98 estado=cerrada;
davidmateos 4:aa8a5a606384 99 //pc.printf("Estado cerrada\n");
davidmateos 4:aa8a5a606384 100
javiervicente 0:8a1a447db446 101 while(1) {
javiervicente 0:8a1a447db446 102 distancia=usensor.get_dist_cm();
javiervicente 0:8a1a447db446 103 switch ( estado ) {
javiervicente 0:8a1a447db446 104 case cerrada:
javiervicente 0:8a1a447db446 105 estadoCerrada();
javiervicente 0:8a1a447db446 106 break;
javiervicente 0:8a1a447db446 107 case abriendose:
javiervicente 0:8a1a447db446 108 estadoAbriendose();
javiervicente 0:8a1a447db446 109 break;
javiervicente 0:8a1a447db446 110 case abierta:
javiervicente 0:8a1a447db446 111 estadoAbierta();
javiervicente 0:8a1a447db446 112 break;
javiervicente 0:8a1a447db446 113 case cerrandose:
javiervicente 0:8a1a447db446 114 estadoCerrandose();
javiervicente 0:8a1a447db446 115 break;
javiervicente 0:8a1a447db446 116 default:
javiervicente 0:8a1a447db446 117 break;
javiervicente 0:8a1a447db446 118 }
javiervicente 0:8a1a447db446 119 }
javiervicente 0:8a1a447db446 120 }