la araña de la loka esa

Dependencies:   mbed

main.cpp

Committer:
djinn77
Date:
2018-11-22
Revision:
8:23f13de8b3be
Parent:
7:a0a38b2c99a2

File content as of revision 8:23f13de8b3be:

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

 DigitalOut s0(PB_13);
 DigitalOut s1(PB_14);
 DigitalOut s2(PB_15);
 DigitalOut s3(PB_1);
 //--------------------------------------AQUI VA EL JOSTICK
 // Definiendo Botones

AnalogIn vry(A3);          //PIN eje Y DEL JOSTCIK
AnalogIn vrx(A4);          //PIN eje X DEL JOSTCIK
float meas_vx;
float meas_vy;
//--------------------------------------AQUI VA EL JOSTICK

 //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)
            //{
   
    // AQUI VA LA LECTURA DEL JOSTICK
                
    int x=1;            
    meas_vx = vrx.read() * 3300; // Convierte el valor de lectura de la entrada entre 0-3300 eje X   
    meas_vy = vry.read() * 3300; // Convierte el valor de lectura de la entrada entre 0-3300 eje X              
    if (x==1) 
        {

            wait(0.1);
            if (meas_vx > 1700)
            {
                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 (meas_vy > 1700)
                    {
                        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 (meas_vx < 1550)
                        {
                        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);
    
         
}