wss
Dependencies: mbed Adafruit_GFX DS1820
main.cpp@5:0e6b049208d8, 2021-12-13 (annotated)
- Committer:
- davidmateos
- Date:
- Mon Dec 13 19:09:00 2021 +0000
- Revision:
- 5:0e6b049208d8
- Parent:
- 4:aa8a5a606384
- Child:
- 6:23dfd7b0e58c
ultima version contador
Who changed what in which revision?
User | Revision | Line number | New 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 | |
davidmateos | 5:0e6b049208d8 | 17 | int contador=0; |
davidmateos | 5:0e6b049208d8 | 18 | |
davidmateos | 5:0e6b049208d8 | 19 | |
javiervicente | 0:8a1a447db446 | 20 | enum estados {cerrada, abriendose, cerrandose, abierta}; |
javiervicente | 0:8a1a447db446 | 21 | estados estado; |
javiervicente | 0:8a1a447db446 | 22 | |
javiervicente | 0:8a1a447db446 | 23 | |
javiervicente | 0:8a1a447db446 | 24 | void mideDistancia() |
javiervicente | 0:8a1a447db446 | 25 | { |
javiervicente | 0:8a1a447db446 | 26 | usensor.start(); |
javiervicente | 0:8a1a447db446 | 27 | } |
javiervicente | 0:8a1a447db446 | 28 | |
javiervicente | 0:8a1a447db446 | 29 | void paso(int d) |
javiervicente | 0:8a1a447db446 | 30 | { |
javiervicente | 0:8a1a447db446 | 31 | dir=d; |
javiervicente | 0:8a1a447db446 | 32 | step=1; |
daniferca | 2:828e6ac4800a | 33 | wait_us(100); // Este es el cambio de paso |
javiervicente | 0:8a1a447db446 | 34 | step=0; |
daniferca | 2:828e6ac4800a | 35 | wait_us(900); //Este hay que cambiar |
javiervicente | 0:8a1a447db446 | 36 | } |
javiervicente | 0:8a1a447db446 | 37 | |
javiervicente | 0:8a1a447db446 | 38 | void estadoCerrada() |
javiervicente | 0:8a1a447db446 | 39 | { |
davidmateos | 5:0e6b049208d8 | 40 | |
davidmateos | 5:0e6b049208d8 | 41 | if( (finalIzq == 1) && contador <3) { |
davidmateos | 5:0e6b049208d8 | 42 | wait(1.0); |
javiervicente | 0:8a1a447db446 | 43 | enable=1; |
javiervicente | 0:8a1a447db446 | 44 | estado=abriendose; |
davidmateos | 4:aa8a5a606384 | 45 | // pc.printf("Distance =%d\n",distancia); |
daniferca | 2:828e6ac4800a | 46 | pc.printf("Final2 pulsado\r\n"); |
daniferca | 2:828e6ac4800a | 47 | |
davidmateos | 4:aa8a5a606384 | 48 | |
davidmateos | 5:0e6b049208d8 | 49 | //distancia < 5 |
javiervicente | 0:8a1a447db446 | 50 | } |
davidmateos | 5:0e6b049208d8 | 51 | |
javiervicente | 0:8a1a447db446 | 52 | } |
javiervicente | 0:8a1a447db446 | 53 | |
javiervicente | 0:8a1a447db446 | 54 | void estadoAbriendose() |
javiervicente | 0:8a1a447db446 | 55 | { |
davidmateos | 5:0e6b049208d8 | 56 | //pc.printf("Distance =%d\n",distancia); |
daniferca | 2:828e6ac4800a | 57 | if((finalDer==1)) { |
daniferca | 1:1e68e882342d | 58 | |
javiervicente | 0:8a1a447db446 | 59 | estado=abierta; |
javiervicente | 0:8a1a447db446 | 60 | enable=0; |
daniferca | 2:828e6ac4800a | 61 | |
daniferca | 1:1e68e882342d | 62 | } else { |
daniferca | 2:828e6ac4800a | 63 | |
daniferca | 2:828e6ac4800a | 64 | paso(1); |
daniferca | 2:828e6ac4800a | 65 | //pc.printf("paso\n"); |
javiervicente | 0:8a1a447db446 | 66 | |
javiervicente | 0:8a1a447db446 | 67 | } |
davidmateos | 5:0e6b049208d8 | 68 | // if (distancia<4){ |
davidmateos | 5:0e6b049208d8 | 69 | //estado=cerrandose; |
davidmateos | 5:0e6b049208d8 | 70 | |
davidmateos | 5:0e6b049208d8 | 71 | // } |
javiervicente | 0:8a1a447db446 | 72 | } |
javiervicente | 0:8a1a447db446 | 73 | |
javiervicente | 0:8a1a447db446 | 74 | void estadoCerrandose(){ |
davidmateos | 4:aa8a5a606384 | 75 | //pc.printf("Distance =%d\n",distancia); |
daniferca | 2:828e6ac4800a | 76 | if((finalIzq==1)) { |
daniferca | 1:1e68e882342d | 77 | |
javiervicente | 0:8a1a447db446 | 78 | estado=cerrada; |
javiervicente | 0:8a1a447db446 | 79 | enable=0; |
daniferca | 1:1e68e882342d | 80 | } else { |
daniferca | 2:828e6ac4800a | 81 | |
daniferca | 2:828e6ac4800a | 82 | paso(0); |
daniferca | 2:828e6ac4800a | 83 | //pc.printf("paso\n"); |
daniferca | 1:1e68e882342d | 84 | |
javiervicente | 0:8a1a447db446 | 85 | } |
javiervicente | 0:8a1a447db446 | 86 | } |
javiervicente | 0:8a1a447db446 | 87 | |
javiervicente | 0:8a1a447db446 | 88 | void estadoAbierta() |
javiervicente | 0:8a1a447db446 | 89 | { |
davidmateos | 4:aa8a5a606384 | 90 | //pc.printf("Distance =%d\n",distancia); |
davidmateos | 5:0e6b049208d8 | 91 | //if (finalDer==1) { |
daniferca | 1:1e68e882342d | 92 | |
daniferca | 2:828e6ac4800a | 93 | |
javiervicente | 0:8a1a447db446 | 94 | enable=1; |
javiervicente | 0:8a1a447db446 | 95 | estado=cerrandose; |
daniferca | 2:828e6ac4800a | 96 | pc.printf("Final1 pulsado\r\n"); |
daniferca | 1:1e68e882342d | 97 | |
davidmateos | 5:0e6b049208d8 | 98 | //} |
davidmateos | 5:0e6b049208d8 | 99 | contador++; |
javiervicente | 0:8a1a447db446 | 100 | } |
javiervicente | 0:8a1a447db446 | 101 | |
javiervicente | 0:8a1a447db446 | 102 | int main() |
javiervicente | 0:8a1a447db446 | 103 | { |
javiervicente | 0:8a1a447db446 | 104 | pc.baud(115200); |
javiervicente | 0:8a1a447db446 | 105 | tickerMideDistancia.attach(&mideDistancia, 0.5); |
javiervicente | 0:8a1a447db446 | 106 | estado=cerrada; |
davidmateos | 5:0e6b049208d8 | 107 | |
davidmateos | 4:aa8a5a606384 | 108 | //pc.printf("Estado cerrada\n"); |
davidmateos | 4:aa8a5a606384 | 109 | |
javiervicente | 0:8a1a447db446 | 110 | while(1) { |
javiervicente | 0:8a1a447db446 | 111 | distancia=usensor.get_dist_cm(); |
javiervicente | 0:8a1a447db446 | 112 | switch ( estado ) { |
javiervicente | 0:8a1a447db446 | 113 | case cerrada: |
javiervicente | 0:8a1a447db446 | 114 | estadoCerrada(); |
javiervicente | 0:8a1a447db446 | 115 | break; |
javiervicente | 0:8a1a447db446 | 116 | case abriendose: |
javiervicente | 0:8a1a447db446 | 117 | estadoAbriendose(); |
javiervicente | 0:8a1a447db446 | 118 | break; |
javiervicente | 0:8a1a447db446 | 119 | case abierta: |
javiervicente | 0:8a1a447db446 | 120 | estadoAbierta(); |
javiervicente | 0:8a1a447db446 | 121 | break; |
javiervicente | 0:8a1a447db446 | 122 | case cerrandose: |
javiervicente | 0:8a1a447db446 | 123 | estadoCerrandose(); |
javiervicente | 0:8a1a447db446 | 124 | break; |
javiervicente | 0:8a1a447db446 | 125 | default: |
javiervicente | 0:8a1a447db446 | 126 | break; |
javiervicente | 0:8a1a447db446 | 127 | } |
javiervicente | 0:8a1a447db446 | 128 | } |
javiervicente | 0:8a1a447db446 | 129 | } |