wss

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
davidmateos
Date:
Tue Dec 14 12:27:41 2021 +0000
Revision:
13:d2c49ef3b2ac
Parent:
12:71ae2f729b7f
Child:
14:21cb6b2ac16b
ULTIMA ACT

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 8:a071c579f9ea 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 8:a071c579f9ea 17
davidmateos 8:a071c579f9ea 18
davidmateos 6:23dfd7b0e58c 19 I2CPreInit gI2C(PB_9,PB_8);
davidmateos 8:a071c579f9ea 20
davidmateos 6:23dfd7b0e58c 21 // an SPI sub-class that provides a constructed default
davidmateos 8:a071c579f9ea 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 10:db8ef252faba 39 DigitalOut led(LED1);
davidmateos 11:ff1efbab2d9e 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 11:ff1efbab2d9e 43
davidmateos 10:db8ef252faba 44 float temp = 0;
davidmateos 5:0e6b049208d8 45 int contador=0;
davidmateos 5:0e6b049208d8 46
davidmateos 13:d2c49ef3b2ac 47
davidmateos 13:d2c49ef3b2ac 48 int x=1;
davidmateos 13:d2c49ef3b2ac 49 int y=0;
davidmateos 13:d2c49ef3b2ac 50 //if (x==1){
davidmateos 13:d2c49ef3b2ac 51 //y=0;
davidmateos 13:d2c49ef3b2ac 52 // }else{
davidmateos 13:d2c49ef3b2ac 53 // y=1;
davidmateos 13:d2c49ef3b2ac 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 11:ff1efbab2d9e 79 if (x==1){
davidmateos 5:0e6b049208d8 80 if( (finalIzq == 1) && contador <3) {
davidmateos 11:ff1efbab2d9e 81 wait(0.5);
javiervicente 0:8a1a447db446 82 enable=1;
javiervicente 0:8a1a447db446 83 estado=abriendose;
davidmateos 4:aa8a5a606384 84 // pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 85 pc.printf("Final2 pulsado\r\n");
daniferca 2:828e6ac4800a 86
davidmateos 4:aa8a5a606384 87
davidmateos 5:0e6b049208d8 88 //distancia < 5
davidmateos 13:d2c49ef3b2ac 89 }else if(contador==3){
davidmateos 13:d2c49ef3b2ac 90 y=1;
davidmateos 13:d2c49ef3b2ac 91 x=0;
davidmateos 13:d2c49ef3b2ac 92 }
davidmateos 11:ff1efbab2d9e 93 }
davidmateos 11:ff1efbab2d9e 94 if(x==0 &&y==0){
davidmateos 11:ff1efbab2d9e 95 pc.printf("No seleccionado programa\r\n");
davidmateos 11:ff1efbab2d9e 96 }
davidmateos 11:ff1efbab2d9e 97
davidmateos 11:ff1efbab2d9e 98
davidmateos 11:ff1efbab2d9e 99 if(y==1){
davidmateos 11:ff1efbab2d9e 100 if(finalIzq == 1 && finalDer==1){
davidmateos 11:ff1efbab2d9e 101
davidmateos 11:ff1efbab2d9e 102 enable=1;
davidmateos 11:ff1efbab2d9e 103 tiempo.reset();
davidmateos 11:ff1efbab2d9e 104 tiempo.start();
davidmateos 11:ff1efbab2d9e 105 estado=abriendose;
davidmateos 11:ff1efbab2d9e 106 }
davidmateos 11:ff1efbab2d9e 107 }
davidmateos 11:ff1efbab2d9e 108
davidmateos 13:d2c49ef3b2ac 109
javiervicente 0:8a1a447db446 110 }
javiervicente 0:8a1a447db446 111
javiervicente 0:8a1a447db446 112 void estadoAbriendose()
javiervicente 0:8a1a447db446 113 {
davidmateos 11:ff1efbab2d9e 114
davidmateos 11:ff1efbab2d9e 115 if(x==1){
davidmateos 5:0e6b049208d8 116 //pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 117 if((finalDer==1)) {
daniferca 1:1e68e882342d 118
javiervicente 0:8a1a447db446 119 estado=abierta;
javiervicente 0:8a1a447db446 120 enable=0;
daniferca 2:828e6ac4800a 121
daniferca 1:1e68e882342d 122 } else {
davidmateos 11:ff1efbab2d9e 123 gOled.clearDisplay();
davidmateos 11:ff1efbab2d9e 124
daniferca 2:828e6ac4800a 125 paso(1);
daniferca 2:828e6ac4800a 126 //pc.printf("paso\n");
javiervicente 0:8a1a447db446 127
javiervicente 0:8a1a447db446 128 }
davidmateos 11:ff1efbab2d9e 129 }
davidmateos 5:0e6b049208d8 130 // if (distancia<4){
davidmateos 5:0e6b049208d8 131 //estado=cerrandose;
davidmateos 5:0e6b049208d8 132
davidmateos 5:0e6b049208d8 133 // }
davidmateos 11:ff1efbab2d9e 134 if(y==1){
davidmateos 11:ff1efbab2d9e 135
davidmateos 11:ff1efbab2d9e 136
davidmateos 12:71ae2f729b7f 137 if(tiempo.read()<0.5){
davidmateos 11:ff1efbab2d9e 138 paso(1);
davidmateos 11:ff1efbab2d9e 139 }
davidmateos 11:ff1efbab2d9e 140 else if(finalIzq==0){
davidmateos 11:ff1efbab2d9e 141 paso(0);
davidmateos 11:ff1efbab2d9e 142 }
davidmateos 11:ff1efbab2d9e 143 else{
davidmateos 11:ff1efbab2d9e 144 estado=cerrada;
davidmateos 11:ff1efbab2d9e 145 }
davidmateos 11:ff1efbab2d9e 146
davidmateos 11:ff1efbab2d9e 147 }
davidmateos 13:d2c49ef3b2ac 148
davidmateos 11:ff1efbab2d9e 149
javiervicente 0:8a1a447db446 150 }
javiervicente 0:8a1a447db446 151
javiervicente 0:8a1a447db446 152 void estadoCerrandose(){
davidmateos 4:aa8a5a606384 153 //pc.printf("Distance =%d\n",distancia);
davidmateos 11:ff1efbab2d9e 154 if(x==1){
daniferca 2:828e6ac4800a 155 if((finalIzq==1)) {
daniferca 1:1e68e882342d 156
javiervicente 0:8a1a447db446 157 estado=cerrada;
javiervicente 0:8a1a447db446 158 enable=0;
daniferca 1:1e68e882342d 159 } else {
davidmateos 11:ff1efbab2d9e 160
daniferca 2:828e6ac4800a 161 paso(0);
daniferca 2:828e6ac4800a 162 //pc.printf("paso\n");
daniferca 1:1e68e882342d 163
javiervicente 0:8a1a447db446 164 }
davidmateos 11:ff1efbab2d9e 165 }
davidmateos 13:d2c49ef3b2ac 166
davidmateos 11:ff1efbab2d9e 167
javiervicente 0:8a1a447db446 168 }
javiervicente 0:8a1a447db446 169
javiervicente 0:8a1a447db446 170 void estadoAbierta()
javiervicente 0:8a1a447db446 171 {
davidmateos 4:aa8a5a606384 172 //pc.printf("Distance =%d\n",distancia);
davidmateos 5:0e6b049208d8 173 //if (finalDer==1) {
daniferca 1:1e68e882342d 174
daniferca 2:828e6ac4800a 175
javiervicente 0:8a1a447db446 176 enable=1;
javiervicente 0:8a1a447db446 177 estado=cerrandose;
daniferca 2:828e6ac4800a 178 pc.printf("Final1 pulsado\r\n");
daniferca 1:1e68e882342d 179
davidmateos 5:0e6b049208d8 180 //}
davidmateos 5:0e6b049208d8 181 contador++;
davidmateos 9:d651ed116942 182 gOled.clearDisplay();
davidmateos 9:d651ed116942 183 gOled.printf("piezas transportadas %d\n",contador);
davidmateos 13:d2c49ef3b2ac 184 gOled.printf("temp = %3.1f C\r\n", temp);
davidmateos 9:d651ed116942 185 gOled.display();
davidmateos 9:d651ed116942 186 gOled.setTextCursor(0,0);
davidmateos 13:d2c49ef3b2ac 187
davidmateos 13:d2c49ef3b2ac 188
davidmateos 13:d2c49ef3b2ac 189
javiervicente 0:8a1a447db446 190 }
javiervicente 0:8a1a447db446 191
javiervicente 0:8a1a447db446 192 int main()
javiervicente 0:8a1a447db446 193 {
javiervicente 0:8a1a447db446 194 pc.baud(115200);
javiervicente 0:8a1a447db446 195 tickerMideDistancia.attach(&mideDistancia, 0.5);
javiervicente 0:8a1a447db446 196 estado=cerrada;
davidmateos 9:d651ed116942 197 gOled.begin();
davidmateos 9:d651ed116942 198 gOled.clearDisplay();
davidmateos 9:d651ed116942 199 gOled.printf("Hola\n");
davidmateos 11:ff1efbab2d9e 200 gOled.display();
davidmateos 13:d2c49ef3b2ac 201 //x=(pc.getc());
davidmateos 4:aa8a5a606384 202 //pc.printf("Estado cerrada\n");
davidmateos 11:ff1efbab2d9e 203 if(x==1){
davidmateos 11:ff1efbab2d9e 204 gOled.clearDisplay();
davidmateos 12:71ae2f729b7f 205 gOled.printf("Programa de transporte de piezas\n");
davidmateos 11:ff1efbab2d9e 206 gOled.display();
davidmateos 11:ff1efbab2d9e 207 }
davidmateos 11:ff1efbab2d9e 208
davidmateos 11:ff1efbab2d9e 209 if(y==1){
davidmateos 11:ff1efbab2d9e 210 gOled.clearDisplay();
davidmateos 12:71ae2f729b7f 211 gOled.printf("Programa de taladro\n");
davidmateos 11:ff1efbab2d9e 212 gOled.display();
davidmateos 11:ff1efbab2d9e 213 }
davidmateos 11:ff1efbab2d9e 214 tiempo.reset();
davidmateos 8:a071c579f9ea 215
davidmateos 10:db8ef252faba 216 int error = 0;
davidmateos 10:db8ef252faba 217 pc.baud(115200);
davidmateos 10:db8ef252faba 218
davidmateos 10:db8ef252faba 219 if(ds1820.begin()) {
davidmateos 10:db8ef252faba 220 while(1) {
davidmateos 13:d2c49ef3b2ac 221
davidmateos 13:d2c49ef3b2ac 222
davidmateos 10:db8ef252faba 223 ds1820.startConversion(); // start temperature conversion from analog to digital
davidmateos 10:db8ef252faba 224 wait(1.0); // let DS1820 complete the temperature conversion
davidmateos 10:db8ef252faba 225 error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
davidmateos 10:db8ef252faba 226 switch(error) {
davidmateos 10:db8ef252faba 227 case 0: // no errors -> 'temp' contains the value of measured temperature
davidmateos 10:db8ef252faba 228 pc.printf("temp = %3.1f C\r\n", temp);
davidmateos 10:db8ef252faba 229 break;
davidmateos 10:db8ef252faba 230 case 1: // no sensor present -> 'temp' is not updated
davidmateos 10:db8ef252faba 231 pc.printf("no sensor present\n\r");
davidmateos 10:db8ef252faba 232 break;
davidmateos 10:db8ef252faba 233 case 2: // CRC error -> 'temp' is not updated
davidmateos 10:db8ef252faba 234 pc.printf("CRC error\r\n");
davidmateos 10:db8ef252faba 235 }
davidmateos 10:db8ef252faba 236 led = !led;
davidmateos 10:db8ef252faba 237 }
davidmateos 10:db8ef252faba 238 } else
davidmateos 10:db8ef252faba 239 pc.printf("No DS1820 sensor found!\r\n");
davidmateos 10:db8ef252faba 240
davidmateos 10:db8ef252faba 241
davidmateos 10:db8ef252faba 242
davidmateos 10:db8ef252faba 243
davidmateos 10:db8ef252faba 244
javiervicente 0:8a1a447db446 245 while(1) {
javiervicente 0:8a1a447db446 246 distancia=usensor.get_dist_cm();
davidmateos 9:d651ed116942 247
davidmateos 11:ff1efbab2d9e 248 //calefactor
davidmateos 11:ff1efbab2d9e 249 // rele=1;
davidmateos 11:ff1efbab2d9e 250 // led=1;
davidmateos 11:ff1efbab2d9e 251 // wait(1.0);
davidmateos 11:ff1efbab2d9e 252 // rele=0;
davidmateos 11:ff1efbab2d9e 253 // led=0;
davidmateos 11:ff1efbab2d9e 254 // wait(1.0);
davidmateos 11:ff1efbab2d9e 255 //
davidmateos 13:d2c49ef3b2ac 256 if(pc.readable()) {
davidmateos 13:d2c49ef3b2ac 257 x=(pc.getc());
davidmateos 13:d2c49ef3b2ac 258 }
davidmateos 11:ff1efbab2d9e 259
javiervicente 0:8a1a447db446 260 switch ( estado ) {
javiervicente 0:8a1a447db446 261 case cerrada:
javiervicente 0:8a1a447db446 262 estadoCerrada();
javiervicente 0:8a1a447db446 263 break;
javiervicente 0:8a1a447db446 264 case abriendose:
javiervicente 0:8a1a447db446 265 estadoAbriendose();
javiervicente 0:8a1a447db446 266 break;
javiervicente 0:8a1a447db446 267 case abierta:
javiervicente 0:8a1a447db446 268 estadoAbierta();
javiervicente 0:8a1a447db446 269 break;
javiervicente 0:8a1a447db446 270 case cerrandose:
javiervicente 0:8a1a447db446 271 estadoCerrandose();
javiervicente 0:8a1a447db446 272 break;
javiervicente 0:8a1a447db446 273 default:
javiervicente 0:8a1a447db446 274 break;
javiervicente 0:8a1a447db446 275 }
javiervicente 0:8a1a447db446 276 }
javiervicente 0:8a1a447db446 277 }