Daniel Garcia / Mbed 2 deprecated Arana

Dependencies:   mbed

main.cpp

Committer:
Getzeir
Date:
2018-11-20
Revision:
7:a0a38b2c99a2
Parent:
6:8d7f6fe73ed1

File content as of revision 7:a0a38b2c99a2:

#include "mbed.h"
#include "main.h"

 DigitalOut s0(PB_13);
 DigitalOut s1(PB_14);
 DigitalOut s2(PB_15);
 DigitalOut s3(PB_1);
 //DigitalIn  out (PB_2);
 DigitalOut LED(LED1);

 Serial bluetooth(PA_0, PA_1);  
 Serial command(USBTX, USBRX);
        
 InterruptIn out (PB_2);
 //nterruptIn salto(PB_6);
 Timer tiempo;
 
 char value;
 unsigned int lectura (void);
 unsigned int ROJO=0;
 unsigned int VERDE=0;
 unsigned int AZUL=0;
 
        
  

int main() {
    
    init_servo();
    //init_serial();
    out.rise (&sensor);
    bluetooth.baud(9600);
    command.baud(9600); 
    wait (1);
    
    }
     

//void init_serial()
//{     
//}
     

    

            

            
void sensor()
{
        
    
    unsigned int ROJO=0;
    unsigned int VERDE=0;       
    unsigned int AZUL=0;
    
    
        s0=1;    
        s1=0;
   
        s2=0;
        s3=0;
        ROJO= lectura();
        
        s2=0;
        s3=1;
        AZUL= lectura();
        
        s2=1;
        s3=1;
        VERDE= lectura();
        
        
        if (ROJO<VERDE && VERDE>AZUL && AZUL>ROJO)
        {
            command.printf("Color detectado: ROJO \n");
            command.printf("EMOCIONADO!!!!....\n");
                        mover_ser(0x01,0x00);
                        mover_ser(0x03,0x00);
                        mover_ser(0x05,0x00);
                        mover_ser(0x07,0x00);
                        wait(0.3);
                        mover_ser(0x01,0x3c);
                        mover_ser(0x03,0x3c);
                        mover_ser(0x05,0x3c);
                        mover_ser(0x07,0x3c);
                        wait(0.3);
                        mover_ser(0x01,0x00);
                        mover_ser(0x03,0x00);
                        mover_ser(0x05,0x00);
                        mover_ser(0x07,0x00);
                        wait(0.3);
                        mover_ser(0x01,0x3c);
                        mover_ser(0x03,0x3c);
                        mover_ser(0x05,0x3c);
                        mover_ser(0x07,0x3c);
                        wait(0.3);
                     
                        
                          
        } 
        else
        {
            if (VERDE<AZUL && AZUL>ROJO && ROJO>VERDE)
            {
                command.printf("Color detectado: VERDE \n");
                command.printf("HOLA!!!!....\n");
                        mover_ser(0x03,0x00);
                        mover_ser(0x07,0x00);
                        mover_ser(0x05,0x5a);
                        wait(0.1);
                        mover_ser(0x01,0x00);
                        wait(0.4);
                        mover_ser(0x02,0x5a);
                        wait(0.4);  
                        mover_ser(0x02,0x0a);
                        wait(0.4); 
                        mover_ser(0x02,0x5a);
                        wait(0.4); 
                        mover_ser(0x02,0x0a);     
                        wait(0.4); 
                        mover_ser(0x02,0x5a);
                        wait(0.4);
                        mover_ser(0x01,0x3c); 
                        mover_ser(0x03,0x3c);
                        mover_ser(0x07,0x3c);
                        mover_ser(0x05,0x3c);
                        wait(0.1);
                        
                       
                        
                       
            } 
            else
            {
                if (AZUL<ROJO && ROJO>VERDE && VERDE>AZUL)
                {
                    command.printf("Color detectado: AZUL \n");
                    command.printf("''GIRANDO''!!!!....\n");
                        mover_ser(0x01,0x00);
                        wait(0.3);
                        mover_ser(0x03,0x00);
                        wait(0.3);
                        mover_ser(0x07,0x00);
                        wait(0.3);
                        mover_ser(0x01,0x46);
                        wait(0.3);
                        mover_ser(0x05,0x00);
                        wait(0.3);
                        mover_ser(0x03,0x46);
                        wait(0.3);
                        mover_ser(0x01,0x00);
                        wait(0.3);
                        mover_ser(0x07,0x46);
                        wait(0.3);
                        mover_ser(0x03,0x00);
                        wait(0.3);
                        mover_ser(0x05,0x46);
                        mover_ser(0x01,0x46);
                        mover_ser(0x03,0x46);
                        mover_ser(0x07,0x46);
                        wait(0.3);
                            
                } 
                else 
                {
                    //command.printf("BLUETOOTH\n");
                   // while (ROJO>250 && VERDE>250 && AZUL>250)
            //{
    if (bluetooth.readable()) 
        {
            value = bluetooth.getc();
            bluetooth.printf("Valor: %c \n", value);
            wait(0.1);
            command.printf("Valor: %c \n", value);
            if (value=='1')
            {
                command.printf("ROTAR DERECHA\n");
                        
                       
                        mover_ser(0x01,0x00);
                        wait(0.1);
                        mover_ser(0x02,0x32);
                        wait(0.1);
                        mover_ser(0x01,0x3C);
                        wait(0.1);
                        mover_ser(0x03,0x00);
                        wait(0.1);
                        mover_ser(0x04,0x32);
                        wait(0.1);
                        mover_ser(0x03,0x3C);
                        wait(0.1);
                        mover_ser(0x07,0x00);
                        wait(0.1);
                        mover_ser(0x08,0x32);
                        wait(0.1);
                        mover_ser(0x07,0x3C);
                        wait(0.1);
                        mover_ser(0x05,0x00);
                        wait(0.1);
                        mover_ser(0x06,0x32);
                        wait(0.1);
                        mover_ser(0x05,0x3C);
                        wait(0.1);
                        mover_ser(0x02,0x5a);
                        wait(0.1);
                        mover_ser(0x04,0x5a);
                        wait(0.1);
                        mover_ser(0x06,0x5a);
                        wait(0.1);
                        mover_ser(0x08,0x5a);
                        wait(0.1);
            }
            else
            {
                if (value=='2')
                {
                    LED=1;
                    wait(0.1);
                    LED=0;
                    wait(0.1);
                }
                else
                {
                    if (value=='3')
                    {
                        command.printf("ADELANTE....\n");
                        mover_ser(0x01,0x00);
                        wait(0.1);
                        mover_ser(0x02,0x32);
                        wait(0.1);
                        mover_ser(0x01,0x3c);
                        wait(0.1);
                        mover_ser(0x03,0x00);
                        wait(0.1);
                        mover_ser(0x04,0x5a);
                        wait(0.1);
                        mover_ser(0x03,0x3c);
                        wait(0.1);
                        mover_ser(0x02,0x5a);
                        wait(0.1);
                        mover_ser(0x04,0x82);
                        wait(0.1);  
                        mover_ser(0x05,0x00);
                        wait(0.1);
                        mover_ser(0x06,0x82);
                        wait(0.1);
                        mover_ser(0x05,0x3c);
                        wait(0.1);
                        mover_ser(0x07,0x00);
                        wait(0.1);
                        mover_ser(0x08,0x5a);
                        wait(0.1);
                        mover_ser(0x07,0x3c);
                        wait(0.1);
                        mover_ser(0x06,0x5a);
                        wait(0.1);
                        mover_ser(0x08,0x32);
                        wait(0.1);
                    }
                    else
                    {
            
                        if (value=='4')
                        {
                        command.printf("ATRAS....\n");    
                        mover_ser(0x01,0x00);
                        wait(0.1);
                        mover_ser(0x02,0x5a);
                        wait(0.1);
                        mover_ser(0x01,0x3c);
                        wait(0.1);
                        mover_ser(0x03,0x00);
                        wait(0.1);
                        mover_ser(0x04,0x82);
                        wait(0.1);
                        mover_ser(0x03,0x3c);
                        wait(0.1);
                        mover_ser(0x02,0x32);
                        wait(0.1);
                        mover_ser(0x04,0x5a);
                        wait(0.1);  
                        mover_ser(0x05,0x00);
                        wait(0.1);
                        mover_ser(0x06,0x5a);
                        wait(0.1);
                        mover_ser(0x05,0x3c);
                        wait(0.1);
                        mover_ser(0x07,0x00);
                        wait(0.1);
                        mover_ser(0x08,0x32);
                        wait(0.1);
                        mover_ser(0x07,0x3c);
                        wait(0.1);
                        mover_ser(0x06,0x82);
                        wait(0.1);
                        mover_ser(0x08,0x5a);
                        wait(0.1); 
                        }
        }}}}
}
}
}
} 
//}


unsigned int lectura (void)
{

unsigned int inicio=0, final=0, resultado=0;
    tiempo.start ();        
    while (out) {}
    while (!out) {}
    while (out) {}
    
    inicio= tiempo.read_us();
    while (!out) {}
    final=tiempo.read_us();
    resultado=(final-inicio);
    tiempo.reset ();
    return (resultado);
    
         
}