ecoder motor semaforo

Dependencies:   mbed ssd1306_library SLCD

main.cpp

Committer:
20172573073
Date:
2020-06-24
Revision:
0:fb45c2082cb8

File content as of revision 0:fb45c2082cb8:

#include "mbed.h"
#include "rtos.h"
#include "ssd1306.h"
#include "SLCD.h"

SLCD slcd;
SSD1306 lcd (D7,D6);
InterruptIn botoncito(PTC12);
InterruptIn botoncito2(PTC3);
InterruptIn botoncito3(D2);
PwmOut pwm1(D5);
PwmOut pwm2(D8);
DigitalOut mo(D9);
int contador=0,a=1,c=0,r,h,q;
bool s=0,k=0;
Serial pc(USBTX,USBRX);
Semaphore one_slot(1);
Mutex mutex1;


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


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;
    mo=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;
                    mo=s;
                    Thread::wait(2000);
                    break;
                case 2:
                    pwm2=0.5;
                    q=50;
                    mo=s;
                    Thread::wait(2000);
                    break;
                case 3:
                    pwm2=0.7;
                    q=70;
                    mo=s;
                    Thread::wait(2000);
                    break;
                case 4:
                    pwm2=1;
                    q=100;
                    mo=s;
                    Thread::wait(6000);
                    break;
                case 5:
                    pwm2=0.7;
                    q=70;
                    mo=s;
                    Thread::wait(2000);
                    break;
                case 6:
                    pwm2=0.5;
                    q=50;
                    mo=s;
                    Thread::wait(2000);
                    break;
                case 7:
                    q=30;
                    pwm2=0.3;
                    mo=s;
                    Thread::wait(2000);
                    c=0;
                    break;

            }
        }
    }
}

void oled()
{
    while(1) {
        

        if(k==0){
        lcd.init();
        lcd.speed (SSD1306::Fast);
        lcd.cls();
        lcd.locate (1,0);
        time_t seconds = time(NULL);
        lcd.printf("Fecha= %s", ctime(&seconds));
        lcd.locate (4,1);
        lcd.printf ("nivel: %d ",q);
        lcd.locate (5,1);
        lcd.printf ("rpm: %d ",h);
        lcd.redraw();
        
        }else if(k==1){
        lcd.init();
        lcd.speed (SSD1306::Fast);
        lcd.cls();    
        lcd.locate (2,1);
        lcd.printf ("error"); 
        lcd.locate (5,0);
        lcd.printf ("por favor revise el sistema"); 
          lcd.redraw();  
            }
        
        Thread::wait(300);
    }
}
void s7()
{
    while(1) {
        if(s==1 && r==0){
            k=1;
            
        slcd.Home();
        slcd.putc('e');
        slcd.putc('r');
        r=0;
        Thread::wait(1000);
        slcd.clear();
        
            }else{
                k=0;
        slcd.Home();
        h=r*60;
        h=h/4;
        slcd.printf("%d",h);
        r=0;
        Thread::wait(1000);
        slcd.clear();
                
                }
    }
}
void serial()
{
    while(1) {

        one_slot.release();
        one_slot.wait();

        time_t seconds = time(NULL);

        char buffer2[32];
        strftime(buffer2, 32, "%m/%d/%y", localtime(&seconds));
        pc.printf("%s", buffer2);

        char buffer1[32];
        strftime(buffer1, 32, "%H:%M:%S", localtime(&seconds));
        pc.printf(" , %s", buffer1);
        if(k==1) {
            pc.printf(" , %d ciclo, %d rpm, error",q,h);
            
        } else {
            pc.printf(" , %d ciclo, %d rpm, ok",q,h);
        }
        pc.printf("\n\r");
        
        Thread::wait(100);
        one_slot.release();

    }
}
int main()
{

    set_time(1593041944);
    botoncito.mode(PullUp);
    botoncito.fall(&boton1);
    botoncito2.mode(PullUp);
    botoncito2.fall(&boton2);
    botoncito3.mode(PullUp);
    botoncito3.fall(&herra);

    thread1.start(oled);
    thread2.start(s7);
    thread3.start(serial);
    thread4.start((motor1));
    thread4.join();
}