ultimaact

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
davidmateos
Date:
Mon Dec 13 19:51:37 2021 +0000
Revision:
6:23dfd7b0e58c
Parent:
5:0e6b049208d8
Child:
7:296b42f24d44
act ultima

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 6:23dfd7b0e58c 3 #include "Adafruit_SSD1306.h"
davidmateos 6:23dfd7b0e58c 4
davidmateos 6:23dfd7b0e58c 5 class I2CPreInit : public I2C
davidmateos 6:23dfd7b0e58c 6 {
davidmateos 6:23dfd7b0e58c 7 public:
davidmateos 6:23dfd7b0e58c 8 I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl)
davidmateos 6:23dfd7b0e58c 9 {
davidmateos 6:23dfd7b0e58c 10 frequency(100000);
davidmateos 6:23dfd7b0e58c 11 start();
davidmateos 6:23dfd7b0e58c 12 };
davidmateos 6:23dfd7b0e58c 13 };
javiervicente 0:8a1a447db446 14
daniferca 1:1e68e882342d 15
davidmateos 6:23dfd7b0e58c 16 I2CPreInit gI2C(PB_9,PB_8);
davidmateos 6:23dfd7b0e58c 17
davidmateos 6:23dfd7b0e58c 18 // an SPI sub-class that provides a constructed default
davidmateos 6:23dfd7b0e58c 19
davidmateos 6:23dfd7b0e58c 20 Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128);
daniferca 1:1e68e882342d 21
javiervicente 0:8a1a447db446 22 Serial pc(USBTX, USBRX); // tx, rx
javiervicente 0:8a1a447db446 23 Ticker tickerMideDistancia;
davidmateos 4:aa8a5a606384 24 unsigned distancia=20;
javiervicente 0:8a1a447db446 25
daniferca 1:1e68e882342d 26 HCSR04 usensor(D7,D8); //(PinName TrigPin,PinName EchoPin):
daniferca 2:828e6ac4800a 27 DigitalIn finalDer(D2);
daniferca 2:828e6ac4800a 28 DigitalIn finalIzq(D3);
javiervicente 0:8a1a447db446 29 DigitalOut step(D13);
javiervicente 0:8a1a447db446 30 DigitalOut dir(D12);
javiervicente 0:8a1a447db446 31 DigitalOut enable(D11);
javiervicente 0:8a1a447db446 32
davidmateos 5:0e6b049208d8 33 int contador=0;
davidmateos 5:0e6b049208d8 34
davidmateos 5:0e6b049208d8 35
javiervicente 0:8a1a447db446 36 enum estados {cerrada, abriendose, cerrandose, abierta};
javiervicente 0:8a1a447db446 37 estados estado;
javiervicente 0:8a1a447db446 38
javiervicente 0:8a1a447db446 39
javiervicente 0:8a1a447db446 40 void mideDistancia()
javiervicente 0:8a1a447db446 41 {
javiervicente 0:8a1a447db446 42 usensor.start();
javiervicente 0:8a1a447db446 43 }
javiervicente 0:8a1a447db446 44
javiervicente 0:8a1a447db446 45 void paso(int d)
javiervicente 0:8a1a447db446 46 {
javiervicente 0:8a1a447db446 47 dir=d;
javiervicente 0:8a1a447db446 48 step=1;
daniferca 2:828e6ac4800a 49 wait_us(100); // Este es el cambio de paso
javiervicente 0:8a1a447db446 50 step=0;
daniferca 2:828e6ac4800a 51 wait_us(900); //Este hay que cambiar
javiervicente 0:8a1a447db446 52 }
javiervicente 0:8a1a447db446 53
javiervicente 0:8a1a447db446 54 void estadoCerrada()
javiervicente 0:8a1a447db446 55 {
davidmateos 5:0e6b049208d8 56
davidmateos 5:0e6b049208d8 57 if( (finalIzq == 1) && contador <3) {
davidmateos 5:0e6b049208d8 58 wait(1.0);
javiervicente 0:8a1a447db446 59 enable=1;
javiervicente 0:8a1a447db446 60 estado=abriendose;
davidmateos 4:aa8a5a606384 61 // pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 62 pc.printf("Final2 pulsado\r\n");
daniferca 2:828e6ac4800a 63
davidmateos 4:aa8a5a606384 64
davidmateos 5:0e6b049208d8 65 //distancia < 5
javiervicente 0:8a1a447db446 66 }
davidmateos 5:0e6b049208d8 67
javiervicente 0:8a1a447db446 68 }
javiervicente 0:8a1a447db446 69
javiervicente 0:8a1a447db446 70 void estadoAbriendose()
javiervicente 0:8a1a447db446 71 {
davidmateos 5:0e6b049208d8 72 //pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 73 if((finalDer==1)) {
daniferca 1:1e68e882342d 74
javiervicente 0:8a1a447db446 75 estado=abierta;
javiervicente 0:8a1a447db446 76 enable=0;
daniferca 2:828e6ac4800a 77
daniferca 1:1e68e882342d 78 } else {
daniferca 2:828e6ac4800a 79
daniferca 2:828e6ac4800a 80 paso(1);
daniferca 2:828e6ac4800a 81 //pc.printf("paso\n");
javiervicente 0:8a1a447db446 82
javiervicente 0:8a1a447db446 83 }
davidmateos 5:0e6b049208d8 84 // if (distancia<4){
davidmateos 5:0e6b049208d8 85 //estado=cerrandose;
davidmateos 5:0e6b049208d8 86
davidmateos 5:0e6b049208d8 87 // }
javiervicente 0:8a1a447db446 88 }
javiervicente 0:8a1a447db446 89
javiervicente 0:8a1a447db446 90 void estadoCerrandose(){
davidmateos 4:aa8a5a606384 91 //pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 92 if((finalIzq==1)) {
daniferca 1:1e68e882342d 93
javiervicente 0:8a1a447db446 94 estado=cerrada;
javiervicente 0:8a1a447db446 95 enable=0;
daniferca 1:1e68e882342d 96 } else {
daniferca 2:828e6ac4800a 97
daniferca 2:828e6ac4800a 98 paso(0);
daniferca 2:828e6ac4800a 99 //pc.printf("paso\n");
daniferca 1:1e68e882342d 100
javiervicente 0:8a1a447db446 101 }
javiervicente 0:8a1a447db446 102 }
javiervicente 0:8a1a447db446 103
javiervicente 0:8a1a447db446 104 void estadoAbierta()
javiervicente 0:8a1a447db446 105 {
davidmateos 4:aa8a5a606384 106 //pc.printf("Distance =%d\n",distancia);
davidmateos 5:0e6b049208d8 107 //if (finalDer==1) {
daniferca 1:1e68e882342d 108
daniferca 2:828e6ac4800a 109
javiervicente 0:8a1a447db446 110 enable=1;
javiervicente 0:8a1a447db446 111 estado=cerrandose;
daniferca 2:828e6ac4800a 112 pc.printf("Final1 pulsado\r\n");
daniferca 1:1e68e882342d 113
davidmateos 5:0e6b049208d8 114 //}
davidmateos 5:0e6b049208d8 115 contador++;
davidmateos 6:23dfd7b0e58c 116 gOled.clearDisplay();
davidmateos 6:23dfd7b0e58c 117 gOled.printf("numero %d\n",contador);
davidmateos 6:23dfd7b0e58c 118 gOled.display();
davidmateos 6:23dfd7b0e58c 119 gOled.setTextCursor(0,0);
davidmateos 6:23dfd7b0e58c 120 //wait(1.000);
davidmateos 6:23dfd7b0e58c 121
javiervicente 0:8a1a447db446 122 }
javiervicente 0:8a1a447db446 123
javiervicente 0:8a1a447db446 124 int main()
javiervicente 0:8a1a447db446 125 {
javiervicente 0:8a1a447db446 126 pc.baud(115200);
javiervicente 0:8a1a447db446 127 tickerMideDistancia.attach(&mideDistancia, 0.5);
davidmateos 6:23dfd7b0e58c 128 gOled.begin();
davidmateos 6:23dfd7b0e58c 129 gOled.clearDisplay();
javiervicente 0:8a1a447db446 130 estado=cerrada;
davidmateos 5:0e6b049208d8 131
davidmateos 4:aa8a5a606384 132 //pc.printf("Estado cerrada\n");
davidmateos 6:23dfd7b0e58c 133 gOled.printf("Buenos dias\n");
davidmateos 6:23dfd7b0e58c 134
davidmateos 6:23dfd7b0e58c 135
davidmateos 6:23dfd7b0e58c 136
javiervicente 0:8a1a447db446 137 while(1) {
javiervicente 0:8a1a447db446 138 distancia=usensor.get_dist_cm();
davidmateos 6:23dfd7b0e58c 139
javiervicente 0:8a1a447db446 140 switch ( estado ) {
javiervicente 0:8a1a447db446 141 case cerrada:
javiervicente 0:8a1a447db446 142 estadoCerrada();
javiervicente 0:8a1a447db446 143 break;
javiervicente 0:8a1a447db446 144 case abriendose:
javiervicente 0:8a1a447db446 145 estadoAbriendose();
javiervicente 0:8a1a447db446 146 break;
javiervicente 0:8a1a447db446 147 case abierta:
javiervicente 0:8a1a447db446 148 estadoAbierta();
javiervicente 0:8a1a447db446 149 break;
javiervicente 0:8a1a447db446 150 case cerrandose:
javiervicente 0:8a1a447db446 151 estadoCerrandose();
javiervicente 0:8a1a447db446 152 break;
javiervicente 0:8a1a447db446 153 default:
javiervicente 0:8a1a447db446 154 break;
davidmateos 6:23dfd7b0e58c 155
javiervicente 0:8a1a447db446 156 }
davidmateos 6:23dfd7b0e58c 157
davidmateos 6:23dfd7b0e58c 158
javiervicente 0:8a1a447db446 159 }
davidmateos 6:23dfd7b0e58c 160
javiervicente 0:8a1a447db446 161 }