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
- Committer:
- aitor01
- Date:
- 2021-12-16
- Revision:
- 3:3fbadf6c492f
- Parent:
- 2:fd3b989cedcd
- Child:
- 4:7f4d81ed3984
File content as of revision 3:3fbadf6c492f:
#include "mbed.h" #include "math.h" #include "Adafruit_SSD1306.h" //#include "hcsr04.h" class I2CPreInit : public I2C { public: I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) { frequency(100000); start(); }; }; I2CPreInit gI2C(PB_9,PB_8); // an SPI sub-class that provides a constructed default Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128); Serial pc(USBTX, USBRX); // tx, rx Ticker tickerMideDistancia; Ticker visualTemp; unsigned distancia=1000; AnalogIn senal(A0); DigitalIn final1(D9); DigitalIn final2(D10); DigitalOut step(D2); DigitalOut dir(A4); DigitalOut enableMotor(D11); DigitalIn boton_inicial(D3); DigitalIn boton_emergencia(D7); DigitalOut dirAMotor(D4);//M121 PwmOut dirBMotor(D5);//M121 DigitalOut rele(D6);//Rcal InterruptIn encoderA(A2); Timer t; Timer bot; //Ticker tickerVel; int velocidad=0; float temperatura; int contador=0; //int target=75; Para el control de velocidad void encoderAIrq()// contador de vueltas { contador++; } enum estados {reset, apagado, motorpalante,atras, estatico, emergencia }; estados estado; //HCSR04 usensor(D7,D8); //(PinName TrigPin,PinName EchoPin): void paso(int d) { dir=d; step=1; wait_us(100); step=0; wait_us(900); } void estadoreset() { if (final1!=1) { enableMotor=1; paso(0); } else if (final1 ==1) { gOled.clearDisplay(); gOled.printf("La temperatura es de %.4f\n",temperatura); gOled.printf("La velocidad en rpm es de %d\r\n",velocidad); gOled.display(); gOled.setTextCursor(0,0); estado = apagado; enableMotor =0; } } void estadoapagado() { dirBMotor =0; if (boton_inicial==1) { enableMotor=1; t.reset(); estado= motorpalante; } } void estadomotorpalante() { if (boton_emergencia==1) { estado=emergencia; } if (boton_inicial==0&&final2==0) { paso(1); dirBMotor=1; rele =1; } else if (final2==1) { int tiempo=t.read(); //pc.printf("La temperatura es de %.4f\n",temperatura); gOled.clearDisplay(); gOled.printf("La temperatura es de %.4f\n",temperatura); gOled.printf("La velocidad en rpm es de %d\r\n",velocidad); gOled.display(); gOled.setTextCursor(0,0); // pc.printf("El tiempo transcurrido es de %d\n",tiempo); //pc.printf("La temperatura es de %.4f\n",temperatura); //dirAMotor=0; wait(2.0); //enableMotor=0; //rele=0; //if (final2==1) { estado=atras; //} //Print en pantalla con el Oled. } else if (final2!=1 && boton_inicial==1 && final1!=1) { enableMotor=0; rele=0; dirBMotor=1;//Esto hay que cambiarlo estado= estatico; bot.reset(); } } void estadoatras() { if (boton_emergencia==1) { estado=emergencia; } if (final2!=1 && boton_inicial==1 && final1!=1) { enableMotor=0; rele=0; dirBMotor=1;//Esto hay que cambiarlo bot.reset(); estado= estatico; } else if (final1 ==1) { estado = motorpalante; } else { paso(0); } } void estadoestatico() { if (boton_emergencia==1) { estado=emergencia; } if (boton_inicial ==0 ) { wait(0.5); //estado = apagado; } else if (boton_inicial==1 && bot>1) { dirBMotor=0; estado = reset; } } void muestraTemp() { pc.printf("La temperatura es de %.4f\n",temperatura); gOled.clearDisplay(); gOled.printf("La temperatura es de %.4f\n",temperatura); gOled.printf("%d\r\n",velocidad); gOled.display(); gOled.setTextCursor(0,0); } void medir() { float tension=senal.read()*3.3; float Rt= 100e3*((3.3-tension)/tension); temperatura = (3950/(log(Rt/100e3)+(3950/298)))-273.1; } void funcionVel() { velocidad=contador*60;//esto es en rpm haciendo *60seg para pasar a min. contador=0; /*if(target-velocidad>0){ dirBMotor=dirBMotor+0.01; } else{ dirBMotor=dirBMotor-0.01; }*/ } void estadoemergencia() { estado = apagado; } int main() { pc.baud(115200); pc.printf("La velocidad es de %d\r rpm\n",velocidad); gOled.begin(); gOled.clearDisplay(); gOled.printf("Hola, bienvenido\n"); gOled.printf("La velocidad en rpm es de %d\r\n",velocidad); gOled.display(); gOled.setTextCursor(0,0); //visualTemp.attach(&muestraTemp,1.0); //tickerMideDistancia.attach(&mideDistancia, 0.5); estado=reset; step=1; enableMotor =1; pc.printf("Estado cerrada\n"); t.start(); bot.start(); encoderA.rise(&encoderAIrq); // attach the address of the flip function to the rising edge // tickerVel.attach(&funcionVel,0.1); dirAMotor=0; //dirBMotor=0.5; while(1) { medir(); //distancia=usensor.get_dist_cm(); switch ( estado ) { case reset: estadoreset(); pc.printf("Estado reset\n"); break; case apagado: estadoapagado(); pc.printf("Estado apagado\n"); break; case motorpalante: estadomotorpalante(); pc.printf("Estado alante\n"); break; case atras: estadoatras(); pc.printf("Estado atras\n"); break; case estatico: pc.printf("Estado estatico\n"); estadoestatico(); break; case emergencia: pc.printf("Estado emergencia\n"); estadoemergencia(); break; } } }