![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
wss
Dependencies: mbed Adafruit_GFX DS1820
main.cpp@1:1e68e882342d, 2021-12-10 (annotated)
- Committer:
- daniferca
- Date:
- Fri Dec 10 15:32:56 2021 +0000
- Revision:
- 1:1e68e882342d
- Parent:
- 0:8a1a447db446
- Child:
- 2:828e6ac4800a
maquinacambiadaproyecto1;
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 | 1:1e68e882342d | 11 | DigitalIn final1(D2); |
daniferca | 1:1e68e882342d | 12 | DigitalIn final2(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 | HCSR04 usensor(D7,D8); //(PinName TrigPin,PinName EchoPin): |
javiervicente | 0:8a1a447db446 | 21 | |
javiervicente | 0:8a1a447db446 | 22 | void mideDistancia() |
javiervicente | 0:8a1a447db446 | 23 | { |
javiervicente | 0:8a1a447db446 | 24 | usensor.start(); |
javiervicente | 0:8a1a447db446 | 25 | } |
javiervicente | 0:8a1a447db446 | 26 | |
javiervicente | 0:8a1a447db446 | 27 | void paso(int d) |
javiervicente | 0:8a1a447db446 | 28 | { |
javiervicente | 0:8a1a447db446 | 29 | dir=d; |
javiervicente | 0:8a1a447db446 | 30 | step=1; |
javiervicente | 0:8a1a447db446 | 31 | wait_us(100); |
javiervicente | 0:8a1a447db446 | 32 | step=0; |
javiervicente | 0:8a1a447db446 | 33 | wait_us(900); |
javiervicente | 0:8a1a447db446 | 34 | } |
javiervicente | 0:8a1a447db446 | 35 | |
javiervicente | 0:8a1a447db446 | 36 | void estadoCerrada() |
javiervicente | 0:8a1a447db446 | 37 | { |
daniferca | 1:1e68e882342d | 38 | if(distancia<5)&&(final1 == 1) { |
javiervicente | 0:8a1a447db446 | 39 | enable=1; |
javiervicente | 0:8a1a447db446 | 40 | estado=abriendose; |
javiervicente | 0:8a1a447db446 | 41 | pc.printf("Estado Abriendose, distancia %d\n",distancia); |
daniferca | 1:1e68e882342d | 42 | pc.printf("Final1 pulsado\r\n"); |
javiervicente | 0:8a1a447db446 | 43 | } |
javiervicente | 0:8a1a447db446 | 44 | } |
javiervicente | 0:8a1a447db446 | 45 | |
javiervicente | 0:8a1a447db446 | 46 | void estadoAbriendose() |
javiervicente | 0:8a1a447db446 | 47 | { |
daniferca | 1:1e68e882342d | 48 | if((final2==1)) { |
daniferca | 1:1e68e882342d | 49 | |
javiervicente | 0:8a1a447db446 | 50 | estado=abierta; |
javiervicente | 0:8a1a447db446 | 51 | enable=0; |
daniferca | 1:1e68e882342d | 52 | } else { |
daniferca | 1:1e68e882342d | 53 | dir = 1; |
daniferca | 1:1e68e882342d | 54 | step=1; |
javiervicente | 0:8a1a447db446 | 55 | |
javiervicente | 0:8a1a447db446 | 56 | } |
javiervicente | 0:8a1a447db446 | 57 | } |
javiervicente | 0:8a1a447db446 | 58 | |
javiervicente | 0:8a1a447db446 | 59 | void estadoCerrandose(){ |
daniferca | 1:1e68e882342d | 60 | if((final1==1)) { |
daniferca | 1:1e68e882342d | 61 | |
javiervicente | 0:8a1a447db446 | 62 | estado=cerrada; |
javiervicente | 0:8a1a447db446 | 63 | enable=0; |
daniferca | 1:1e68e882342d | 64 | } else { |
daniferca | 1:1e68e882342d | 65 | dir = 0; |
daniferca | 1:1e68e882342d | 66 | step=1; |
daniferca | 1:1e68e882342d | 67 | |
javiervicente | 0:8a1a447db446 | 68 | } |
javiervicente | 0:8a1a447db446 | 69 | } |
javiervicente | 0:8a1a447db446 | 70 | |
javiervicente | 0:8a1a447db446 | 71 | void estadoAbierta() |
javiervicente | 0:8a1a447db446 | 72 | { |
daniferca | 1:1e68e882342d | 73 | if (final2==1) { |
daniferca | 1:1e68e882342d | 74 | |
daniferca | 1:1e68e882342d | 75 | wait(1.0); |
javiervicente | 0:8a1a447db446 | 76 | enable=1; |
javiervicente | 0:8a1a447db446 | 77 | estado=cerrandose; |
daniferca | 1:1e68e882342d | 78 | pc.printf("Final2 pulsado\r\n"); |
daniferca | 1:1e68e882342d | 79 | |
javiervicente | 0:8a1a447db446 | 80 | } |
javiervicente | 0:8a1a447db446 | 81 | } |
javiervicente | 0:8a1a447db446 | 82 | |
javiervicente | 0:8a1a447db446 | 83 | int main() |
javiervicente | 0:8a1a447db446 | 84 | { |
javiervicente | 0:8a1a447db446 | 85 | pc.baud(115200); |
javiervicente | 0:8a1a447db446 | 86 | tickerMideDistancia.attach(&mideDistancia, 0.5); |
javiervicente | 0:8a1a447db446 | 87 | estado=cerrada; |
javiervicente | 0:8a1a447db446 | 88 | pc.printf("Estado cerrada\n"); |
javiervicente | 0:8a1a447db446 | 89 | while(1) { |
javiervicente | 0:8a1a447db446 | 90 | distancia=usensor.get_dist_cm(); |
javiervicente | 0:8a1a447db446 | 91 | switch ( estado ) { |
javiervicente | 0:8a1a447db446 | 92 | case cerrada: |
javiervicente | 0:8a1a447db446 | 93 | estadoCerrada(); |
javiervicente | 0:8a1a447db446 | 94 | break; |
javiervicente | 0:8a1a447db446 | 95 | case abriendose: |
javiervicente | 0:8a1a447db446 | 96 | estadoAbriendose(); |
javiervicente | 0:8a1a447db446 | 97 | break; |
javiervicente | 0:8a1a447db446 | 98 | case abierta: |
javiervicente | 0:8a1a447db446 | 99 | estadoAbierta(); |
javiervicente | 0:8a1a447db446 | 100 | break; |
javiervicente | 0:8a1a447db446 | 101 | case cerrandose: |
javiervicente | 0:8a1a447db446 | 102 | estadoCerrandose(); |
javiervicente | 0:8a1a447db446 | 103 | break; |
javiervicente | 0:8a1a447db446 | 104 | default: |
javiervicente | 0:8a1a447db446 | 105 | break; |
javiervicente | 0:8a1a447db446 | 106 | } |
javiervicente | 0:8a1a447db446 | 107 | } |
javiervicente | 0:8a1a447db446 | 108 | } |