ultimaact

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
davidmateos
Date:
Tue Dec 14 09:38:10 2021 +0000
Revision:
10:db8ef252faba
Parent:
9:d651ed116942
Child:
11:ff1efbab2d9e
con temeratura

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 10:db8ef252faba 37 DS1820 ds1820(D10); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
davidmateos 10:db8ef252faba 38 float temp = 0;
davidmateos 5:0e6b049208d8 39 int contador=0;
davidmateos 5:0e6b049208d8 40
davidmateos 5:0e6b049208d8 41
javiervicente 0:8a1a447db446 42 enum estados {cerrada, abriendose, cerrandose, abierta};
javiervicente 0:8a1a447db446 43 estados estado;
javiervicente 0:8a1a447db446 44
javiervicente 0:8a1a447db446 45
javiervicente 0:8a1a447db446 46 void mideDistancia()
javiervicente 0:8a1a447db446 47 {
javiervicente 0:8a1a447db446 48 usensor.start();
javiervicente 0:8a1a447db446 49 }
javiervicente 0:8a1a447db446 50
javiervicente 0:8a1a447db446 51 void paso(int d)
javiervicente 0:8a1a447db446 52 {
javiervicente 0:8a1a447db446 53 dir=d;
javiervicente 0:8a1a447db446 54 step=1;
daniferca 2:828e6ac4800a 55 wait_us(100); // Este es el cambio de paso
javiervicente 0:8a1a447db446 56 step=0;
daniferca 2:828e6ac4800a 57 wait_us(900); //Este hay que cambiar
javiervicente 0:8a1a447db446 58 }
javiervicente 0:8a1a447db446 59
javiervicente 0:8a1a447db446 60 void estadoCerrada()
javiervicente 0:8a1a447db446 61 {
davidmateos 5:0e6b049208d8 62
davidmateos 5:0e6b049208d8 63 if( (finalIzq == 1) && contador <3) {
davidmateos 5:0e6b049208d8 64 wait(1.0);
javiervicente 0:8a1a447db446 65 enable=1;
javiervicente 0:8a1a447db446 66 estado=abriendose;
davidmateos 4:aa8a5a606384 67 // pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 68 pc.printf("Final2 pulsado\r\n");
daniferca 2:828e6ac4800a 69
davidmateos 4:aa8a5a606384 70
davidmateos 5:0e6b049208d8 71 //distancia < 5
javiervicente 0:8a1a447db446 72 }
davidmateos 5:0e6b049208d8 73
javiervicente 0:8a1a447db446 74 }
javiervicente 0:8a1a447db446 75
javiervicente 0:8a1a447db446 76 void estadoAbriendose()
javiervicente 0:8a1a447db446 77 {
davidmateos 5:0e6b049208d8 78 //pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 79 if((finalDer==1)) {
daniferca 1:1e68e882342d 80
javiervicente 0:8a1a447db446 81 estado=abierta;
javiervicente 0:8a1a447db446 82 enable=0;
daniferca 2:828e6ac4800a 83
daniferca 1:1e68e882342d 84 } else {
daniferca 2:828e6ac4800a 85
daniferca 2:828e6ac4800a 86 paso(1);
daniferca 2:828e6ac4800a 87 //pc.printf("paso\n");
javiervicente 0:8a1a447db446 88
javiervicente 0:8a1a447db446 89 }
davidmateos 5:0e6b049208d8 90 // if (distancia<4){
davidmateos 5:0e6b049208d8 91 //estado=cerrandose;
davidmateos 5:0e6b049208d8 92
davidmateos 5:0e6b049208d8 93 // }
javiervicente 0:8a1a447db446 94 }
javiervicente 0:8a1a447db446 95
javiervicente 0:8a1a447db446 96 void estadoCerrandose(){
davidmateos 4:aa8a5a606384 97 //pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 98 if((finalIzq==1)) {
daniferca 1:1e68e882342d 99
javiervicente 0:8a1a447db446 100 estado=cerrada;
javiervicente 0:8a1a447db446 101 enable=0;
daniferca 1:1e68e882342d 102 } else {
daniferca 2:828e6ac4800a 103
daniferca 2:828e6ac4800a 104 paso(0);
daniferca 2:828e6ac4800a 105 //pc.printf("paso\n");
daniferca 1:1e68e882342d 106
javiervicente 0:8a1a447db446 107 }
javiervicente 0:8a1a447db446 108 }
javiervicente 0:8a1a447db446 109
javiervicente 0:8a1a447db446 110 void estadoAbierta()
javiervicente 0:8a1a447db446 111 {
davidmateos 4:aa8a5a606384 112 //pc.printf("Distance =%d\n",distancia);
davidmateos 5:0e6b049208d8 113 //if (finalDer==1) {
daniferca 1:1e68e882342d 114
daniferca 2:828e6ac4800a 115
javiervicente 0:8a1a447db446 116 enable=1;
javiervicente 0:8a1a447db446 117 estado=cerrandose;
daniferca 2:828e6ac4800a 118 pc.printf("Final1 pulsado\r\n");
daniferca 1:1e68e882342d 119
davidmateos 5:0e6b049208d8 120 //}
davidmateos 5:0e6b049208d8 121 contador++;
davidmateos 9:d651ed116942 122 gOled.clearDisplay();
davidmateos 9:d651ed116942 123 gOled.printf("piezas transportadas %d\n",contador);
davidmateos 9:d651ed116942 124 gOled.display();
davidmateos 9:d651ed116942 125 gOled.setTextCursor(0,0);
javiervicente 0:8a1a447db446 126 }
javiervicente 0:8a1a447db446 127
javiervicente 0:8a1a447db446 128 int main()
javiervicente 0:8a1a447db446 129 {
javiervicente 0:8a1a447db446 130 pc.baud(115200);
javiervicente 0:8a1a447db446 131 tickerMideDistancia.attach(&mideDistancia, 0.5);
javiervicente 0:8a1a447db446 132 estado=cerrada;
davidmateos 9:d651ed116942 133 gOled.begin();
davidmateos 9:d651ed116942 134 gOled.clearDisplay();
davidmateos 9:d651ed116942 135 gOled.printf("Hola\n");
davidmateos 4:aa8a5a606384 136 //pc.printf("Estado cerrada\n");
davidmateos 8:a071c579f9ea 137
davidmateos 10:db8ef252faba 138 int error = 0;
davidmateos 10:db8ef252faba 139 pc.baud(115200);
davidmateos 10:db8ef252faba 140
davidmateos 10:db8ef252faba 141 if(ds1820.begin()) {
davidmateos 10:db8ef252faba 142 while(1) {
davidmateos 10:db8ef252faba 143 ds1820.startConversion(); // start temperature conversion from analog to digital
davidmateos 10:db8ef252faba 144 wait(1.0); // let DS1820 complete the temperature conversion
davidmateos 10:db8ef252faba 145 error = ds1820.read(temp); // read temperature from DS1820 and perform cyclic redundancy check (CRC)
davidmateos 10:db8ef252faba 146 switch(error) {
davidmateos 10:db8ef252faba 147 case 0: // no errors -> 'temp' contains the value of measured temperature
davidmateos 10:db8ef252faba 148 pc.printf("temp = %3.1f C\r\n", temp);
davidmateos 10:db8ef252faba 149 break;
davidmateos 10:db8ef252faba 150 case 1: // no sensor present -> 'temp' is not updated
davidmateos 10:db8ef252faba 151 pc.printf("no sensor present\n\r");
davidmateos 10:db8ef252faba 152 break;
davidmateos 10:db8ef252faba 153 case 2: // CRC error -> 'temp' is not updated
davidmateos 10:db8ef252faba 154 pc.printf("CRC error\r\n");
davidmateos 10:db8ef252faba 155 }
davidmateos 10:db8ef252faba 156 led = !led;
davidmateos 10:db8ef252faba 157 }
davidmateos 10:db8ef252faba 158 } else
davidmateos 10:db8ef252faba 159 pc.printf("No DS1820 sensor found!\r\n");
davidmateos 10:db8ef252faba 160
davidmateos 10:db8ef252faba 161
davidmateos 10:db8ef252faba 162
davidmateos 10:db8ef252faba 163
davidmateos 10:db8ef252faba 164
javiervicente 0:8a1a447db446 165 while(1) {
javiervicente 0:8a1a447db446 166 distancia=usensor.get_dist_cm();
davidmateos 9:d651ed116942 167
javiervicente 0:8a1a447db446 168 switch ( estado ) {
javiervicente 0:8a1a447db446 169 case cerrada:
javiervicente 0:8a1a447db446 170 estadoCerrada();
javiervicente 0:8a1a447db446 171 break;
javiervicente 0:8a1a447db446 172 case abriendose:
javiervicente 0:8a1a447db446 173 estadoAbriendose();
javiervicente 0:8a1a447db446 174 break;
javiervicente 0:8a1a447db446 175 case abierta:
javiervicente 0:8a1a447db446 176 estadoAbierta();
javiervicente 0:8a1a447db446 177 break;
javiervicente 0:8a1a447db446 178 case cerrandose:
javiervicente 0:8a1a447db446 179 estadoCerrandose();
javiervicente 0:8a1a447db446 180 break;
javiervicente 0:8a1a447db446 181 default:
javiervicente 0:8a1a447db446 182 break;
javiervicente 0:8a1a447db446 183 }
javiervicente 0:8a1a447db446 184 }
javiervicente 0:8a1a447db446 185 }