Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: Adafruit_GFX
main.cpp@2:fd3b989cedcd, 2021-12-15 (annotated)
- Committer:
- agat
- Date:
- Wed Dec 15 18:57:42 2021 +0000
- Revision:
- 2:fd3b989cedcd
- Parent:
- 1:1d515eb7abf3
- Child:
- 3:3fbadf6c492f
Hasta aqui esta bien dia 15
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
agat | 0:e3755f5769fe | 1 | #include "mbed.h" |
agat | 0:e3755f5769fe | 2 | #include "math.h" |
agat | 2:fd3b989cedcd | 3 | #include "Adafruit_SSD1306.h" |
agat | 2:fd3b989cedcd | 4 | |
agat | 0:e3755f5769fe | 5 | //#include "hcsr04.h" |
agat | 2:fd3b989cedcd | 6 | class I2CPreInit : public I2C |
agat | 2:fd3b989cedcd | 7 | { |
agat | 2:fd3b989cedcd | 8 | public: |
agat | 2:fd3b989cedcd | 9 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
agat | 2:fd3b989cedcd | 10 | { |
agat | 2:fd3b989cedcd | 11 | frequency(100000); |
agat | 2:fd3b989cedcd | 12 | start(); |
agat | 2:fd3b989cedcd | 13 | }; |
agat | 2:fd3b989cedcd | 14 | }; |
agat | 2:fd3b989cedcd | 15 | |
agat | 2:fd3b989cedcd | 16 | I2CPreInit gI2C(PB_9,PB_8); |
agat | 2:fd3b989cedcd | 17 | |
agat | 2:fd3b989cedcd | 18 | // an SPI sub-class that provides a constructed default |
agat | 2:fd3b989cedcd | 19 | |
agat | 2:fd3b989cedcd | 20 | Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128); |
agat | 0:e3755f5769fe | 21 | Serial pc(USBTX, USBRX); // tx, rx |
agat | 0:e3755f5769fe | 22 | Ticker tickerMideDistancia; |
aitor01 | 1:1d515eb7abf3 | 23 | Ticker visualTemp; |
agat | 0:e3755f5769fe | 24 | unsigned distancia=1000; |
agat | 0:e3755f5769fe | 25 | |
aitor01 | 1:1d515eb7abf3 | 26 | AnalogIn senal(A0); |
agat | 0:e3755f5769fe | 27 | DigitalIn final1(D9); |
agat | 0:e3755f5769fe | 28 | DigitalIn final2(D10); |
agat | 0:e3755f5769fe | 29 | DigitalOut step(D2); |
agat | 0:e3755f5769fe | 30 | DigitalOut dir(A4); |
agat | 0:e3755f5769fe | 31 | DigitalOut enableMotor(D11); |
agat | 0:e3755f5769fe | 32 | DigitalIn boton_inicial(D3); |
aitor01 | 1:1d515eb7abf3 | 33 | DigitalOut dirAMotor(D4);//M121 |
aitor01 | 1:1d515eb7abf3 | 34 | PwmOut dirBMotor(D5);//M121 |
aitor01 | 1:1d515eb7abf3 | 35 | DigitalOut rele(D6);//Rcal |
agat | 0:e3755f5769fe | 36 | |
aitor01 | 1:1d515eb7abf3 | 37 | //InterruptIn encoderA(A2); |
aitor01 | 1:1d515eb7abf3 | 38 | |
aitor01 | 1:1d515eb7abf3 | 39 | Timer t; |
aitor01 | 1:1d515eb7abf3 | 40 | |
aitor01 | 1:1d515eb7abf3 | 41 | float temperatura; |
aitor01 | 1:1d515eb7abf3 | 42 | |
aitor01 | 1:1d515eb7abf3 | 43 | enum estados {reset, apagado, encendido, motorpalante,atras, estatico }; |
agat | 0:e3755f5769fe | 44 | estados estado; |
agat | 0:e3755f5769fe | 45 | |
agat | 0:e3755f5769fe | 46 | //HCSR04 usensor(D7,D8); //(PinName TrigPin,PinName EchoPin): |
agat | 0:e3755f5769fe | 47 | void paso(int d) |
agat | 0:e3755f5769fe | 48 | { |
agat | 0:e3755f5769fe | 49 | dir=d; |
agat | 0:e3755f5769fe | 50 | step=1; |
agat | 0:e3755f5769fe | 51 | wait_us(100); |
agat | 0:e3755f5769fe | 52 | step=0; |
agat | 0:e3755f5769fe | 53 | wait_us(900); |
agat | 0:e3755f5769fe | 54 | } |
agat | 0:e3755f5769fe | 55 | void estadoreset() |
agat | 0:e3755f5769fe | 56 | { |
agat | 0:e3755f5769fe | 57 | if (final1!=1) { |
aitor01 | 1:1d515eb7abf3 | 58 | enableMotor=1; |
agat | 0:e3755f5769fe | 59 | paso(0); |
agat | 0:e3755f5769fe | 60 | } else if (final1 ==1) { |
agat | 0:e3755f5769fe | 61 | estado = apagado; |
agat | 0:e3755f5769fe | 62 | enableMotor =0; |
agat | 0:e3755f5769fe | 63 | } |
agat | 0:e3755f5769fe | 64 | } |
agat | 0:e3755f5769fe | 65 | |
agat | 0:e3755f5769fe | 66 | void estadoapagado() |
agat | 0:e3755f5769fe | 67 | { |
agat | 0:e3755f5769fe | 68 | if (boton_inicial==1) { |
agat | 0:e3755f5769fe | 69 | enableMotor=1; |
aitor01 | 1:1d515eb7abf3 | 70 | estado= motorpalante; |
aitor01 | 1:1d515eb7abf3 | 71 | t.reset(); |
agat | 0:e3755f5769fe | 72 | } |
agat | 0:e3755f5769fe | 73 | } |
agat | 0:e3755f5769fe | 74 | |
aitor01 | 1:1d515eb7abf3 | 75 | /*void estadoencendido() |
agat | 0:e3755f5769fe | 76 | { |
aitor01 | 1:1d515eb7abf3 | 77 | if (boton_inicial==0) { |
agat | 0:e3755f5769fe | 78 | paso(1); |
agat | 0:e3755f5769fe | 79 | estado = motorpalante; |
agat | 0:e3755f5769fe | 80 | |
agat | 0:e3755f5769fe | 81 | } |
aitor01 | 1:1d515eb7abf3 | 82 | }*/ |
aitor01 | 1:1d515eb7abf3 | 83 | |
aitor01 | 1:1d515eb7abf3 | 84 | void estadomotorpalante() |
aitor01 | 1:1d515eb7abf3 | 85 | { |
aitor01 | 1:1d515eb7abf3 | 86 | //if (final2) |
aitor01 | 1:1d515eb7abf3 | 87 | if (boton_inicial==0&&final2==0) { |
agat | 2:fd3b989cedcd | 88 | |
aitor01 | 1:1d515eb7abf3 | 89 | paso(1); |
agat | 2:fd3b989cedcd | 90 | |
aitor01 | 1:1d515eb7abf3 | 91 | dirAMotor=1; |
aitor01 | 1:1d515eb7abf3 | 92 | rele =1; |
aitor01 | 1:1d515eb7abf3 | 93 | |
aitor01 | 1:1d515eb7abf3 | 94 | } else if (final2==1) { |
aitor01 | 1:1d515eb7abf3 | 95 | int tiempo=t.read(); |
agat | 2:fd3b989cedcd | 96 | |
agat | 2:fd3b989cedcd | 97 | //pc.printf("La temperatura es de %.4f\n",temperatura); |
agat | 2:fd3b989cedcd | 98 | gOled.clearDisplay(); |
agat | 2:fd3b989cedcd | 99 | gOled.printf("La temperatura es de %.4f\n",temperatura); |
agat | 2:fd3b989cedcd | 100 | gOled.display(); |
agat | 2:fd3b989cedcd | 101 | gOled.setTextCursor(0,0); |
agat | 2:fd3b989cedcd | 102 | |
agat | 2:fd3b989cedcd | 103 | // pc.printf("El tiempo transcurrido es de %d\n",tiempo); |
agat | 2:fd3b989cedcd | 104 | //pc.printf("La temperatura es de %.4f\n",temperatura); |
aitor01 | 1:1d515eb7abf3 | 105 | //dirAMotor=0; |
aitor01 | 1:1d515eb7abf3 | 106 | wait(2.0); |
aitor01 | 1:1d515eb7abf3 | 107 | //enableMotor=0; |
aitor01 | 1:1d515eb7abf3 | 108 | //rele=0; |
aitor01 | 1:1d515eb7abf3 | 109 | |
agat | 2:fd3b989cedcd | 110 | //if (final2==1) { |
aitor01 | 1:1d515eb7abf3 | 111 | estado=atras; |
agat | 2:fd3b989cedcd | 112 | //} |
aitor01 | 1:1d515eb7abf3 | 113 | //Print en pantalla con el Oled. |
aitor01 | 1:1d515eb7abf3 | 114 | } |
agat | 0:e3755f5769fe | 115 | } |
agat | 0:e3755f5769fe | 116 | |
aitor01 | 1:1d515eb7abf3 | 117 | void estadoatras() |
aitor01 | 1:1d515eb7abf3 | 118 | { |
aitor01 | 1:1d515eb7abf3 | 119 | if (final2!=1 && boton_inicial==1 && final1!=1) { |
aitor01 | 1:1d515eb7abf3 | 120 | enableMotor=0; |
aitor01 | 1:1d515eb7abf3 | 121 | rele=0; |
aitor01 | 1:1d515eb7abf3 | 122 | dirAMotor=0; |
aitor01 | 1:1d515eb7abf3 | 123 | estado= estatico; |
aitor01 | 1:1d515eb7abf3 | 124 | } else if (final1 ==1) { |
aitor01 | 1:1d515eb7abf3 | 125 | estado = motorpalante; |
aitor01 | 1:1d515eb7abf3 | 126 | } else { |
aitor01 | 1:1d515eb7abf3 | 127 | paso(0); |
aitor01 | 1:1d515eb7abf3 | 128 | } |
aitor01 | 1:1d515eb7abf3 | 129 | } |
agat | 0:e3755f5769fe | 130 | |
agat | 2:fd3b989cedcd | 131 | void muestraTemp() |
agat | 2:fd3b989cedcd | 132 | { |
aitor01 | 1:1d515eb7abf3 | 133 | pc.printf("La temperatura es de %.4f\n",temperatura); |
agat | 2:fd3b989cedcd | 134 | gOled.clearDisplay(); |
agat | 2:fd3b989cedcd | 135 | gOled.printf("La temperatura es de %.4f\n",temperatura); |
agat | 2:fd3b989cedcd | 136 | gOled.display(); |
agat | 2:fd3b989cedcd | 137 | gOled.setTextCursor(0,0); |
agat | 2:fd3b989cedcd | 138 | } |
agat | 0:e3755f5769fe | 139 | |
agat | 2:fd3b989cedcd | 140 | void medir() |
agat | 2:fd3b989cedcd | 141 | { |
aitor01 | 1:1d515eb7abf3 | 142 | float tension=senal.read()*3.3; |
aitor01 | 1:1d515eb7abf3 | 143 | float Rt= 100e3*((3.3-tension)/tension); |
aitor01 | 1:1d515eb7abf3 | 144 | temperatura = (3950/(log(Rt/100e3)+(3950/298)))-273.1; |
agat | 2:fd3b989cedcd | 145 | } |
agat | 0:e3755f5769fe | 146 | int main() |
agat | 0:e3755f5769fe | 147 | { |
agat | 0:e3755f5769fe | 148 | pc.baud(115200); |
agat | 2:fd3b989cedcd | 149 | gOled.begin(); |
agat | 2:fd3b989cedcd | 150 | gOled.clearDisplay(); |
agat | 2:fd3b989cedcd | 151 | gOled.printf("Hola"); |
agat | 2:fd3b989cedcd | 152 | gOled.display(); |
agat | 2:fd3b989cedcd | 153 | gOled.setTextCursor(0,0); |
aitor01 | 1:1d515eb7abf3 | 154 | //visualTemp.attach(&muestraTemp,1.0); |
agat | 0:e3755f5769fe | 155 | //tickerMideDistancia.attach(&mideDistancia, 0.5); |
agat | 0:e3755f5769fe | 156 | estado=reset; |
agat | 0:e3755f5769fe | 157 | step=1; |
agat | 0:e3755f5769fe | 158 | enableMotor =1; |
agat | 0:e3755f5769fe | 159 | pc.printf("Estado cerrada\n"); |
aitor01 | 1:1d515eb7abf3 | 160 | t.start(); |
agat | 0:e3755f5769fe | 161 | while(1) { |
agat | 2:fd3b989cedcd | 162 | medir(); |
agat | 0:e3755f5769fe | 163 | //distancia=usensor.get_dist_cm(); |
agat | 0:e3755f5769fe | 164 | switch ( estado ) { |
agat | 2:fd3b989cedcd | 165 | |
agat | 0:e3755f5769fe | 166 | case reset: |
agat | 0:e3755f5769fe | 167 | estadoreset(); |
aitor01 | 1:1d515eb7abf3 | 168 | pc.printf("Estado reset\n"); |
agat | 0:e3755f5769fe | 169 | break; |
agat | 0:e3755f5769fe | 170 | case apagado: |
agat | 0:e3755f5769fe | 171 | estadoapagado(); |
aitor01 | 1:1d515eb7abf3 | 172 | pc.printf("Estado apagado\n"); |
agat | 0:e3755f5769fe | 173 | break; |
aitor01 | 1:1d515eb7abf3 | 174 | /* case encendido: |
aitor01 | 1:1d515eb7abf3 | 175 | pc.printf("HOLI2"); |
aitor01 | 1:1d515eb7abf3 | 176 | estadoencendido(); |
aitor01 | 1:1d515eb7abf3 | 177 | break;*/ |
aitor01 | 1:1d515eb7abf3 | 178 | case motorpalante: |
aitor01 | 1:1d515eb7abf3 | 179 | estadomotorpalante(); |
aitor01 | 1:1d515eb7abf3 | 180 | pc.printf("Estado alante\n"); |
agat | 0:e3755f5769fe | 181 | break; |
aitor01 | 1:1d515eb7abf3 | 182 | case atras: |
aitor01 | 1:1d515eb7abf3 | 183 | estadoatras(); |
aitor01 | 1:1d515eb7abf3 | 184 | pc.printf("Estado atras\n"); |
aitor01 | 1:1d515eb7abf3 | 185 | break; |
aitor01 | 1:1d515eb7abf3 | 186 | case estatico: |
aitor01 | 1:1d515eb7abf3 | 187 | pc.printf("Estado estatico\n"); |
aitor01 | 1:1d515eb7abf3 | 188 | //estadoestatico(); |
agat | 0:e3755f5769fe | 189 | break; |
agat | 0:e3755f5769fe | 190 | /*case cerrada: |
agat | 0:e3755f5769fe | 191 | estadoCerrada(); |
agat | 0:e3755f5769fe | 192 | break; |
agat | 0:e3755f5769fe | 193 | case abriendose: |
agat | 0:e3755f5769fe | 194 | estadoAbriendose(); |
agat | 0:e3755f5769fe | 195 | break; |
agat | 0:e3755f5769fe | 196 | case abierta: |
agat | 0:e3755f5769fe | 197 | estadoAbierta(); |
agat | 0:e3755f5769fe | 198 | break; |
agat | 0:e3755f5769fe | 199 | case cerrandose: |
agat | 0:e3755f5769fe | 200 | estadoCerrandose(); |
agat | 0:e3755f5769fe | 201 | break; |
agat | 0:e3755f5769fe | 202 | default: |
agat | 0:e3755f5769fe | 203 | break; |
agat | 0:e3755f5769fe | 204 | }*/ |
agat | 0:e3755f5769fe | 205 | } |
agat | 0:e3755f5769fe | 206 | } |
agat | 0:e3755f5769fe | 207 | } |