ultimaact

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
davidmateos
Date:
Tue Dec 14 11:02:12 2021 +0000
Revision:
11:ff1efbab2d9e
Parent:
10:db8ef252faba
mod taladro

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