electro

Dependencies:   mbed mbed-os KeypadLib TextLCD

electrocoagulador_final.cpp

Committer:
afsaavedram
Date:
2019-03-02
Revision:
4:2636fe1c6527
Parent:
3:a4994da37d1f
Child:
5:9869290cb13f

File content as of revision 4:2636fe1c6527:

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

/*******************ELECTROVALVULAS********************/

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
AnalogIn sensor_level(A0);      //Sensor nivel tanque 1
PwmOut motor(PA_5);             //variable temporal para el motor (se cambiará después)
/*****************************************************/

/*******************INTERRUPCIONES********************/

InterruptIn button(USER_BUTTON);
Ticker check_level;
Ticker up;
Ticker down;

Timeout t_1;
Timeout t_2;
Timeout t_3;
Timeout t_4;

//tiempos de usuario
int temp_user_1=0;                      //tiempo tope de electrocoagulacion
int temp_user_2=0;                      //tiempo tope de reposo de la vinaza
int temp_user_3=0;                      //tiempo tope de vaciado del agua en el tanque 2
int temp_user_4=0;                      //tiempo tope de vaciado de residuos en el tanque 2


//timers
Timer timer_1;
Timer timer_2;
Timer timer_3;

/********************TECLADO Y LCD***********************/

//          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);
float num=0;
char cadena[3]= {' ',' ',' '};
int llena=0; //verificar que la entrada es de 3 numeros


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

void stop()  //STOP
{

    while(1) {
        lcd.cls();
        lcd.printf("      STOP!");
        lcd.locate(0,1);
        lcd.printf("  PLEASE RESET");
        wait_ms(150);
    }
}

void level()
{
    if(sensor_level>=3) {
        E_1=0;
    }
}

void electro_1()
{
    E_1=!E_1;
}

void electro_2()
{
    E_2=!E_2;
}

void electro_3()
{
    E_3=!E_3;
}

void electro_4()
{
    E_4=!E_4;
}

void pwm_rise()
{
    if(keypad.getKey()=='7') {
        double n=n+0.003;
        if(n>=1) {
            n=1;
        }
        motor.period(0.5);      // 4 second period
        motor.write(n);         // % of duty cycle, relative to period
        wait(150);
    }
}

void pwm_fall()
{
    if(keypad.getKey()=='8') {
        double n=n-0.003;
        if(n<0) {
            n=0;
        }
        motor.period(0.5);      // 4 second period
        motor.write(n);         // % of duty cycle, relative to period
        wait(150);
    }
}

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

int main()
{
    button.rise(&stop);
    keypad.enablePullUp();
    wait(1);
    lcd.locate(0,0);
    lcd.printf("  Control para\n");
    lcd.locate(0,1);
    lcd.printf("electrocoagular\n");
    wait(1.5);
    lcd.cls();
    down.attach(&pwm_fall,0.013);
    up.attach(&pwm_rise,0.011);


    while(1) {
ini_1:
        lcd.cls();
        lcd.locate(0,0);
        lcd.printf("t_1=%d",temp_user_1);
        lcd.locate(8,0);
        lcd.printf("t_2=%d",temp_user_2);
        lcd.locate(0,1);
        lcd.printf("t_3=%d",temp_user_3);
        lcd.locate(8,1);
        lcd.printf("t_4=%d",temp_user_4);
        wait_ms(100);

        char key = keypad.getKey();

        //if(key=='A') {
        //lcd.cls();
        //lcd.locate(0,0);
        //lcd.printf("%c",key);
        //wait(3);



        //           lcd.printf("t_1=%f",temp_user_1);


        if(key=='1') {
            temp_user_1=temp_user_1+10;
            if(temp_user_1<00) {
                temp_user_1=00;
            }
            wait_ms(150);
        }
        if(key=='2') {
            temp_user_1=temp_user_1-1;
            if(temp_user_1<00) {
                temp_user_1=00;
            }
            wait_ms(150);
        }
        if(key=='3') {
            temp_user_2=temp_user_2+10;
            if(temp_user_2<00) {
                temp_user_2=00;
            }
            wait_ms(150);
        }
        if(key=='A') {
            temp_user_2=temp_user_2-1;
            if(temp_user_2<00) {
                temp_user_2=00;
            }
            wait_ms(150);
        }
        if(key=='4') {
            temp_user_3=temp_user_3+10;
            if(temp_user_3<00) {
                temp_user_3=00;
            }
            wait_ms(150);
        }
        if(key=='5') {
            temp_user_3=temp_user_3-1;
            if(temp_user_3<00) {
                temp_user_3=00;
            }
            wait_ms(150);
        }
        if(key=='6') {
            temp_user_4=temp_user_4+10;
            if(temp_user_4<00) {
                temp_user_4=00;
            }
            wait_ms(150);
        }
        if(key=='B') {
            temp_user_4=temp_user_4-1;
            if(temp_user_4<00) {
                temp_user_4=00;
            }
            wait_ms(150);
        }

        /***********************COMIENZA CONTROL***********************/
        if(key=='D' && temp_user_1!=0) {
            E_1=1;
            while(E_1==1) {
                check_level.attach(&level, 0.5);
            }
            t_1.attach(&electro_2,temp_user_1);
            //motor=1;
            timer_1.start();
            while(timer_1.read()>=temp_user_1) {
                timer_1.stop();
                motor.write(0); 
                timer_2.start();
                while(timer_2.read()<=timer_1) {
                    E_2=1;
                }
                electro_2();
                timer_2.stop();
                timer_3.start();
                while(timer_3.read()<= temp_user_2+temp_user_3+temp_user_4) {
                    t_2.attach(&electro_3,temp_user_2);
                    t_3.attach(&electro_3,temp_user_2+temp_user_3);
                    t_4.attach(&electro_4,temp_user_4);
                }
            }






        } else if (key=='D' && temp_user_1==0) {
            lcd.cls();
            lcd.printf("   t_1 empty");
            wait(1.5);
            key='\0';
            goto ini_1;
        }


        /*
        if(key=='A'){
            while(llena<=2){
            if(llena<3){
            cadena[llena]=keypad.getKey();
            temp_user_1=strtod(cadena,NULL);
            llena++;
                switch(llena){
                    case 0:
                    if(llena==0 && keypad.getKey()!= '\0'){
                    cadena[llena]=keypad.getKey();
                    llena=llena +1;
                    break;
                    }

                    case 1:
                    if(llena==1 && keypad.getKey()!= '\0'){
                    cadena[llena]=keypad.getKey();
                    llena=llena+1;
                    break;
                    }

                    case 2:
                    if(llena==2 && keypad.getKey()!= '\0'){
                    cadena[llena]=keypad.getKey();
                    llena=llena+1;
                    break;}

                    default:
                    break;
                }*/
        //lcd.cls();
        //temp_user_1=strtod(cadena,NULL);
        //lcd.locate(0,0);
        //lcd.printf("t_1=%f",temp_user_1);





    }//termina while de main
}//termina main