Programa mbed

Dependencies:   mbed TextLCD MMA8451Q

main.cpp

Committer:
poresc
Date:
2020-11-23
Revision:
0:2968705eeb36

File content as of revision 0:2968705eeb36:

#include "mbed.h"
#include "string"
#include "TextLCD.h"
Serial pc(USBTX,USBRX);

TextLCD lcd(PTE20,PTE21,PTE22,PTE23,PTE29,PTE30, TextLCD::LCD16x2);
DigitalOut negro(PTB0);
DigitalOut naranja(PTB1);
DigitalOut cafe(PTB2);
DigitalOut amarillo(PTB3);
DigitalIn pulso(PTC1);
void velocidad(int a,float ad);
double temp=0;
int numero_pasos=0;
int k=0;
string cadena;
int selec;
char sentido;
int recorrido=0;
char tiempo;
bool antirebote=false;
Timer Timer1;
float rpm=0,auxrpm=0;
int r=0;
int dist(char datos[5]);


int main(){
  char buffer[5];
  int red=0;
     while(1){
  if((pulso==0)&&(antirebote==false)){
           // contador++;
            //disa++;
            antirebote=true;
            auxrpm=Timer1.read_ms();
            Timer1.stop();
            Timer1.reset();
          //  rpm=10000/auxrpm;
           
            if(recorrido>0){
            Timer1.start();}
            }
        if((pulso==1)&&(antirebote==true)){
            antirebote=false;
            }
           
        
         if(pc.readable()){// captura string
         
          pc.gets(buffer,8);
          buffer[0]=' ';
          //pc.printf("%s",buffer);
          sentido=buffer[1];
        
          //  lcd.printf("%d",temp);
         } 
          red=dist(buffer);
          if(red!=0){
              
          red=dist(buffer);
          temp=red*0.01;
       /*   lcd.cls();
          lcd.locate(0,0);   
          lcd.printf("%d",red);
          lcd.locate(0,1);     
          lcd.printf("%.3f",temp);*/
         
    }
        

/*if(tiempo=='1'){
 temp=0.001;}*/
 if(sentido=='d'){
        
        //lcd.printf("esta en los extremos");
        selec=1;
        //pc.printf("   derecha");
for(int i=0; i<=600;i++){
    if(k>4){
        r=r+1;
        k=0;
        }
         if(r==11){
            rpm=(r*(1/temp)*60)/1000;
            lcd.cls();
            lcd.locate(0,0); 
            lcd.printf("%.3f",rpm);
            r=0;}
    k=k+1;
    switch(k){
        case 1:
        amarillo=1;
        negro=0;
        //wait(0.2);
        break;
        case 2:
        cafe=1;
        amarillo=0;
        //wait(0.2);
        break;
        case 3:
        naranja=1;
        cafe=0;
       // wait(0.2);
        break;
        case 4:
        negro=1;
        naranja=0;
       // wait(0.2);
        break;
        }
       wait(temp);
        
        
    
   } 
   
   //rpm=0;
 //  red=0; 
   }
    lcd.cls();
    lcd.locate(0,0); 
    lcd.printf("esperando");
     pc.printf("esperando ");
            
 if(sentido=='i'){
        selec=2;
        //pc.printf("   izquierda");
for(int i=0; i<=600;i++){
    if(k>4){
        k=0;
        r=r+1;
        }
        if(r==11){
            rpm=(r*(1/temp)*60)/1000;
            lcd.cls();
            lcd.locate(0,0); 
            lcd.printf("%.3f",rpm);
            pc.printf("%.1f",rpm);
            r=0;}
    k=k+1;
    switch(k){
        case 1:
        negro=1;
        amarillo=0;
        //wait(0.2);
        break;
        case 2:
        naranja=1;
        negro=0;
        //wait(0.2);
        break;
        case 3:
        cafe=1;
        naranja=0;
       // wait(0.2);
        break;
        case 4:
        amarillo=1;
        cafe=0;
       // wait(0.2);
        break;
        }
       wait(temp);
        
        
    
   } 
    
  // rpm=0;
  // red=0; 
   }
   lcd.cls();
    lcd.locate(0,0); 
    lcd.printf("esperando");
     pc.printf(" esperando");
    
        
        if(sentido=='n'){
       /* cafe=0;
        amarillo=0;
        negro=0;
        naranja=0;
        selec=0;*/
       // pc.printf("  sin sentido");
      }
       // wait(0.5);


            
}
}  

int dist(char datos[8]){
   int numero=0,contador=0;
   char c=' ';
    for(int i = 8;i >= 0;i --){
       c=datos[i];
       //pc.putc(c);
       if(c=='0'){contador=contador+1;  }
       if(c=='1'){
          //  pc.printf("uno");
            contador=contador+1;
            if(contador==1){numero=numero+1;}}
          /*  if(contador==2){numero=numero+10;}
            if(contador==3){numero=numero+100;} }*/
        if(c=='2'){
            contador=contador+1;
            if(contador==1){numero=numero+2;}}
           /* if(contador==2){numero=numero+20;}
            if(contador==3){numero=numero+200;} }*/
        if(c=='3'){
            contador=contador+1;
            if(contador==1){numero=numero+3;}}
            /*if(contador==2){numero=numero+30;}
            if(contador==3){numero=numero+300;} }*/
        if(c=='4'){
            contador=contador+1;
            if(contador==1){numero=numero+4;}}
         /*   if(contador==2){numero=numero+40;}
            if(contador==3){numero=numero+400;} }*/
        if(c=='5'){
            contador=contador+1;
            if(contador==1){numero=numero+5;}}
           /* if(contador==2){numero=numero+50;}
            if(contador==3){numero=numero+500;} }*/
        if(c=='6'){
            contador=contador+1;
            if(contador==1){numero=numero+6;}}
          /*  if(contador==2){numero=numero+60;}
            if(contador==3){numero=numero+600;} }*/
        if(c=='7'){
            contador=contador+1;
            if(contador==1){numero=numero+7;}}
            /*if(contador==2){numero=numero+70;}
            if(contador==3){numero=numero+700;} }*/
        if(c=='8'){
            contador=contador+1;
            if(contador==1){numero=numero+8;}}
        /*    if(contador==2){numero=numero+80;}
            if(contador==3){numero=numero+800;} }*/
        if(c=='9'){
            contador=contador+1;
            if(contador==1){numero=numero+9;}}
           /* if(contador==2){numero=numero+90;}
            if(contador==3){numero=numero+900;} }*/
       
        }
        
   
    
      
   // lcd.printf("%d",numero);
     
   return numero;
               
               
            
    
            
    
    }