electro

Dependencies:   mbed QEI mbed-os KeypadLib TextLCD

electrocoagulador_final.cpp

Committer:
evanegasa
Date:
2019-03-04
Revision:
6:0d2b3f985e41
Parent:
5:9869290cb13f
Child:
7:52b459308d55

File content as of revision 6:0d2b3f985e41:

#include "mbed.h"
#include "keypad.h"
#include "TextLCD.h"
#include "stdlib.h"

/******************* IO ********************/
DigitalOut E_1(D8,0);         // Control de electrovalvula 1
DigitalOut E_2(D9,0);         // Control de electrovalvula 2
DigitalOut E_3(D10,0);         // Control de electrovalvula 3
DigitalOut E_4(D12,0);         // Control de electrovalvula 4
PwmOut     motor(PA_5);         // Variable temporal para el motor (se cambiará después)

/******************* INTERRUPCIONES ********************/
Timeout t;
InterruptIn button(USER_BUTTON);    // Cancelar operación

/*******************VARIABLES********************/
int tempUser1 = 1;                  // Tiempo tope de electrocoagulacion
int tempUser2 = 1;                  // Tiempo tope de reposo de la vinaza
int tempUser3 = 2;                  // Tiempo tope de vaciado del agua en el tanque 2
int tempUser4 = 1;                  // Tiempo tope de vaciado de residuos en el tanque 2
int pwm = 50;                       // Duty cycle del motor
int tiempoLlenado = 1;              // Tiempo de llenado del tanque 1

int a = 0;
int b = 0;
int c = 0;
int running = 0;
int finished = 0;
int canceled = 0;
int campo = 1;

/******************** TECLADO Y LCD ***********************/
Serial pc(USBTX, USBRX); //rs, e,d4,d5,d6,d7
TextLCD lcd(PC_8,PC_9,PC_10,PC_11,PC_12,PD_2);
Keypad keypad(D3, D2, D1, D0, D7, D6, D5, D4);

/********************* FUNCIONES **************************/

void printLCD(){
    while(1){
        lcd.cls();
        lcd.locate(0,0);
            lcd.printf("t1=%d",tempUser1);
        lcd.locate(6,0);
            lcd.printf("t2=%d",tempUser2);
        lcd.locate(0,1);
            lcd.printf("t3=%d",tempUser3);
        lcd.locate(6,1);
            lcd.printf("t4=%d",tempUser4);
        lcd.locate(12,0);
            lcd.printf("PWM");
        lcd.locate(12,1);
            lcd.printf("%d",pwm);
        wait_ms(350);
        switch(campo){
            case  1:
                lcd.locate(0,0);
                lcd.printf("t1=   ",tempUser1); break;
            case  2:
                lcd.locate(6,0);
                lcd.printf("t2=   ",tempUser2); break;
            case  3:
                lcd.locate(0,1);
                lcd.printf("t3=   ",tempUser3); break;
            case  4:
                lcd.locate(6,1);
                lcd.printf("t4=   ",tempUser4); break;       
            case  5:
                lcd.locate(12,1);
                lcd.printf("   ");
            default: ; break;
        }
        wait_ms(350);    
    }   
}

int getNum(){
    while(1){
        char key = keypad.getKey();
        switch(key) {
            case '0': return 0;
            case '1': return 1;
            case '2': return 2;
            case '3': return 3;
            case '4': return 4;
            case '5': return 5;
            case '6': return 6;
            case '7': return 7;
            case '8': return 8;
            case '9': return 9;
            case 'A': 
                campo++;
                if(campo==6) campo=1; return -1;
            //case 'B': return -2;
            //case 'C': return -3;
            case 'D': return -4;
            default: ; break;
        }
        wait_ms(100);
    }
}            

/*********************** MAIN ***************************/

void init(){ 
    E_1 = 0;
    E_2 = 0;
    E_3 = 0;
    E_4 = 0;
    keypad.enablePullUp();
    wait(1);
    lcd.locate(0,0);
    lcd.printf("  Control para\n");
    lcd.locate(0,1);
    lcd.printf("electrocoagular\n");
    wait(1.5);
    motor.period(4);
    return;
}

void setUserTimes(){
    while(1) {                   
        setvalue:
        a = 100*getNum();
            wait_ms(150);
            if(a==-1)goto setvalue;
            if(a==-4)break;
        b = 10*getNum();
            wait_ms(150);
            if(b==-1)goto setvalue;
            if(b==-4)break;
        c = getNum(); 
            wait_ms(150);
            if(c==-1)goto setvalue;
            if(c==-4)break;
        
        switch(campo){
        case 1:
            tempUser1=a+b+c; break;
        case 2:
            tempUser2=a+b+c; break;
        case 3:
            tempUser3=a+b+c; break;
        case 4:
            tempUser4=a+b+c; break;
        case 5:
            pwm=a+b+c;         break;
        default: ;             break;
        }
        wait_ms(150);
    }
    return;  
}

void vaciadoSolido(){
    E_4 = 0;
    finished = 1; 
}

void vaciadoLiquido(){
    E_3 = 0;
    E_4 = 1;
    t.attach(&vaciadoSolido, tempUser4);
}

void reposo(){
    E_3 = 1;
    t.attach(&vaciadoLiquido, tempUser3);
}

void vaciado1(){
    E_2 = 0;     
    t.attach(&reposo, tempUser2);
}

void electro(){
    motor.write(0.0f);
    E_2 = 1;
    t.attach(&vaciado1, tiempoLlenado);
}

void tiempoValvula1(){
    E_1 = 0;
    motor.write((float) pwm/100);     
    t.attach(&electro, tempUser1);
}

void cancel_op(){   
    canceled = 1;
    t.detach();
}

int main(){
    button.rise(&cancel_op);  
    while(1){
        if(running==0){
            running = 1;
            init();
            Thread lcdprint;
            lcdprint.start(printLCD);
            
            campo = 1;
            setUserTimes();
            
            campo = 0;
            finished = 0;
            canceled = 0;
            E_1 = 1;
    
            t.attach(&tiempoValvula1, tiempoLlenado); 
        }
        if(finished == 1){
            lcd.cls();
            lcd.locate(0,0);
            lcd.printf("Finalizado\n");
            running = 0;
            wait(2);
        }
        if(canceled == 1){
            lcd.cls();
            lcd.locate(0,0);
            lcd.printf("Operacion\n");
            lcd.locate(0,1);
            lcd.printf("cancelada\n");
            running = 0;
            wait(2);
        }
    }
}