ultimaact
Dependencies: mbed Adafruit_GFX DS1820
main.cpp@3:2685cbff6832, 2021-12-10 (annotated)
- Committer:
- daniferca
- Date:
- Fri Dec 10 16:40:23 2021 +0000
- Revision:
- 3:2685cbff6832
- Parent:
- 2:828e6ac4800a
- Child:
- 4:aa8a5a606384
proyectoV2
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; |
javiervicente | 0:8a1a447db446 | 8 | unsigned distancia=1000;; |
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 | { |
daniferca | 2:828e6ac4800a | 37 | if((finalIzq == 1)) { |
daniferca | 2:828e6ac4800a | 38 | wait(1.0); |
javiervicente | 0:8a1a447db446 | 39 | enable=1; |
javiervicente | 0:8a1a447db446 | 40 | estado=abriendose; |
daniferca | 2:828e6ac4800a | 41 | |
daniferca | 2:828e6ac4800a | 42 | pc.printf("Final2 pulsado\r\n"); |
daniferca | 2:828e6ac4800a | 43 | |
daniferca | 2:828e6ac4800a | 44 | |
daniferca | 3:2685cbff6832 | 45 | |
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 | { |
daniferca | 2:828e6ac4800a | 53 | if((finalDer==1)) { |
daniferca | 1:1e68e882342d | 54 | |
javiervicente | 0:8a1a447db446 | 55 | estado=abierta; |
javiervicente | 0:8a1a447db446 | 56 | enable=0; |
daniferca | 2:828e6ac4800a | 57 | |
daniferca | 1:1e68e882342d | 58 | } else { |
daniferca | 2:828e6ac4800a | 59 | |
daniferca | 2:828e6ac4800a | 60 | paso(1); |
daniferca | 2:828e6ac4800a | 61 | //pc.printf("paso\n"); |
javiervicente | 0:8a1a447db446 | 62 | |
javiervicente | 0:8a1a447db446 | 63 | } |
javiervicente | 0:8a1a447db446 | 64 | } |
javiervicente | 0:8a1a447db446 | 65 | |
javiervicente | 0:8a1a447db446 | 66 | void estadoCerrandose(){ |
daniferca | 2:828e6ac4800a | 67 | if((finalIzq==1)) { |
daniferca | 1:1e68e882342d | 68 | |
javiervicente | 0:8a1a447db446 | 69 | estado=cerrada; |
javiervicente | 0:8a1a447db446 | 70 | enable=0; |
daniferca | 1:1e68e882342d | 71 | } else { |
daniferca | 2:828e6ac4800a | 72 | |
daniferca | 2:828e6ac4800a | 73 | paso(0); |
daniferca | 2:828e6ac4800a | 74 | //pc.printf("paso\n"); |
daniferca | 1:1e68e882342d | 75 | |
javiervicente | 0:8a1a447db446 | 76 | } |
javiervicente | 0:8a1a447db446 | 77 | } |
javiervicente | 0:8a1a447db446 | 78 | |
javiervicente | 0:8a1a447db446 | 79 | void estadoAbierta() |
javiervicente | 0:8a1a447db446 | 80 | { |
daniferca | 2:828e6ac4800a | 81 | if (finalDer==1) { |
daniferca | 1:1e68e882342d | 82 | |
daniferca | 2:828e6ac4800a | 83 | |
javiervicente | 0:8a1a447db446 | 84 | enable=1; |
javiervicente | 0:8a1a447db446 | 85 | estado=cerrandose; |
daniferca | 2:828e6ac4800a | 86 | pc.printf("Final1 pulsado\r\n"); |
daniferca | 1:1e68e882342d | 87 | |
javiervicente | 0:8a1a447db446 | 88 | } |
javiervicente | 0:8a1a447db446 | 89 | } |
javiervicente | 0:8a1a447db446 | 90 | |
javiervicente | 0:8a1a447db446 | 91 | int main() |
javiervicente | 0:8a1a447db446 | 92 | { |
javiervicente | 0:8a1a447db446 | 93 | pc.baud(115200); |
javiervicente | 0:8a1a447db446 | 94 | tickerMideDistancia.attach(&mideDistancia, 0.5); |
javiervicente | 0:8a1a447db446 | 95 | estado=cerrada; |
daniferca | 2:828e6ac4800a | 96 | pc.printf("Estado cerrada\n"); |
javiervicente | 0:8a1a447db446 | 97 | while(1) { |
javiervicente | 0:8a1a447db446 | 98 | distancia=usensor.get_dist_cm(); |
javiervicente | 0:8a1a447db446 | 99 | switch ( estado ) { |
javiervicente | 0:8a1a447db446 | 100 | case cerrada: |
javiervicente | 0:8a1a447db446 | 101 | estadoCerrada(); |
javiervicente | 0:8a1a447db446 | 102 | break; |
javiervicente | 0:8a1a447db446 | 103 | case abriendose: |
javiervicente | 0:8a1a447db446 | 104 | estadoAbriendose(); |
javiervicente | 0:8a1a447db446 | 105 | break; |
javiervicente | 0:8a1a447db446 | 106 | case abierta: |
javiervicente | 0:8a1a447db446 | 107 | estadoAbierta(); |
javiervicente | 0:8a1a447db446 | 108 | break; |
javiervicente | 0:8a1a447db446 | 109 | case cerrandose: |
javiervicente | 0:8a1a447db446 | 110 | estadoCerrandose(); |
javiervicente | 0:8a1a447db446 | 111 | break; |
javiervicente | 0:8a1a447db446 | 112 | default: |
javiervicente | 0:8a1a447db446 | 113 | break; |
javiervicente | 0:8a1a447db446 | 114 | } |
javiervicente | 0:8a1a447db446 | 115 | } |
javiervicente | 0:8a1a447db446 | 116 | } |