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