juan carlos cortes parra / Mbed 2 deprecated Lab4rampadesaceleracion

Dependencies:   mbed TextLCD

Revision:
0:888f499e0ce0
Child:
1:fccdc54f1314
diff -r 000000000000 -r 888f499e0ce0 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Dec 04 04:28:34 2020 +0000
@@ -0,0 +1,226 @@
+#include "mbed.h"
+#include "string"
+#include "mbed.h"
+#include "sstream"
+#include <string>
+#include "TextLCD.h"
+
+TextLCD lcd(PTE20,PTE21,PTE22,PTE23,PTE29,PTE30, TextLCD::LCD16x2);// Rs, E, d4, d5, d6, d7, RW=GND
+Serial pc(USBTX,USBRX);
+DigitalIn pulso(PTC5);
+DigitalOut right(PTC7);
+DigitalOut left(PTC0);
+DigitalIn start(PTC3),finish(PTC7);
+Timer Timer1;
+int rea,ns;
+string cadena;
+using namespace std;
+void velocidad(int a,float ad);
+int dist(char datos[5]);
+PwmOut l1(PTB3),l2(PTC2);
+int selec;
+float cambio;
+float rpm=0,auxrpm=0;
+int contador=0;
+int distancia=0,disa=0;
+int second=0;
+bool antirebote=false;
+int recorrido=0,rpm1=0;
+char cade;
+//using namespace boost;
+float h;
+void inicio(){
+    while(1){
+        if(start==0){
+            break;
+        }
+        velocidad(2,0.5);
+    }
+    }
+void rampa (int sel){
+    for(float i=1;i<=10;i++){
+     velocidad(sel,i*10);
+     wait(0.01);
+    }
+    }
+int main(){
+    velocidad(0,0);
+    inicio();
+    velocidad(0,0);
+    lcd.printf("esta en los extremos");
+    int red=0;
+    char buffer[12],aux[4];
+    while(1){
+             
+         if(pc.readable()){// captura string
+         
+          pc.gets(buffer,12);
+          buffer[0]=' ';
+          cade=buffer[1];
+              aux[1]=buffer[1];
+              aux[2]=buffer[2];
+              aux[3]=buffer[3];      
+              second=dist(aux);
+              aux[1]=buffer[4];
+              aux[2]=buffer[5];
+              aux[3]=buffer[6];
+              rpm1=dist(aux);
+              aux[1]=buffer[7];
+              aux[2]=buffer[8];
+              aux[3]=buffer[9];
+              distancia=dist(aux);
+          
+        
+          if(red>0){
+              recorrido=red*6;
+              }
+          //pc.printf("%s",buffer);
+         // dist(buffer);
+         //pc.printf("%s",cadena);
+         pc.printf("%d",recorrido);
+         }
+        
+        if(cade=='d'){
+            selec=1; 
+            
+            } 
+        if (cade=='i'){ 
+            selec=2; 
+            
+            
+            } 
+        if(cade=='n'){selec=0;     
+            }
+            
+            
+            
+            
+          pc.printf("%d  ,%d  ,%d ",rpm1,distancia,second);
+               
+            
+          wait(0.01);
+         
+           // selec=1;
+          
+          
+          //velocidad(selec,cambio);
+   
+           
+       
+        }
+      
+}
+
+void velocidad(int a ,float ad){
+    switch (a){
+        case 0:
+            lcd.cls();
+            lcd.locate(0,0);     
+            lcd.printf("%d mm",distancia);
+            lcd.locate(0,1);
+            lcd.printf(" V :%.2f rpm",rpm);
+            l1=0;
+            l2=0;
+            
+            break;
+        case 1:
+            
+            if(rpm<10){
+                pc.printf("00%.1f ,%d ",rpm,distancia);
+                }
+            if((rpm>10)&&(rpm<100)){
+                pc.printf("0%.1f ,%d ",rpm,distancia);
+                } 
+            if(rpm>100){
+                pc.printf("0%.1f ,%d ",rpm,distancia);
+                }  
+           
+            lcd.cls();
+            lcd.locate(0,0);     
+            lcd.printf("%d mm",distancia);
+            lcd.locate(0,1);
+            lcd.printf(" V :%.2f rpm",rpm);
+            l1.write(ad);
+            
+            l2=0;
+            break;
+        case 2:
+              if(rpm<10){
+                pc.printf("00%.1f ,%d ",rpm,distancia);
+                }
+            if((rpm>10)&&(rpm<100)){
+                pc.printf("0%.1f ,%d ",rpm,distancia);
+                } 
+            if(rpm>100){
+                pc.printf("0%.1f ,%d ",rpm,distancia);
+                }  
+            lcd.cls();   
+            lcd.locate(0,0);     
+            lcd.printf("%d mm",distancia);
+            lcd.locate(0,1);
+            lcd.printf("%.2f rpm",rpm);
+           
+            l2.write(ad);
+            l1=0;
+            break;
+        
+  }  
+      
+}
+
+int dist(char datos[4]){
+   int numero=0,contador=0;
+   char c=' ';
+    for(int i = 3;i >= 0;i --){
+       c=datos[i];
+       //pc.putc(c);
+       if(c=='0'){contador=contador+1;  }
+       if(c=='1'){
+            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;} }
+       
+        }
+   return numero;                     
+    }
\ No newline at end of file