class project digital Group B4-B5

Dependencies:   ArduMotoShield ArduinoMotorShield mbed

Revision:
2:83f5986b5e23
Parent:
0:3aaac6f4553e
--- a/main.cpp	Wed Dec 09 04:10:00 2015 +0000
+++ b/main.cpp	Wed Dec 09 12:16:19 2015 +0000
@@ -2,13 +2,14 @@
 #include "mbed.h"
 
 ArduMotoShield Motor;
-AnalogIn EMS(A0),A(A1),current(A2),RV_set(A3),RV2(A4);
-DigitalIn mybutton(USER_BUTTON),L339(D4);
+AnalogIn current(A2),RV_set(A3),RV2(A4);
+DigitalIn mybutton(USER_BUTTON);
 
 Serial Device(D8, D2);
 Serial pc(SERIAL_TX, SERIAL_RX);
 
-void swap(float &x,float &y);
+float max(float ,float );
+float min(float ,float );
 
 int main(void){
     
@@ -20,13 +21,30 @@
     float x=0,y=0;
     char word;
     int i=0,count=0;
+    float maxAmps = 0,minAmps = 0;
+    
     
     pc.printf("Hello PROJECT_CAR.\n");
     while(1)
     {
        // printf("Valuenery = %.2f. %d\n", RV2.read(),abs(i));
         //wait(0.75);
-        Motor.stop();
+        //Motor.forward(1.0,1.0);
+        //pc.printf(" %.2f\n ",(current.read()*1000+10));
+        //wait(0.1);
+        /*float amps = (501 - (current.read()*1000))*27.03/1023;
+         maxAmps = max(maxAmps , amps);
+         minAmps = min(minAmps , amps);
+        float noise = maxAmps - minAmps;
+        pc.printf("Curent is  %.2f A.      %.2f ",amps,abs(amps));
+        pc.printf("Noise is %.2f\n",noise);
+        if (pc.readable() != -1){maxAmps = amps; minAmps =  amps;}
+        wait(0.1);*/
+        
+        
+        
+        
+        
         if(count<=0)
         {
             if(Device.readable())
@@ -60,17 +78,18 @@
                 }
                 if(word == '#')
                 { 
-                    value = current.read()*1023;
+                //Device.printf("24.3,324.5,234.4,\n");
                     Device.printf("%.2f,",0);//ความเร็ว
-                    Device.printf("%.2f,",(((value-490)*5/1023)/0.0645));//กระแส
+                    Device.printf("%.2f,",0);//กระแส
                     if(RV_set.read()>=0&&RV_set.read()<0.25)
-                        Device.printf(" แห้ง ,\n");//โหมด
+                        Device.printf("Kange,\n");//โหมด
                     else if(RV_set.read()>=0.25&&RV_set.read()<0.50)
-                        Device.printf(" ผอม ,\n");//โหมด
+                        Device.printf("Thin,\n");//โหมด
                     else if(RV_set.read()>=0.50&&RV_set.read()<0.75)
-                        Device.printf(" อวบ ,\n");//โหมด
+                        Device.printf("Long,\n");//โหมด
                     else
-                        Device.printf(" อ้วน ,\n");//โหมด
+                        Device.printf("FAT,\n");//โหมด
+                        
                 }    
             }
             wait(0.5);
@@ -89,7 +108,7 @@
                     }
                     else if(RV2.read()>=0.49&&RV2.read()<=0.51)
                     {
-                        Left = y/3.3;
+                        Left = (y/3.3)-0.072;
                         Right = y/3.3;
                         pc.printf("CentF = %.2f , %.2f , %.2f\n",Left,Right,RV2.read());   
                     }
@@ -100,29 +119,40 @@
                         pc.printf("Right = %.2f , %.2f , %.2f.\n",RV2.read(),Left,Right);
                     }
                     Motor.forward(Left,Right); 
-                        value = current.read()*1023;
+                        float amps = (501 - (current.read()*1000))*27.03/1023;
+                        maxAmps = max(maxAmps , amps);
+                        minAmps = min(minAmps , amps);
+                        float noise = maxAmps - minAmps;
+                        Device.printf("%.2f,",abs(amps));//กระแส
                         Device.printf("%.2f,",((Left+Right)/2));//ความเร็ว
-                        Device.printf("%.2f,",(((value-490)*5/1023)/0.0645));//กระแส
+                        pc.printf("Current is %.2f\n",abs(amps));
                         if(1==0)
-                            Device.printf(" แห้ง ,\n");//โหมด
+                            Device.printf("Khang ,\n");//โหมด
                         else if(i==1)
-                            Device.printf(" ผอม ,\n");//โหมด
+                            Device.printf("Thin,\n");//โหมด
                         else if(i==2)
-                            Device.printf(" อวบ ,\n");//โหมด
+                            Device.printf("Long,\n");//โหมด
                         else
-                            Device.printf(" อ้วน ,\n");//โหมด
-                    wait(0.5);
+                            Device.printf("FAT,\n");//โหมด
+                   if (pc.readable() != -1){maxAmps = amps; minAmps =  amps;}
+                    wait(0.8);
                 }
             }
         }
     }
     //Motor.stop();
 }
-
-void swap(float &x,float &y)
+float max(float a,float b)
 {
-    float temp;
-    temp = x;
-    x = y;
-    y = temp;
+    if(a>b)
+        return a;    
+    else
+        return b;
+}
+float min(float a,float b)
+{
+    if(a<b)
+        return a;    
+    else
+        return b;
 }
\ No newline at end of file