wss

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
davidmateos
Date:
Tue Dec 14 14:59:26 2021 +0000
Revision:
15:2acad1c57ad4
Parent:
14:21cb6b2ac16b
ddwdd

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