ENCO

Dependencies:   mbed

main.cpp

Committer:
jota_ud
Date:
2020-12-07
Revision:
1:214e5fb18573
Parent:
0:fb45c2082cb8

File content as of revision 1:214e5fb18573:

#include "mbed.h"
#include "rtos.h"



InterruptIn botoncito(PTC12);
InterruptIn botoncito2(PTC3);
InterruptIn botoncito3(D2);
//DigitalOut myled(LED_RED);


PwmOut pwm1(PTD4);
PwmOut pwm2(PTA13);
DigitalOut myled(D9);

int contador=0,a=1,c=0,r,h,q,nivel=0;
bool s=0,k=0,DC=0;

Serial pc(USBTX,USBRX);

Semaphore one_slot(1);
Mutex mutex1;


Thread thread;
Thread thread1;
Thread thread2;
Thread thread3;


void herra(){
    
    one_slot.release();
    one_slot.wait();
    r=r+1;
    
    
    one_slot.release();
}

void boton1()
{
    one_slot.release();
    one_slot.wait();
    //one_slot.try_acquire();
    s=!s;
    one_slot.release();
}
void boton2()
{
    one_slot.release();
    one_slot.wait();
    NVIC_SystemReset ();
    one_slot.release();
}



void motor1()
{
    while(1) {
        if(s==1) {
            c++;
            switch(c) {
                case 1:
                    pwm2=0.3;
                    q=30;
                    nivel=1;
                    Thread::wait(2000);
                    break;
                case 2:
                    pwm2=0.5;
                    q=50;
                    nivel=1;
                    Thread::wait(2000);
                    break;
                case 3:
                    pwm2=0.7;
                    q=70;
                    nivel=1;
                    Thread::wait(2000);
                    break;
                case 4:
                    pwm2=1;
                    q=100;
                    nivel=2;
                    Thread::wait(6000);
                    break;
                case 5:
                    pwm2=0.7;
                    q=70;
                    nivel=3;
                    Thread::wait(2000);
                    break;
                case 6:
                    pwm2=0.5;
                    q=50;
                    nivel=3;
                    Thread::wait(2000);
                    break;
                case 7:
                    q=30;
                    pwm2=0.3;
                    nivel=3;
                    Thread::wait(2000);
                    c=0;
                    break;

            }
            
        }else{
            pwm2=0.0;
            Thread::wait(1000); 
            }
    }
}




void s7()
{
    while(1) {
        if(s==1 && r==0) {
            k=1;
            r=0;
            DC=1;
            myled=!myled;
            Thread::wait(1000);

        } else if(s==1 && r>0){
            k=0;
            h=r*60;
            h=h/4;
            //pc.printf("%d",h);
            r=0;
            DC=1;
            myled=0;
            Thread::wait(1000);
            
        } else if(s==0){
            k=1;
            DC=0;
            myled=0;
            }
        
    }
}

void serial()
{
    while(1) {
        
        if(k==1) {
            if(DC==0){
                pc.printf("Estado - Detenido - nivel:%d\n",q);
                }else{
                        if(nivel==1){
                        pc.printf("Estado - acelerando\n\r   Nivel:%d - rpm:Error - Revise el sistema\n",q,h);
                    
                        }else if(nivel==2){
                            pc.printf("Estado - establecimiento\n\r   Nivel:%d - rpm:Error - Revise el sistema\n",q,h);
                        
                            }else if(nivel==3){
                                pc.printf("Estado - frenado\n\r   Nivel:%d - rpm:Error - Revise el sistema\n",q,h);
                            }
                   
                }
            

        } else if(k==0){
                    if(nivel==1){
                    pc.printf("Estado - acelerando\n\r   Nivel:%d - rpm:%d - ok\n",q,h);
                    
                    }else if(nivel==2){
                        pc.printf("Estado - establecimiento\n\r   Nivel:%d - rpm:%d - ok\n",q,h);
                        
                        }else if(nivel==3){
                            pc.printf("Estado - frenado\n\r   Nivel:%d - rpm:%d - ok\n",q,h);
                            
                            }
                    
        
        }
        
        
        
        
        pc.printf("\n\r");
        
        Thread::wait(200);

    }
}
int main()
{   
    botoncito.mode(PullUp);
    botoncito.fall(&boton1);
    
    botoncito2.mode(PullUp);
    botoncito2.fall(&boton2);
    
    botoncito3.mode(PullUp);
    botoncito3.fall(&herra);
    
    
    thread1.start(s7);
    thread2.start(serial);
    thread3.start((motor1));
    thread3.join();
}