wss
Dependencies: mbed Adafruit_GFX DS1820
main.cpp@14:21cb6b2ac16b, 2021-12-14 (annotated)
- Committer:
- davidmateos
- Date:
- Tue Dec 14 13:05:02 2021 +0000
- Revision:
- 14:21cb6b2ac16b
- Parent:
- 13:d2c49ef3b2ac
- Child:
- 15:2acad1c57ad4
eee
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 | 10:db8ef252faba | 3 | #include "DS1820.h" |
davidmateos | 8:a071c579f9ea | 4 | |
davidmateos | 8:a071c579f9ea | 5 | |
davidmateos | 6:23dfd7b0e58c | 6 | #include "Adafruit_SSD1306.h" |
davidmateos | 14:21cb6b2ac16b | 7 | |
davidmateos | 6:23dfd7b0e58c | 8 | class I2CPreInit : public I2C |
davidmateos | 6:23dfd7b0e58c | 9 | { |
davidmateos | 6:23dfd7b0e58c | 10 | public: |
davidmateos | 6:23dfd7b0e58c | 11 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
davidmateos | 6:23dfd7b0e58c | 12 | { |
davidmateos | 6:23dfd7b0e58c | 13 | frequency(100000); |
davidmateos | 6:23dfd7b0e58c | 14 | start(); |
davidmateos | 6:23dfd7b0e58c | 15 | }; |
davidmateos | 6:23dfd7b0e58c | 16 | }; |
davidmateos | 14:21cb6b2ac16b | 17 | |
davidmateos | 14:21cb6b2ac16b | 18 | |
davidmateos | 6:23dfd7b0e58c | 19 | I2CPreInit gI2C(PB_9,PB_8); |
davidmateos | 14:21cb6b2ac16b | 20 | |
davidmateos | 6:23dfd7b0e58c | 21 | // an SPI sub-class that provides a constructed default |
davidmateos | 14:21cb6b2ac16b | 22 | |
davidmateos | 6:23dfd7b0e58c | 23 | Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128); |
davidmateos | 9:d651ed116942 | 24 | |
daniferca | 1:1e68e882342d | 25 | |
javiervicente | 0:8a1a447db446 | 26 | Serial pc(USBTX, USBRX); // tx, rx |
davidmateos | 13:d2c49ef3b2ac | 27 | //Serial bt(D1, D0); // tx, rx |
davidmateos | 13:d2c49ef3b2ac | 28 | |
davidmateos | 13:d2c49ef3b2ac | 29 | |
javiervicente | 0:8a1a447db446 | 30 | Ticker tickerMideDistancia; |
davidmateos | 4:aa8a5a606384 | 31 | unsigned distancia=20; |
javiervicente | 0:8a1a447db446 | 32 | |
daniferca | 1:1e68e882342d | 33 | HCSR04 usensor(D7,D8); //(PinName TrigPin,PinName EchoPin): |
daniferca | 2:828e6ac4800a | 34 | DigitalIn finalDer(D2); |
daniferca | 2:828e6ac4800a | 35 | DigitalIn finalIzq(D3); |
javiervicente | 0:8a1a447db446 | 36 | DigitalOut step(D13); |
javiervicente | 0:8a1a447db446 | 37 | DigitalOut dir(D12); |
javiervicente | 0:8a1a447db446 | 38 | DigitalOut enable(D11); |
davidmateos | 14:21cb6b2ac16b | 39 | DigitalOut led(LED1); |
davidmateos | 14:21cb6b2ac16b | 40 | DS1820 ds1820(D10); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin |
davidmateos | 11:ff1efbab2d9e | 41 | |
davidmateos | 11:ff1efbab2d9e | 42 | //gitalOut rele(A5); |
davidmateos | 14:21cb6b2ac16b | 43 | |
davidmateos | 10:db8ef252faba | 44 | float temp = 0; |
davidmateos | 5:0e6b049208d8 | 45 | int contador=0; |
davidmateos | 5:0e6b049208d8 | 46 | |
davidmateos | 13:d2c49ef3b2ac | 47 | |
davidmateos | 14:21cb6b2ac16b | 48 | char x='0'; |
davidmateos | 14:21cb6b2ac16b | 49 | char y='0'; |
davidmateos | 13:d2c49ef3b2ac | 50 | //if (x==1){ |
davidmateos | 14:21cb6b2ac16b | 51 | //y=0; |
davidmateos | 14:21cb6b2ac16b | 52 | // }else{ |
davidmateos | 14:21cb6b2ac16b | 53 | // y=1; |
davidmateos | 14:21cb6b2ac16b | 54 | // } |
davidmateos | 13:d2c49ef3b2ac | 55 | |
davidmateos | 11:ff1efbab2d9e | 56 | //////////////// |
davidmateos | 11:ff1efbab2d9e | 57 | Timer tiempo; |
davidmateos | 5:0e6b049208d8 | 58 | |
javiervicente | 0:8a1a447db446 | 59 | enum estados {cerrada, abriendose, cerrandose, abierta}; |
javiervicente | 0:8a1a447db446 | 60 | estados estado; |
javiervicente | 0:8a1a447db446 | 61 | |
javiervicente | 0:8a1a447db446 | 62 | |
javiervicente | 0:8a1a447db446 | 63 | void mideDistancia() |
javiervicente | 0:8a1a447db446 | 64 | { |
javiervicente | 0:8a1a447db446 | 65 | usensor.start(); |
javiervicente | 0:8a1a447db446 | 66 | } |
javiervicente | 0:8a1a447db446 | 67 | |
javiervicente | 0:8a1a447db446 | 68 | void paso(int d) |
javiervicente | 0:8a1a447db446 | 69 | { |
javiervicente | 0:8a1a447db446 | 70 | dir=d; |
javiervicente | 0:8a1a447db446 | 71 | step=1; |
daniferca | 2:828e6ac4800a | 72 | wait_us(100); // Este es el cambio de paso |
javiervicente | 0:8a1a447db446 | 73 | step=0; |
daniferca | 2:828e6ac4800a | 74 | wait_us(900); //Este hay que cambiar |
javiervicente | 0:8a1a447db446 | 75 | } |
javiervicente | 0:8a1a447db446 | 76 | |
javiervicente | 0:8a1a447db446 | 77 | void estadoCerrada() |
javiervicente | 0:8a1a447db446 | 78 | { |
davidmateos | 14:21cb6b2ac16b | 79 | if (x=='1') { |
davidmateos | 14:21cb6b2ac16b | 80 | if( (finalIzq == 1) && contador <3) { |
davidmateos | 14:21cb6b2ac16b | 81 | wait(0.5); |
davidmateos | 14:21cb6b2ac16b | 82 | enable=1; |
davidmateos | 14:21cb6b2ac16b | 83 | estado=abriendose; |
davidmateos | 14:21cb6b2ac16b | 84 | // pc.printf("Distance =%d\n",distancia); |
davidmateos | 14:21cb6b2ac16b | 85 | pc.printf("Final2 pulsado\r\n"); |
davidmateos | 14:21cb6b2ac16b | 86 | |
davidmateos | 14:21cb6b2ac16b | 87 | |
davidmateos | 14:21cb6b2ac16b | 88 | //distancia < 5 |
davidmateos | 14:21cb6b2ac16b | 89 | } else if(contador==3) { |
davidmateos | 14:21cb6b2ac16b | 90 | y='1'; |
davidmateos | 14:21cb6b2ac16b | 91 | x='0'; |
davidmateos | 13:d2c49ef3b2ac | 92 | } |
davidmateos | 11:ff1efbab2d9e | 93 | } |
davidmateos | 14:21cb6b2ac16b | 94 | if(x=='0' &&y=='0') { |
davidmateos | 11:ff1efbab2d9e | 95 | pc.printf("No seleccionado programa\r\n"); |
davidmateos | 14:21cb6b2ac16b | 96 | } |
davidmateos | 14:21cb6b2ac16b | 97 | |
davidmateos | 14:21cb6b2ac16b | 98 | |
davidmateos | 14:21cb6b2ac16b | 99 | if(y=='1') { |
davidmateos | 14:21cb6b2ac16b | 100 | if(finalIzq == 1 && finalDer==1) { |
davidmateos | 14:21cb6b2ac16b | 101 | |
davidmateos | 14:21cb6b2ac16b | 102 | enable=1; |
davidmateos | 14:21cb6b2ac16b | 103 | tiempo.reset(); |
davidmateos | 14:21cb6b2ac16b | 104 | tiempo.start(); |
davidmateos | 14:21cb6b2ac16b | 105 | estado=abriendose; |
davidmateos | 11:ff1efbab2d9e | 106 | } |
davidmateos | 14:21cb6b2ac16b | 107 | } |
davidmateos | 14:21cb6b2ac16b | 108 | |
davidmateos | 14:21cb6b2ac16b | 109 | |
javiervicente | 0:8a1a447db446 | 110 | } |
javiervicente | 0:8a1a447db446 | 111 | |
javiervicente | 0:8a1a447db446 | 112 | void estadoAbriendose() |
javiervicente | 0:8a1a447db446 | 113 | { |
davidmateos | 14:21cb6b2ac16b | 114 | |
davidmateos | 14:21cb6b2ac16b | 115 | if(x=='1') { |
davidmateos | 14:21cb6b2ac16b | 116 | //pc.printf("Distance =%d\n",distancia); |
davidmateos | 14:21cb6b2ac16b | 117 | if((finalDer==1)) { |
davidmateos | 14:21cb6b2ac16b | 118 | |
davidmateos | 14:21cb6b2ac16b | 119 | estado=abierta; |
davidmateos | 14:21cb6b2ac16b | 120 | enable=0; |
davidmateos | 14:21cb6b2ac16b | 121 | |
davidmateos | 14:21cb6b2ac16b | 122 | } else { |
davidmateos | 14:21cb6b2ac16b | 123 | gOled.clearDisplay(); |
davidmateos | 14:21cb6b2ac16b | 124 | |
davidmateos | 14:21cb6b2ac16b | 125 | paso(1); |
davidmateos | 14:21cb6b2ac16b | 126 | //pc.printf("paso\n"); |
davidmateos | 14:21cb6b2ac16b | 127 | |
davidmateos | 14:21cb6b2ac16b | 128 | } |
davidmateos | 11:ff1efbab2d9e | 129 | } |
davidmateos | 14:21cb6b2ac16b | 130 | // if (distancia<4){ |
davidmateos | 14:21cb6b2ac16b | 131 | //estado=cerrandose; |
davidmateos | 14:21cb6b2ac16b | 132 | |
davidmateos | 14:21cb6b2ac16b | 133 | // } |
davidmateos | 14:21cb6b2ac16b | 134 | if(y=='1') { |
davidmateos | 14:21cb6b2ac16b | 135 | |
davidmateos | 14:21cb6b2ac16b | 136 | |
davidmateos | 14:21cb6b2ac16b | 137 | if(tiempo.read()<0.5) { |
davidmateos | 14:21cb6b2ac16b | 138 | paso(1); |
davidmateos | 14:21cb6b2ac16b | 139 | } else if(finalIzq==0) { |
davidmateos | 14:21cb6b2ac16b | 140 | paso(0); |
davidmateos | 14:21cb6b2ac16b | 141 | } else { |
davidmateos | 14:21cb6b2ac16b | 142 | estado=cerrada; |
davidmateos | 14:21cb6b2ac16b | 143 | } |
davidmateos | 14:21cb6b2ac16b | 144 | |
davidmateos | 14:21cb6b2ac16b | 145 | } |
davidmateos | 14:21cb6b2ac16b | 146 | |
davidmateos | 14:21cb6b2ac16b | 147 | |
javiervicente | 0:8a1a447db446 | 148 | } |
javiervicente | 0:8a1a447db446 | 149 | |
davidmateos | 14:21cb6b2ac16b | 150 | void estadoCerrandose() |
davidmateos | 14:21cb6b2ac16b | 151 | { |
davidmateos | 14:21cb6b2ac16b | 152 | //pc.printf("Distance =%d\n",distancia); |
davidmateos | 14:21cb6b2ac16b | 153 | if(x=='1') { |
davidmateos | 14:21cb6b2ac16b | 154 | if((finalIzq==1)) { |
davidmateos | 14:21cb6b2ac16b | 155 | |
davidmateos | 14:21cb6b2ac16b | 156 | estado=cerrada; |
davidmateos | 14:21cb6b2ac16b | 157 | enable=0; |
davidmateos | 14:21cb6b2ac16b | 158 | } else { |
davidmateos | 14:21cb6b2ac16b | 159 | |
davidmateos | 14:21cb6b2ac16b | 160 | paso(0); |
davidmateos | 14:21cb6b2ac16b | 161 | //pc.printf("paso\n"); |
davidmateos | 14:21cb6b2ac16b | 162 | |
davidmateos | 14:21cb6b2ac16b | 163 | } |
javiervicente | 0:8a1a447db446 | 164 | } |
davidmateos | 14:21cb6b2ac16b | 165 | |
davidmateos | 14:21cb6b2ac16b | 166 | |
javiervicente | 0:8a1a447db446 | 167 | } |
javiervicente | 0:8a1a447db446 | 168 | |
javiervicente | 0:8a1a447db446 | 169 | void estadoAbierta() |
javiervicente | 0:8a1a447db446 | 170 | { |
davidmateos | 4:aa8a5a606384 | 171 | //pc.printf("Distance =%d\n",distancia); |
davidmateos | 5:0e6b049208d8 | 172 | //if (finalDer==1) { |
davidmateos | 14:21cb6b2ac16b | 173 | |
davidmateos | 14:21cb6b2ac16b | 174 | |
davidmateos | 14:21cb6b2ac16b | 175 | enable=1; |
davidmateos | 14:21cb6b2ac16b | 176 | estado=cerrandose; |
davidmateos | 14:21cb6b2ac16b | 177 | pc.printf("Final1 pulsado\r\n"); |
davidmateos | 14:21cb6b2ac16b | 178 | |
davidmateos | 5:0e6b049208d8 | 179 | //} |
davidmateos | 5:0e6b049208d8 | 180 | contador++; |
davidmateos | 9:d651ed116942 | 181 | gOled.clearDisplay(); |
davidmateos | 9:d651ed116942 | 182 | gOled.printf("piezas transportadas %d\n",contador); |
davidmateos | 13:d2c49ef3b2ac | 183 | gOled.printf("temp = %3.1f C\r\n", temp); |
davidmateos | 9:d651ed116942 | 184 | gOled.display(); |
davidmateos | 14:21cb6b2ac16b | 185 | gOled.setTextCursor(0,0); |
davidmateos | 14:21cb6b2ac16b | 186 | |
davidmateos | 14:21cb6b2ac16b | 187 | |
davidmateos | 14:21cb6b2ac16b | 188 | |
javiervicente | 0:8a1a447db446 | 189 | } |
javiervicente | 0:8a1a447db446 | 190 | |
javiervicente | 0:8a1a447db446 | 191 | int main() |
javiervicente | 0:8a1a447db446 | 192 | { |
javiervicente | 0:8a1a447db446 | 193 | pc.baud(115200); |
javiervicente | 0:8a1a447db446 | 194 | tickerMideDistancia.attach(&mideDistancia, 0.5); |
javiervicente | 0:8a1a447db446 | 195 | estado=cerrada; |
davidmateos | 14:21cb6b2ac16b | 196 | gOled.begin(); |
davidmateos | 9:d651ed116942 | 197 | gOled.clearDisplay(); |
davidmateos | 14:21cb6b2ac16b | 198 | gOled.printf("Hola\n"); |
davidmateos | 14:21cb6b2ac16b | 199 | gOled.display(); |
davidmateos | 14:21cb6b2ac16b | 200 | //x=(pc.getc()); |
davidmateos | 4:aa8a5a606384 | 201 | //pc.printf("Estado cerrada\n"); |
davidmateos | 14:21cb6b2ac16b | 202 | if(x==1) { |
davidmateos | 11:ff1efbab2d9e | 203 | gOled.clearDisplay(); |
davidmateos | 14:21cb6b2ac16b | 204 | gOled.printf("Programa de transporte de piezas\n"); |
davidmateos | 14:21cb6b2ac16b | 205 | gOled.display(); |
davidmateos | 14:21cb6b2ac16b | 206 | } |
davidmateos | 14:21cb6b2ac16b | 207 | |
davidmateos | 14:21cb6b2ac16b | 208 | if(y==1) { |
davidmateos | 11:ff1efbab2d9e | 209 | gOled.clearDisplay(); |
davidmateos | 14:21cb6b2ac16b | 210 | gOled.printf("Programa de taladro\n"); |
davidmateos | 14:21cb6b2ac16b | 211 | gOled.display(); |
davidmateos | 14:21cb6b2ac16b | 212 | } |
davidmateos | 11:ff1efbab2d9e | 213 | tiempo.reset(); |
davidmateos | 14:21cb6b2ac16b | 214 | |
davidmateos | 14:21cb6b2ac16b | 215 | int error = 0; |
davidmateos | 10:db8ef252faba | 216 | pc.baud(115200); |
davidmateos | 14:21cb6b2ac16b | 217 | |
davidmateos | 10:db8ef252faba | 218 | if(ds1820.begin()) { |
davidmateos | 10:db8ef252faba | 219 | while(1) { |
davidmateos | 14:21cb6b2ac16b | 220 | |
davidmateos | 14:21cb6b2ac16b | 221 | |
davidmateos | 10:db8ef252faba | 222 | ds1820.startConversion(); // start temperature conversion from analog to digital |
davidmateos | 14:21cb6b2ac16b | 223 | wait(1.0); // let DS1820 complete the temperature conversion |
davidmateos | 10:db8ef252faba | 224 | error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC) |
davidmateos | 10:db8ef252faba | 225 | switch(error) { |
davidmateos | 14:21cb6b2ac16b | 226 | case 0: // no errors -> 'temp' contains the value of measured temperature |
davidmateos | 14:21cb6b2ac16b | 227 | pc.printf("temp = %3.1f C\r\n", temp); |
davidmateos | 14:21cb6b2ac16b | 228 | break; |
davidmateos | 14:21cb6b2ac16b | 229 | case 1: // no sensor present -> 'temp' is not updated |
davidmateos | 14:21cb6b2ac16b | 230 | pc.printf("no sensor present\n\r"); |
davidmateos | 14:21cb6b2ac16b | 231 | break; |
davidmateos | 14:21cb6b2ac16b | 232 | case 2: // CRC error -> 'temp' is not updated |
davidmateos | 14:21cb6b2ac16b | 233 | pc.printf("CRC error\r\n"); |
davidmateos | 14:21cb6b2ac16b | 234 | } |
davidmateos | 10:db8ef252faba | 235 | led = !led; |
davidmateos | 10:db8ef252faba | 236 | } |
davidmateos | 10:db8ef252faba | 237 | } else |
davidmateos | 10:db8ef252faba | 238 | pc.printf("No DS1820 sensor found!\r\n"); |
davidmateos | 14:21cb6b2ac16b | 239 | |
davidmateos | 14:21cb6b2ac16b | 240 | |
davidmateos | 14:21cb6b2ac16b | 241 | |
davidmateos | 14:21cb6b2ac16b | 242 | |
davidmateos | 14:21cb6b2ac16b | 243 | |
javiervicente | 0:8a1a447db446 | 244 | while(1) { |
javiervicente | 0:8a1a447db446 | 245 | distancia=usensor.get_dist_cm(); |
davidmateos | 14:21cb6b2ac16b | 246 | if(pc.readable()) { |
davidmateos | 14:21cb6b2ac16b | 247 | x=(pc.getc()); |
davidmateos | 14:21cb6b2ac16b | 248 | } |
davidmateos | 11:ff1efbab2d9e | 249 | //calefactor |
davidmateos | 14:21cb6b2ac16b | 250 | // rele=1; |
davidmateos | 14:21cb6b2ac16b | 251 | // led=1; |
davidmateos | 11:ff1efbab2d9e | 252 | // wait(1.0); |
davidmateos | 14:21cb6b2ac16b | 253 | // rele=0; |
davidmateos | 11:ff1efbab2d9e | 254 | // led=0; |
davidmateos | 14:21cb6b2ac16b | 255 | // wait(1.0); |
davidmateos | 11:ff1efbab2d9e | 256 | // |
davidmateos | 13:d2c49ef3b2ac | 257 | if(pc.readable()) { |
davidmateos | 13:d2c49ef3b2ac | 258 | x=(pc.getc()); |
davidmateos | 13:d2c49ef3b2ac | 259 | } |
davidmateos | 14:21cb6b2ac16b | 260 | |
javiervicente | 0:8a1a447db446 | 261 | switch ( estado ) { |
javiervicente | 0:8a1a447db446 | 262 | case cerrada: |
javiervicente | 0:8a1a447db446 | 263 | estadoCerrada(); |
javiervicente | 0:8a1a447db446 | 264 | break; |
javiervicente | 0:8a1a447db446 | 265 | case abriendose: |
javiervicente | 0:8a1a447db446 | 266 | estadoAbriendose(); |
javiervicente | 0:8a1a447db446 | 267 | break; |
javiervicente | 0:8a1a447db446 | 268 | case abierta: |
javiervicente | 0:8a1a447db446 | 269 | estadoAbierta(); |
javiervicente | 0:8a1a447db446 | 270 | break; |
javiervicente | 0:8a1a447db446 | 271 | case cerrandose: |
javiervicente | 0:8a1a447db446 | 272 | estadoCerrandose(); |
javiervicente | 0:8a1a447db446 | 273 | break; |
javiervicente | 0:8a1a447db446 | 274 | default: |
javiervicente | 0:8a1a447db446 | 275 | break; |
javiervicente | 0:8a1a447db446 | 276 | } |
javiervicente | 0:8a1a447db446 | 277 | } |
javiervicente | 0:8a1a447db446 | 278 | } |