wss

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
davidmateos
Date:
Tue Dec 14 15:56:56 2021 +0000
Revision:
16:d49a029f2a1b
Parent:
15:2acad1c57ad4
Child:
17:74282523ba3e
ddd

Who changed what in which revision?

UserRevisionLine numberNew 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);
davidmateos 15:2acad1c57ad4 36 DigitalIn boton(D5);
davidmateos 16:d49a029f2a1b 37 DigitalIn switcher(D9);
javiervicente 0:8a1a447db446 38 DigitalOut step(D13);
javiervicente 0:8a1a447db446 39 DigitalOut dir(D12);
javiervicente 0:8a1a447db446 40 DigitalOut enable(D11);
davidmateos 14:21cb6b2ac16b 41 DigitalOut led(LED1);
davidmateos 14:21cb6b2ac16b 42 DS1820 ds1820(D10); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
davidmateos 11:ff1efbab2d9e 43
davidmateos 11:ff1efbab2d9e 44 //gitalOut rele(A5);
davidmateos 14:21cb6b2ac16b 45
davidmateos 10:db8ef252faba 46 float temp = 0;
davidmateos 5:0e6b049208d8 47 int contador=0;
davidmateos 5:0e6b049208d8 48
davidmateos 13:d2c49ef3b2ac 49
davidmateos 15:2acad1c57ad4 50 int x=0;
davidmateos 15:2acad1c57ad4 51 int y=0;
davidmateos 13:d2c49ef3b2ac 52 //if (x==1){
davidmateos 14:21cb6b2ac16b 53 //y=0;
davidmateos 14:21cb6b2ac16b 54 // }else{
davidmateos 14:21cb6b2ac16b 55 // y=1;
davidmateos 14:21cb6b2ac16b 56 // }
davidmateos 13:d2c49ef3b2ac 57
davidmateos 11:ff1efbab2d9e 58 ////////////////
davidmateos 11:ff1efbab2d9e 59 Timer tiempo;
davidmateos 5:0e6b049208d8 60
javiervicente 0:8a1a447db446 61 enum estados {cerrada, abriendose, cerrandose, abierta};
javiervicente 0:8a1a447db446 62 estados estado;
javiervicente 0:8a1a447db446 63
javiervicente 0:8a1a447db446 64
javiervicente 0:8a1a447db446 65 void mideDistancia()
javiervicente 0:8a1a447db446 66 {
javiervicente 0:8a1a447db446 67 usensor.start();
javiervicente 0:8a1a447db446 68 }
javiervicente 0:8a1a447db446 69
javiervicente 0:8a1a447db446 70 void paso(int d)
javiervicente 0:8a1a447db446 71 {
javiervicente 0:8a1a447db446 72 dir=d;
javiervicente 0:8a1a447db446 73 step=1;
daniferca 2:828e6ac4800a 74 wait_us(100); // Este es el cambio de paso
javiervicente 0:8a1a447db446 75 step=0;
daniferca 2:828e6ac4800a 76 wait_us(900); //Este hay que cambiar
javiervicente 0:8a1a447db446 77 }
javiervicente 0:8a1a447db446 78
javiervicente 0:8a1a447db446 79 void estadoCerrada()
javiervicente 0:8a1a447db446 80 {
davidmateos 15:2acad1c57ad4 81
davidmateos 15:2acad1c57ad4 82 if (x==1) {
davidmateos 14:21cb6b2ac16b 83 if( (finalIzq == 1) && contador <3) {
davidmateos 14:21cb6b2ac16b 84 wait(0.5);
davidmateos 14:21cb6b2ac16b 85 enable=1;
davidmateos 14:21cb6b2ac16b 86 estado=abriendose;
davidmateos 14:21cb6b2ac16b 87 // pc.printf("Distance =%d\n",distancia);
davidmateos 14:21cb6b2ac16b 88 pc.printf("Final2 pulsado\r\n");
davidmateos 14:21cb6b2ac16b 89
davidmateos 14:21cb6b2ac16b 90
davidmateos 14:21cb6b2ac16b 91 //distancia < 5
davidmateos 16:d49a029f2a1b 92 } //else if(contador==3) {
davidmateos 16:d49a029f2a1b 93 // x=0;
davidmateos 16:d49a029f2a1b 94 //y=1;
davidmateos 16:d49a029f2a1b 95 //contador=0;
davidmateos 16:d49a029f2a1b 96 //}
davidmateos 11:ff1efbab2d9e 97 }
davidmateos 15:2acad1c57ad4 98 if(x==0 &&y==0) {
davidmateos 11:ff1efbab2d9e 99 pc.printf("No seleccionado programa\r\n");
davidmateos 14:21cb6b2ac16b 100 }
davidmateos 14:21cb6b2ac16b 101
davidmateos 14:21cb6b2ac16b 102
davidmateos 15:2acad1c57ad4 103 if(y==1) {
davidmateos 16:d49a029f2a1b 104 if(finalIzq == 1 && boton==1) {
davidmateos 14:21cb6b2ac16b 105
davidmateos 14:21cb6b2ac16b 106 enable=1;
davidmateos 14:21cb6b2ac16b 107 tiempo.reset();
davidmateos 14:21cb6b2ac16b 108 tiempo.start();
davidmateos 14:21cb6b2ac16b 109 estado=abriendose;
davidmateos 11:ff1efbab2d9e 110 }
davidmateos 14:21cb6b2ac16b 111 }
davidmateos 14:21cb6b2ac16b 112
davidmateos 14:21cb6b2ac16b 113
javiervicente 0:8a1a447db446 114 }
javiervicente 0:8a1a447db446 115
javiervicente 0:8a1a447db446 116 void estadoAbriendose()
javiervicente 0:8a1a447db446 117 {
davidmateos 14:21cb6b2ac16b 118
davidmateos 15:2acad1c57ad4 119 if(x==1) {
davidmateos 14:21cb6b2ac16b 120 //pc.printf("Distance =%d\n",distancia);
davidmateos 14:21cb6b2ac16b 121 if((finalDer==1)) {
davidmateos 14:21cb6b2ac16b 122
davidmateos 14:21cb6b2ac16b 123 estado=abierta;
davidmateos 14:21cb6b2ac16b 124 enable=0;
davidmateos 14:21cb6b2ac16b 125
davidmateos 14:21cb6b2ac16b 126 } else {
davidmateos 14:21cb6b2ac16b 127 gOled.clearDisplay();
davidmateos 14:21cb6b2ac16b 128
davidmateos 14:21cb6b2ac16b 129 paso(1);
davidmateos 14:21cb6b2ac16b 130 //pc.printf("paso\n");
davidmateos 14:21cb6b2ac16b 131
davidmateos 14:21cb6b2ac16b 132 }
davidmateos 11:ff1efbab2d9e 133 }
davidmateos 14:21cb6b2ac16b 134 // if (distancia<4){
davidmateos 14:21cb6b2ac16b 135 //estado=cerrandose;
davidmateos 14:21cb6b2ac16b 136
davidmateos 14:21cb6b2ac16b 137 // }
davidmateos 15:2acad1c57ad4 138 if(y==1) {
davidmateos 14:21cb6b2ac16b 139
davidmateos 14:21cb6b2ac16b 140
davidmateos 14:21cb6b2ac16b 141 if(tiempo.read()<0.5) {
davidmateos 14:21cb6b2ac16b 142 paso(1);
davidmateos 14:21cb6b2ac16b 143 } else if(finalIzq==0) {
davidmateos 14:21cb6b2ac16b 144 paso(0);
davidmateos 14:21cb6b2ac16b 145 } else {
davidmateos 14:21cb6b2ac16b 146 estado=cerrada;
davidmateos 14:21cb6b2ac16b 147 }
davidmateos 14:21cb6b2ac16b 148
davidmateos 14:21cb6b2ac16b 149 }
davidmateos 14:21cb6b2ac16b 150
davidmateos 14:21cb6b2ac16b 151
javiervicente 0:8a1a447db446 152 }
javiervicente 0:8a1a447db446 153
davidmateos 14:21cb6b2ac16b 154 void estadoCerrandose()
davidmateos 14:21cb6b2ac16b 155 {
davidmateos 14:21cb6b2ac16b 156 //pc.printf("Distance =%d\n",distancia);
davidmateos 15:2acad1c57ad4 157 if(x==1) {
davidmateos 14:21cb6b2ac16b 158 if((finalIzq==1)) {
davidmateos 14:21cb6b2ac16b 159
davidmateos 14:21cb6b2ac16b 160 estado=cerrada;
davidmateos 14:21cb6b2ac16b 161 enable=0;
davidmateos 14:21cb6b2ac16b 162 } else {
davidmateos 14:21cb6b2ac16b 163
davidmateos 14:21cb6b2ac16b 164 paso(0);
davidmateos 14:21cb6b2ac16b 165 //pc.printf("paso\n");
davidmateos 14:21cb6b2ac16b 166
davidmateos 14:21cb6b2ac16b 167 }
javiervicente 0:8a1a447db446 168 }
davidmateos 14:21cb6b2ac16b 169
davidmateos 14:21cb6b2ac16b 170
javiervicente 0:8a1a447db446 171 }
javiervicente 0:8a1a447db446 172
javiervicente 0:8a1a447db446 173 void estadoAbierta()
javiervicente 0:8a1a447db446 174 {
davidmateos 4:aa8a5a606384 175 //pc.printf("Distance =%d\n",distancia);
davidmateos 5:0e6b049208d8 176 //if (finalDer==1) {
davidmateos 14:21cb6b2ac16b 177
davidmateos 14:21cb6b2ac16b 178
davidmateos 14:21cb6b2ac16b 179 enable=1;
davidmateos 14:21cb6b2ac16b 180 estado=cerrandose;
davidmateos 14:21cb6b2ac16b 181 pc.printf("Final1 pulsado\r\n");
davidmateos 14:21cb6b2ac16b 182
davidmateos 5:0e6b049208d8 183 //}
davidmateos 5:0e6b049208d8 184 contador++;
davidmateos 9:d651ed116942 185 gOled.clearDisplay();
davidmateos 9:d651ed116942 186 gOled.printf("piezas transportadas %d\n",contador);
davidmateos 13:d2c49ef3b2ac 187 gOled.printf("temp = %3.1f C\r\n", temp);
davidmateos 9:d651ed116942 188 gOled.display();
davidmateos 14:21cb6b2ac16b 189 gOled.setTextCursor(0,0);
davidmateos 15:2acad1c57ad4 190 pc.printf("temp = %3.1f C\r\n", temp);
davidmateos 14:21cb6b2ac16b 191
davidmateos 14:21cb6b2ac16b 192
javiervicente 0:8a1a447db446 193 }
javiervicente 0:8a1a447db446 194
javiervicente 0:8a1a447db446 195 int main()
javiervicente 0:8a1a447db446 196 {
javiervicente 0:8a1a447db446 197 pc.baud(115200);
javiervicente 0:8a1a447db446 198 tickerMideDistancia.attach(&mideDistancia, 0.5);
javiervicente 0:8a1a447db446 199 estado=cerrada;
davidmateos 14:21cb6b2ac16b 200 gOled.begin();
davidmateos 9:d651ed116942 201 gOled.clearDisplay();
davidmateos 14:21cb6b2ac16b 202 gOled.printf("Hola\n");
davidmateos 14:21cb6b2ac16b 203 gOled.display();
davidmateos 14:21cb6b2ac16b 204 //x=(pc.getc());
davidmateos 4:aa8a5a606384 205 //pc.printf("Estado cerrada\n");
davidmateos 15:2acad1c57ad4 206
davidmateos 15:2acad1c57ad4 207 if (boton ==1){
davidmateos 16:d49a029f2a1b 208 contador=0;
davidmateos 15:2acad1c57ad4 209 }
davidmateos 15:2acad1c57ad4 210
davidmateos 15:2acad1c57ad4 211
davidmateos 14:21cb6b2ac16b 212 if(x==1) {
davidmateos 11:ff1efbab2d9e 213 gOled.clearDisplay();
davidmateos 14:21cb6b2ac16b 214 gOled.printf("Programa de transporte de piezas\n");
davidmateos 14:21cb6b2ac16b 215 gOled.display();
davidmateos 14:21cb6b2ac16b 216 }
davidmateos 14:21cb6b2ac16b 217
davidmateos 14:21cb6b2ac16b 218 if(y==1) {
davidmateos 11:ff1efbab2d9e 219 gOled.clearDisplay();
davidmateos 14:21cb6b2ac16b 220 gOled.printf("Programa de taladro\n");
davidmateos 14:21cb6b2ac16b 221 gOled.display();
davidmateos 14:21cb6b2ac16b 222 }
davidmateos 11:ff1efbab2d9e 223 tiempo.reset();
davidmateos 14:21cb6b2ac16b 224
davidmateos 15:2acad1c57ad4 225 //int error = 0;
davidmateos 10:db8ef252faba 226 pc.baud(115200);
davidmateos 14:21cb6b2ac16b 227
davidmateos 15:2acad1c57ad4 228 //if(ds1820.begin()) {
davidmateos 15:2acad1c57ad4 229 //while(1) {
davidmateos 14:21cb6b2ac16b 230
davidmateos 14:21cb6b2ac16b 231
davidmateos 15:2acad1c57ad4 232 // ds1820.startConversion(); // start temperature conversion from analog to digital
davidmateos 15:2acad1c57ad4 233 // wait(1.0); // let DS1820 complete the temperature conversion
davidmateos 15:2acad1c57ad4 234 // error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
davidmateos 15:2acad1c57ad4 235 // switch(error) {
davidmateos 15:2acad1c57ad4 236 // case 0: // no errors -> 'temp' contains the value of measured temperature
davidmateos 15:2acad1c57ad4 237 // pc.printf("temp = %3.1f C\r\n", temp);
davidmateos 15:2acad1c57ad4 238 // break;
davidmateos 15:2acad1c57ad4 239 // case 1: // no sensor present -> 'temp' is not updated
davidmateos 15:2acad1c57ad4 240 // pc.printf("no sensor present\n\r");
davidmateos 15:2acad1c57ad4 241 // break;
davidmateos 15:2acad1c57ad4 242 // case 2: // CRC error -> 'temp' is not updated
davidmateos 15:2acad1c57ad4 243 // pc.printf("CRC error\r\n");
davidmateos 15:2acad1c57ad4 244 // }
davidmateos 15:2acad1c57ad4 245 // led = !led;
davidmateos 15:2acad1c57ad4 246 // }
davidmateos 15:2acad1c57ad4 247 // } else
davidmateos 15:2acad1c57ad4 248 // pc.printf("No DS1820 sensor found!\r\n");
davidmateos 14:21cb6b2ac16b 249
davidmateos 14:21cb6b2ac16b 250
davidmateos 14:21cb6b2ac16b 251
davidmateos 15:2acad1c57ad4 252 //if(x==1 || y==1){
davidmateos 14:21cb6b2ac16b 253
javiervicente 0:8a1a447db446 254 while(1) {
davidmateos 15:2acad1c57ad4 255
javiervicente 0:8a1a447db446 256 distancia=usensor.get_dist_cm();
davidmateos 16:d49a029f2a1b 257 if (switcher ==0){
davidmateos 16:d49a029f2a1b 258 y=0;
davidmateos 16:d49a029f2a1b 259 x=1;
davidmateos 16:d49a029f2a1b 260 }else if (switcher==1){
davidmateos 16:d49a029f2a1b 261 y=1;
davidmateos 16:d49a029f2a1b 262 x=0;
davidmateos 16:d49a029f2a1b 263 }
davidmateos 15:2acad1c57ad4 264
davidmateos 15:2acad1c57ad4 265 if (boton ==1){
davidmateos 16:d49a029f2a1b 266 contador=0;
davidmateos 15:2acad1c57ad4 267 }
davidmateos 15:2acad1c57ad4 268
davidmateos 11:ff1efbab2d9e 269 //calefactor
davidmateos 14:21cb6b2ac16b 270 // rele=1;
davidmateos 14:21cb6b2ac16b 271 // led=1;
davidmateos 11:ff1efbab2d9e 272 // wait(1.0);
davidmateos 14:21cb6b2ac16b 273 // rele=0;
davidmateos 11:ff1efbab2d9e 274 // led=0;
davidmateos 14:21cb6b2ac16b 275 // wait(1.0);
davidmateos 11:ff1efbab2d9e 276 //
davidmateos 15:2acad1c57ad4 277 // if(ds1820.begin()) {
davidmateos 15:2acad1c57ad4 278 // ds1820.startConversion(); // start temperature conversion from analog to digital
davidmateos 15:2acad1c57ad4 279 // wait(1.0); // let DS1820 complete the temperature conversion
davidmateos 15:2acad1c57ad4 280 // error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
davidmateos 15:2acad1c57ad4 281 // switch(error) {
davidmateos 15:2acad1c57ad4 282 // case 0: // no errors -> 'temp' contains the value of measured temperature
davidmateos 15:2acad1c57ad4 283 // pc.printf("temp = %3.1f C\r\n", temp);
davidmateos 15:2acad1c57ad4 284 // break;
davidmateos 15:2acad1c57ad4 285 // case 1: // no sensor present -> 'temp' is not updated
davidmateos 15:2acad1c57ad4 286 // pc.printf("no sensor present\n\r");
davidmateos 15:2acad1c57ad4 287 // break;
davidmateos 15:2acad1c57ad4 288 // case 2: // CRC error -> 'temp' is not updated
davidmateos 15:2acad1c57ad4 289 // pc.printf("CRC error\r\n");
davidmateos 15:2acad1c57ad4 290 // }
davidmateos 15:2acad1c57ad4 291 // led = !led;
davidmateos 15:2acad1c57ad4 292 // }
davidmateos 15:2acad1c57ad4 293 // else
davidmateos 15:2acad1c57ad4 294 // pc.printf("No DS1820 sensor found!\r\n");
davidmateos 14:21cb6b2ac16b 295
javiervicente 0:8a1a447db446 296 switch ( estado ) {
javiervicente 0:8a1a447db446 297 case cerrada:
javiervicente 0:8a1a447db446 298 estadoCerrada();
javiervicente 0:8a1a447db446 299 break;
javiervicente 0:8a1a447db446 300 case abriendose:
javiervicente 0:8a1a447db446 301 estadoAbriendose();
javiervicente 0:8a1a447db446 302 break;
javiervicente 0:8a1a447db446 303 case abierta:
javiervicente 0:8a1a447db446 304 estadoAbierta();
javiervicente 0:8a1a447db446 305 break;
javiervicente 0:8a1a447db446 306 case cerrandose:
javiervicente 0:8a1a447db446 307 estadoCerrandose();
javiervicente 0:8a1a447db446 308 break;
javiervicente 0:8a1a447db446 309 default:
javiervicente 0:8a1a447db446 310 break;
javiervicente 0:8a1a447db446 311 }
davidmateos 15:2acad1c57ad4 312
davidmateos 15:2acad1c57ad4 313
javiervicente 0:8a1a447db446 314 }
davidmateos 15:2acad1c57ad4 315 }
davidmateos 15:2acad1c57ad4 316 //}