wss

Dependencies:   mbed Adafruit_GFX DS1820

Committer:
davidmateos
Date:
Tue Dec 14 08:57:43 2021 +0000
Revision:
9:d651ed116942
Parent:
8:a071c579f9ea
Child:
10:db8ef252faba
printf bien hecho

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 8:a071c579f9ea 3
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);
javiervicente 0:8a1a447db446 36
davidmateos 5:0e6b049208d8 37 int contador=0;
davidmateos 5:0e6b049208d8 38
davidmateos 5:0e6b049208d8 39
javiervicente 0:8a1a447db446 40 enum estados {cerrada, abriendose, cerrandose, abierta};
javiervicente 0:8a1a447db446 41 estados estado;
javiervicente 0:8a1a447db446 42
javiervicente 0:8a1a447db446 43
javiervicente 0:8a1a447db446 44 void mideDistancia()
javiervicente 0:8a1a447db446 45 {
javiervicente 0:8a1a447db446 46 usensor.start();
javiervicente 0:8a1a447db446 47 }
javiervicente 0:8a1a447db446 48
javiervicente 0:8a1a447db446 49 void paso(int d)
javiervicente 0:8a1a447db446 50 {
javiervicente 0:8a1a447db446 51 dir=d;
javiervicente 0:8a1a447db446 52 step=1;
daniferca 2:828e6ac4800a 53 wait_us(100); // Este es el cambio de paso
javiervicente 0:8a1a447db446 54 step=0;
daniferca 2:828e6ac4800a 55 wait_us(900); //Este hay que cambiar
javiervicente 0:8a1a447db446 56 }
javiervicente 0:8a1a447db446 57
javiervicente 0:8a1a447db446 58 void estadoCerrada()
javiervicente 0:8a1a447db446 59 {
davidmateos 5:0e6b049208d8 60
davidmateos 5:0e6b049208d8 61 if( (finalIzq == 1) && contador <3) {
davidmateos 5:0e6b049208d8 62 wait(1.0);
javiervicente 0:8a1a447db446 63 enable=1;
javiervicente 0:8a1a447db446 64 estado=abriendose;
davidmateos 4:aa8a5a606384 65 // pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 66 pc.printf("Final2 pulsado\r\n");
daniferca 2:828e6ac4800a 67
davidmateos 4:aa8a5a606384 68
davidmateos 5:0e6b049208d8 69 //distancia < 5
javiervicente 0:8a1a447db446 70 }
davidmateos 5:0e6b049208d8 71
javiervicente 0:8a1a447db446 72 }
javiervicente 0:8a1a447db446 73
javiervicente 0:8a1a447db446 74 void estadoAbriendose()
javiervicente 0:8a1a447db446 75 {
davidmateos 5:0e6b049208d8 76 //pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 77 if((finalDer==1)) {
daniferca 1:1e68e882342d 78
javiervicente 0:8a1a447db446 79 estado=abierta;
javiervicente 0:8a1a447db446 80 enable=0;
daniferca 2:828e6ac4800a 81
daniferca 1:1e68e882342d 82 } else {
daniferca 2:828e6ac4800a 83
daniferca 2:828e6ac4800a 84 paso(1);
daniferca 2:828e6ac4800a 85 //pc.printf("paso\n");
javiervicente 0:8a1a447db446 86
javiervicente 0:8a1a447db446 87 }
davidmateos 5:0e6b049208d8 88 // if (distancia<4){
davidmateos 5:0e6b049208d8 89 //estado=cerrandose;
davidmateos 5:0e6b049208d8 90
davidmateos 5:0e6b049208d8 91 // }
javiervicente 0:8a1a447db446 92 }
javiervicente 0:8a1a447db446 93
javiervicente 0:8a1a447db446 94 void estadoCerrandose(){
davidmateos 4:aa8a5a606384 95 //pc.printf("Distance =%d\n",distancia);
daniferca 2:828e6ac4800a 96 if((finalIzq==1)) {
daniferca 1:1e68e882342d 97
javiervicente 0:8a1a447db446 98 estado=cerrada;
javiervicente 0:8a1a447db446 99 enable=0;
daniferca 1:1e68e882342d 100 } else {
daniferca 2:828e6ac4800a 101
daniferca 2:828e6ac4800a 102 paso(0);
daniferca 2:828e6ac4800a 103 //pc.printf("paso\n");
daniferca 1:1e68e882342d 104
javiervicente 0:8a1a447db446 105 }
javiervicente 0:8a1a447db446 106 }
javiervicente 0:8a1a447db446 107
javiervicente 0:8a1a447db446 108 void estadoAbierta()
javiervicente 0:8a1a447db446 109 {
davidmateos 4:aa8a5a606384 110 //pc.printf("Distance =%d\n",distancia);
davidmateos 5:0e6b049208d8 111 //if (finalDer==1) {
daniferca 1:1e68e882342d 112
daniferca 2:828e6ac4800a 113
javiervicente 0:8a1a447db446 114 enable=1;
javiervicente 0:8a1a447db446 115 estado=cerrandose;
daniferca 2:828e6ac4800a 116 pc.printf("Final1 pulsado\r\n");
daniferca 1:1e68e882342d 117
davidmateos 5:0e6b049208d8 118 //}
davidmateos 5:0e6b049208d8 119 contador++;
davidmateos 9:d651ed116942 120 gOled.clearDisplay();
davidmateos 9:d651ed116942 121 gOled.printf("piezas transportadas %d\n",contador);
davidmateos 9:d651ed116942 122 gOled.display();
davidmateos 9:d651ed116942 123 gOled.setTextCursor(0,0);
javiervicente 0:8a1a447db446 124 }
javiervicente 0:8a1a447db446 125
javiervicente 0:8a1a447db446 126 int main()
javiervicente 0:8a1a447db446 127 {
javiervicente 0:8a1a447db446 128 pc.baud(115200);
javiervicente 0:8a1a447db446 129 tickerMideDistancia.attach(&mideDistancia, 0.5);
javiervicente 0:8a1a447db446 130 estado=cerrada;
davidmateos 9:d651ed116942 131 gOled.begin();
davidmateos 9:d651ed116942 132 gOled.clearDisplay();
davidmateos 9:d651ed116942 133 gOled.printf("Hola\n");
davidmateos 4:aa8a5a606384 134 //pc.printf("Estado cerrada\n");
davidmateos 8:a071c579f9ea 135
javiervicente 0:8a1a447db446 136 while(1) {
javiervicente 0:8a1a447db446 137 distancia=usensor.get_dist_cm();
davidmateos 9:d651ed116942 138
javiervicente 0:8a1a447db446 139 switch ( estado ) {
javiervicente 0:8a1a447db446 140 case cerrada:
javiervicente 0:8a1a447db446 141 estadoCerrada();
javiervicente 0:8a1a447db446 142 break;
javiervicente 0:8a1a447db446 143 case abriendose:
javiervicente 0:8a1a447db446 144 estadoAbriendose();
javiervicente 0:8a1a447db446 145 break;
javiervicente 0:8a1a447db446 146 case abierta:
javiervicente 0:8a1a447db446 147 estadoAbierta();
javiervicente 0:8a1a447db446 148 break;
javiervicente 0:8a1a447db446 149 case cerrandose:
javiervicente 0:8a1a447db446 150 estadoCerrandose();
javiervicente 0:8a1a447db446 151 break;
javiervicente 0:8a1a447db446 152 default:
javiervicente 0:8a1a447db446 153 break;
javiervicente 0:8a1a447db446 154 }
javiervicente 0:8a1a447db446 155 }
javiervicente 0:8a1a447db446 156 }