wss
Dependencies: mbed Adafruit_GFX DS1820
main.cpp@7:296b42f24d44, 2021-12-13 (annotated)
- Committer:
- davidmateos
- Date:
- Mon Dec 13 20:03:11 2021 +0000
- Revision:
- 7:296b42f24d44
- Parent:
- 6:23dfd7b0e58c
- Child:
- 8:a071c579f9ea
ultimisima act
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" |
davidmateos | 6:23dfd7b0e58c | 3 | #include "Adafruit_SSD1306.h" |
davidmateos | 6:23dfd7b0e58c | 4 | |
davidmateos | 6:23dfd7b0e58c | 5 | class I2CPreInit : public I2C |
davidmateos | 6:23dfd7b0e58c | 6 | { |
davidmateos | 6:23dfd7b0e58c | 7 | public: |
davidmateos | 6:23dfd7b0e58c | 8 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
davidmateos | 6:23dfd7b0e58c | 9 | { |
davidmateos | 6:23dfd7b0e58c | 10 | frequency(100000); |
davidmateos | 6:23dfd7b0e58c | 11 | start(); |
davidmateos | 6:23dfd7b0e58c | 12 | }; |
davidmateos | 6:23dfd7b0e58c | 13 | }; |
javiervicente | 0:8a1a447db446 | 14 | |
daniferca | 1:1e68e882342d | 15 | |
davidmateos | 6:23dfd7b0e58c | 16 | I2CPreInit gI2C(PB_9,PB_8); |
davidmateos | 6:23dfd7b0e58c | 17 | |
davidmateos | 6:23dfd7b0e58c | 18 | // an SPI sub-class that provides a constructed default |
davidmateos | 6:23dfd7b0e58c | 19 | |
davidmateos | 6:23dfd7b0e58c | 20 | Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128); |
daniferca | 1:1e68e882342d | 21 | |
javiervicente | 0:8a1a447db446 | 22 | Serial pc(USBTX, USBRX); // tx, rx |
javiervicente | 0:8a1a447db446 | 23 | Ticker tickerMideDistancia; |
davidmateos | 4:aa8a5a606384 | 24 | unsigned distancia=20; |
javiervicente | 0:8a1a447db446 | 25 | |
daniferca | 1:1e68e882342d | 26 | HCSR04 usensor(D7,D8); //(PinName TrigPin,PinName EchoPin): |
daniferca | 2:828e6ac4800a | 27 | DigitalIn finalDer(D2); |
daniferca | 2:828e6ac4800a | 28 | DigitalIn finalIzq(D3); |
javiervicente | 0:8a1a447db446 | 29 | DigitalOut step(D13); |
javiervicente | 0:8a1a447db446 | 30 | DigitalOut dir(D12); |
javiervicente | 0:8a1a447db446 | 31 | DigitalOut enable(D11); |
javiervicente | 0:8a1a447db446 | 32 | |
davidmateos | 5:0e6b049208d8 | 33 | int contador=0; |
davidmateos | 5:0e6b049208d8 | 34 | |
davidmateos | 5:0e6b049208d8 | 35 | |
javiervicente | 0:8a1a447db446 | 36 | enum estados {cerrada, abriendose, cerrandose, abierta}; |
javiervicente | 0:8a1a447db446 | 37 | estados estado; |
javiervicente | 0:8a1a447db446 | 38 | |
javiervicente | 0:8a1a447db446 | 39 | |
javiervicente | 0:8a1a447db446 | 40 | void mideDistancia() |
javiervicente | 0:8a1a447db446 | 41 | { |
javiervicente | 0:8a1a447db446 | 42 | usensor.start(); |
javiervicente | 0:8a1a447db446 | 43 | } |
javiervicente | 0:8a1a447db446 | 44 | |
javiervicente | 0:8a1a447db446 | 45 | void paso(int d) |
javiervicente | 0:8a1a447db446 | 46 | { |
javiervicente | 0:8a1a447db446 | 47 | dir=d; |
javiervicente | 0:8a1a447db446 | 48 | step=1; |
daniferca | 2:828e6ac4800a | 49 | wait_us(100); // Este es el cambio de paso |
javiervicente | 0:8a1a447db446 | 50 | step=0; |
daniferca | 2:828e6ac4800a | 51 | wait_us(900); //Este hay que cambiar |
javiervicente | 0:8a1a447db446 | 52 | } |
javiervicente | 0:8a1a447db446 | 53 | |
javiervicente | 0:8a1a447db446 | 54 | void estadoCerrada() |
javiervicente | 0:8a1a447db446 | 55 | { |
davidmateos | 5:0e6b049208d8 | 56 | |
davidmateos | 5:0e6b049208d8 | 57 | if( (finalIzq == 1) && contador <3) { |
davidmateos | 5:0e6b049208d8 | 58 | wait(1.0); |
javiervicente | 0:8a1a447db446 | 59 | enable=1; |
javiervicente | 0:8a1a447db446 | 60 | estado=abriendose; |
davidmateos | 4:aa8a5a606384 | 61 | // pc.printf("Distance =%d\n",distancia); |
daniferca | 2:828e6ac4800a | 62 | pc.printf("Final2 pulsado\r\n"); |
daniferca | 2:828e6ac4800a | 63 | |
davidmateos | 4:aa8a5a606384 | 64 | |
davidmateos | 5:0e6b049208d8 | 65 | //distancia < 5 |
javiervicente | 0:8a1a447db446 | 66 | } |
davidmateos | 7:296b42f24d44 | 67 | if(contador==3){ |
davidmateos | 7:296b42f24d44 | 68 | gOled.clearDisplay(); |
davidmateos | 7:296b42f24d44 | 69 | gOled.printf("Proceso terminado\n"); |
davidmateos | 7:296b42f24d44 | 70 | |
davidmateos | 7:296b42f24d44 | 71 | gOled.display(); |
davidmateos | 7:296b42f24d44 | 72 | } |
davidmateos | 5:0e6b049208d8 | 73 | |
javiervicente | 0:8a1a447db446 | 74 | } |
javiervicente | 0:8a1a447db446 | 75 | |
javiervicente | 0:8a1a447db446 | 76 | void estadoAbriendose() |
javiervicente | 0:8a1a447db446 | 77 | { |
davidmateos | 5:0e6b049208d8 | 78 | //pc.printf("Distance =%d\n",distancia); |
daniferca | 2:828e6ac4800a | 79 | if((finalDer==1)) { |
daniferca | 1:1e68e882342d | 80 | |
javiervicente | 0:8a1a447db446 | 81 | estado=abierta; |
javiervicente | 0:8a1a447db446 | 82 | enable=0; |
daniferca | 2:828e6ac4800a | 83 | |
daniferca | 1:1e68e882342d | 84 | } else { |
daniferca | 2:828e6ac4800a | 85 | |
daniferca | 2:828e6ac4800a | 86 | paso(1); |
daniferca | 2:828e6ac4800a | 87 | //pc.printf("paso\n"); |
javiervicente | 0:8a1a447db446 | 88 | |
javiervicente | 0:8a1a447db446 | 89 | } |
davidmateos | 5:0e6b049208d8 | 90 | // if (distancia<4){ |
davidmateos | 5:0e6b049208d8 | 91 | //estado=cerrandose; |
davidmateos | 5:0e6b049208d8 | 92 | |
davidmateos | 5:0e6b049208d8 | 93 | // } |
javiervicente | 0:8a1a447db446 | 94 | } |
javiervicente | 0:8a1a447db446 | 95 | |
javiervicente | 0:8a1a447db446 | 96 | void estadoCerrandose(){ |
davidmateos | 4:aa8a5a606384 | 97 | //pc.printf("Distance =%d\n",distancia); |
daniferca | 2:828e6ac4800a | 98 | if((finalIzq==1)) { |
daniferca | 1:1e68e882342d | 99 | |
javiervicente | 0:8a1a447db446 | 100 | estado=cerrada; |
javiervicente | 0:8a1a447db446 | 101 | enable=0; |
daniferca | 1:1e68e882342d | 102 | } else { |
daniferca | 2:828e6ac4800a | 103 | |
daniferca | 2:828e6ac4800a | 104 | paso(0); |
daniferca | 2:828e6ac4800a | 105 | //pc.printf("paso\n"); |
daniferca | 1:1e68e882342d | 106 | |
javiervicente | 0:8a1a447db446 | 107 | } |
javiervicente | 0:8a1a447db446 | 108 | } |
javiervicente | 0:8a1a447db446 | 109 | |
javiervicente | 0:8a1a447db446 | 110 | void estadoAbierta() |
javiervicente | 0:8a1a447db446 | 111 | { |
davidmateos | 4:aa8a5a606384 | 112 | //pc.printf("Distance =%d\n",distancia); |
davidmateos | 5:0e6b049208d8 | 113 | //if (finalDer==1) { |
daniferca | 1:1e68e882342d | 114 | |
daniferca | 2:828e6ac4800a | 115 | |
javiervicente | 0:8a1a447db446 | 116 | enable=1; |
javiervicente | 0:8a1a447db446 | 117 | estado=cerrandose; |
daniferca | 2:828e6ac4800a | 118 | pc.printf("Final1 pulsado\r\n"); |
daniferca | 1:1e68e882342d | 119 | |
davidmateos | 5:0e6b049208d8 | 120 | //} |
davidmateos | 5:0e6b049208d8 | 121 | contador++; |
davidmateos | 6:23dfd7b0e58c | 122 | gOled.clearDisplay(); |
davidmateos | 7:296b42f24d44 | 123 | gOled.printf("Proceso en marcha\n"); |
davidmateos | 7:296b42f24d44 | 124 | gOled.printf("Piezas transportadas %d\n",contador); |
davidmateos | 6:23dfd7b0e58c | 125 | gOled.display(); |
davidmateos | 6:23dfd7b0e58c | 126 | gOled.setTextCursor(0,0); |
davidmateos | 6:23dfd7b0e58c | 127 | //wait(1.000); |
davidmateos | 6:23dfd7b0e58c | 128 | |
javiervicente | 0:8a1a447db446 | 129 | } |
javiervicente | 0:8a1a447db446 | 130 | |
javiervicente | 0:8a1a447db446 | 131 | int main() |
javiervicente | 0:8a1a447db446 | 132 | { |
javiervicente | 0:8a1a447db446 | 133 | pc.baud(115200); |
javiervicente | 0:8a1a447db446 | 134 | tickerMideDistancia.attach(&mideDistancia, 0.5); |
davidmateos | 6:23dfd7b0e58c | 135 | gOled.begin(); |
davidmateos | 6:23dfd7b0e58c | 136 | gOled.clearDisplay(); |
javiervicente | 0:8a1a447db446 | 137 | estado=cerrada; |
davidmateos | 5:0e6b049208d8 | 138 | |
davidmateos | 4:aa8a5a606384 | 139 | //pc.printf("Estado cerrada\n"); |
davidmateos | 6:23dfd7b0e58c | 140 | gOled.printf("Buenos dias\n"); |
davidmateos | 7:296b42f24d44 | 141 | gOled.display(); |
davidmateos | 6:23dfd7b0e58c | 142 | |
davidmateos | 6:23dfd7b0e58c | 143 | |
javiervicente | 0:8a1a447db446 | 144 | while(1) { |
javiervicente | 0:8a1a447db446 | 145 | distancia=usensor.get_dist_cm(); |
davidmateos | 6:23dfd7b0e58c | 146 | |
javiervicente | 0:8a1a447db446 | 147 | switch ( estado ) { |
javiervicente | 0:8a1a447db446 | 148 | case cerrada: |
javiervicente | 0:8a1a447db446 | 149 | estadoCerrada(); |
javiervicente | 0:8a1a447db446 | 150 | break; |
javiervicente | 0:8a1a447db446 | 151 | case abriendose: |
javiervicente | 0:8a1a447db446 | 152 | estadoAbriendose(); |
javiervicente | 0:8a1a447db446 | 153 | break; |
javiervicente | 0:8a1a447db446 | 154 | case abierta: |
javiervicente | 0:8a1a447db446 | 155 | estadoAbierta(); |
javiervicente | 0:8a1a447db446 | 156 | break; |
javiervicente | 0:8a1a447db446 | 157 | case cerrandose: |
javiervicente | 0:8a1a447db446 | 158 | estadoCerrandose(); |
javiervicente | 0:8a1a447db446 | 159 | break; |
javiervicente | 0:8a1a447db446 | 160 | default: |
javiervicente | 0:8a1a447db446 | 161 | break; |
davidmateos | 6:23dfd7b0e58c | 162 | |
javiervicente | 0:8a1a447db446 | 163 | } |
davidmateos | 6:23dfd7b0e58c | 164 | |
davidmateos | 6:23dfd7b0e58c | 165 | |
javiervicente | 0:8a1a447db446 | 166 | } |
davidmateos | 6:23dfd7b0e58c | 167 | |
javiervicente | 0:8a1a447db446 | 168 | } |