Mearm colorsorting with web application

Dependencies:   TCS3200

Revision:
7:3b1ce80e424c
Parent:
6:60ab0d214512
--- a/main.cpp	Thu Dec 14 13:24:01 2017 +0000
+++ b/main.cpp	Tue Jan 30 10:53:08 2018 +0000
@@ -7,45 +7,57 @@
 #include "TCPServer.h"
 #include "TCPSocket.h"
 #include "Servo.h"
-uint8_t http[1024];
+#include "TCS3200.h"
+
+TCS3200 color(PF_6, PF_7, PF_8, PF_9, PA_0);  //Create a TCS3200 object 
+ //            S0    S1    S2     S3     OUT
+
+uint8_t httpr1[1024]={200,200};
 uint8_t httpr[1024]={200,200};
-uint8_t httpr1[300]={2,2,2,2};
-//static void SendWebPage( float ,float ,float , float );
+int colour();
 
+Servo Servo1(PD_15);
+Servo Servo2(PD_12);
+Servo Servo3(PD_13);
+Servo Servo4(PD_14);
  
-                     
 
+int pos1 =0,pos2=0,pos4=0,pos3=0;    
+DigitalOut myled1(LED1);
+DigitalOut myled2(LED2);
+DigitalOut myled3(LED3);  
+               
 int main()
 {
-    Servo Servo1(PD_13);
- Servo Servo2(PD_12);
- Servo Servo3(PD_14);
- Servo Servo4(PD_15);
+
  
-  Servo1.Enable(1500,20000);
-  Servo2.Enable(1500,20000);
-  Servo3.Enable(1500,20000);
+  Servo1.Enable(0,20000);
+  Servo2.Enable(0,20000);
+  Servo3.Enable(0,20000);
+  Servo4.Enable(0,20000);
+  pos3=2500;
+  Servo3.SetPosition(pos3); 
+  wait_ms(10);
+  pos1=950;
+  Servo1.SetPosition(pos1); 
+  wait_ms(10);
+  pos2=600;
+  Servo2.SetPosition(pos2);
+  wait_ms(10);
+  pos4=600;
+  Servo4.SetPosition(pos4);
+  wait_ms(10); 
     
-    int pos,i=0,a=0,a1=0;
-    printf("Basic HTTP server example\n");
-    
-    
+    int pos,i=0,a=0,a1=0,x=0;
+    printf("Pierre Program\n");
     EthernetInterface eth;
     eth.connect();
-    
     printf("The target IP address is '%s'\n", eth.get_ip_address());
-    
     TCPServer srv;
-    
     TCPSocket clt_sock;
-    
     SocketAddress clt_addr;
-   
-    
     /* Open the server on ethernet stack */
     srv.open(&eth);
-    
-    
     /* Bind the HTTP port (TCP 80) to the server */
     //srv.bind(eth.get_ip_address(), 80);
     srv.bind(eth.get_ip_address(), 5001);
@@ -58,16 +70,8 @@
     while (true) {
         
         //==========================
-     for ( pos = 500; pos < 2000; pos += 25) 
-      {
-          Servo1.SetPosition(pos);  
-          httpr[i]=8;
-          wait_ms(20);
-          i++;
-      }
-
-      //i=0;
-          // SendWebPage(pos,v2,v3,v4);
+    
+          
           srv.accept(&clt_sock, &clt_addr);
           printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
           a = clt_sock.send(httpr, strlen((char *)httpr));
@@ -82,6 +86,237 @@
           }
            clt_sock.close();
            printf("accept voir %d\n",httpr1[0]);
+           
+           switch(httpr1[0])
+           {
+            case 49:
+            //===================
+          
+            for ( pos2 = 600; pos2 <= 2000; pos2 += 25) 
+            {
+            Servo2.SetPosition(pos2);  
+            wait_ms(10);
+            }
+            wait_ms(1000);
+      
+           for ( pos3 = 2500; pos3 >= 1650; pos3 -= 25) 
+           {
+           Servo3.SetPosition(pos3);  
+           wait_ms(10);
+           }
+           wait_ms(1000);
+      
+          for ( pos1 = 950; pos1 <= 1650; pos1 += 25) 
+          {
+           Servo1.SetPosition(pos1);  
+           wait_ms(10);
+          }
+      
+          wait_ms(1000);
+          for ( pos4 = 600; pos4 <= 2000; pos4 += 25) 
+          {
+          Servo4.SetPosition(pos4);  
+          wait_ms(10);
+              if(pos4==2000)
+              {
+                  pos1=950;
+                  Servo1.SetPosition(pos1); 
+                  wait_ms(1000);
+                  pos3=2500;
+                  Servo3.SetPosition(pos3);
+                  wait_ms(1000);
+                  
+              }
+          }
+      
+         for ( pos2 = 2000; pos2 <= 2600; pos2 += 25) 
+        {
+          Servo2.SetPosition(pos2);  
+          wait_ms(10);
+        }
+        wait_ms(1000);
+      
+      
+        for ( pos3 = 2500; pos3 >= 1650; pos3 -= 25) 
+        {
+          Servo3.SetPosition(pos3);  
+          wait_ms(10);
+        }
+        wait_ms(1000);
+      
+        for ( pos1 = 950; pos1 <= 1650; pos1 += 25) 
+        {
+          Servo1.SetPosition(pos1);  
+          wait_ms(10);
+        }
+       
+        // ==sensor
+        if(colour()== 1)
+       {
+          printf("RED\n"); 
+               myled1 = 0; 
+               myled2 = 0;
+               myled3 = 1;
+               pos2=600;
+               strcpy((char *)httpr, (char *)"R");
+                
+       }
+       else
+       {
+           if(colour() == 2)
+           {
+              printf("GREEN\n"); 
+               myled1 = 1; 
+               myled2 = 0;
+               myled3 = 0;
+               pos2=900;
+               strcpy((char *)httpr, (char *)"G");
+           }
+         if(colour() == 3)
+           {
+              printf("BLUE\n");
+               myled1 = 0; 
+               myled2 = 1;
+               myled3 = 0;
+              
+             pos2=1010;
+             strcpy((char *)httpr, (char *)"B");
+                      
+               
+           }   
+       } 
+       
+        Servo2.SetPosition(pos2);
+        wait_ms(1000);  
+        //======
+      
+      
+                  pos1=950;
+                  Servo1.SetPosition(pos1); 
+                  wait_ms(1000);
+                  pos3=2500;
+                  Servo3.SetPosition(pos3);
+                  wait_ms(1000);
+                   pos2=600;
+                  Servo2.SetPosition(pos2);
+                  wait_ms(1000);
+                  
+      
+       for ( pos3 = 2500; pos3 >= 1650; pos3 -= 25) 
+      {
+          Servo3.SetPosition(pos3);  
+          wait_ms(10);
+      }  
+      wait_ms(1000);
+      
+        for ( pos1 = 950; pos1 <= 1650; pos1 += 25) 
+      {
+          Servo1.SetPosition(pos1);  
+          wait_ms(10);
+            if(pos1==2050)
+              {
+                  pos4=1400;
+                  Servo4.SetPosition(pos4);
+                  wait_ms(10);
+              }
+      }
+      pos1 =950;
+      Servo1.SetPosition(pos1);  
+      wait_ms(1000);
+      
+      pos3=2500;
+      Servo3.SetPosition(pos3);  
+      wait_ms(1000);
+           
+            //=================== 
+            break;
+            case 50: 
+            //===================
+              // move_3();
+               pos3=2500;
+                Servo3.SetPosition(pos3); 
+                 wait_ms(10);
+             pos1=950;
+            Servo1.SetPosition(pos1); 
+            wait_ms(10);
+            pos2=600;
+            Servo2.SetPosition(pos2);
+            wait_ms(10);
+            pos4=600;
+            Servo4.SetPosition(pos4);
+            wait_ms(10); 
+              strcpy((char *)httpr, (char *)"M2");
+            //===================         
+            break;
+            case 51: 
+            //=====================
+           //  move_1();
+              strcpy((char *)httpr, (char *)"M3");
+            //=====================
+            break;
+            case 52: 
+            //=========================
+             // move_4();
+             strcpy((char *)httpr, (char *)"M4");
+            //=========================
+            break;
+            }
           }
      
-}
\ No newline at end of file
+}
+int colour()
+{
+ color.SetMode(TCS3200::SCALE_100);
+ long red, green, blue, clear;
+ int r = 0,g = 0, b =0 ;
+ for(int c=0 ; c < 10; c++)
+{
+    
+    red = color.ReadRed();
+    green = color.ReadGreen();
+    blue = color.ReadBlue();
+    clear = color.ReadClear();
+        
+    if((red < blue) && (red < green))
+    {
+    r++;        // red
+    }
+    if((green < blue) && (green < red))
+    {
+             g++;//green
+    }
+    if(blue < red && blue < green)
+    {
+             b++;//blue 
+    }
+         
+}
+   
+   if((r > b) && (r > g))
+   {
+     return 1;                  //red
+   }
+   else
+   {
+  if(g > b && g > r)
+   {
+     return 2;                 //green
+   }
+   else
+   {
+            if(b > g && b > r)
+            {
+               return 3;  
+            }
+            else
+            {
+               return 4;  
+            }
+                      //blue
+            
+   }         
+
+
+   }
+
+}