Andriy Makukha / Mbed 2 deprecated football_project_wo_output

Dependencies:   mbed

Fork of football_project by MZJ

Revision:
40:dec5270e03e9
Parent:
39:b1f864b71318
Child:
42:ed38b8671e43
--- a/proto_code.cpp	Sat Jan 09 01:32:54 2016 +0000
+++ b/proto_code.cpp	Sat Jan 09 11:12:29 2016 +0000
@@ -175,6 +175,8 @@
   }    
   
   ta.post_color(0xFF0000);
+  
+  srand(millis());
 }
 #else
 static void slave_setup()
@@ -368,6 +370,7 @@
     if (last_mode != mode)
     {
         if (mode == FREEFORM)
+            srand(millis());
             writeToPhone("RR.\r\n");
             
         if (mode == PATTERN)
@@ -399,12 +402,7 @@
     #ifdef MASTER
     if(mode == FREEFORM)
     {
-      ++active_cone;//getRandomCone(); 
-      
-      if (active_cone > NUM_CONES)
-      {
-        active_cone = 1;    
-      }
+      active_cone = getRandomCone(); 
       
       writeToPhone("GN_AC: %d\r\n", active_cone);
       
@@ -943,18 +941,54 @@
 
 static uint8_t getRandomCone(void)
 {
-  static uint8_t lastCone = 0;
+  static uint8_t last_cone = 0;
   uint8_t cone;
+  int counter = 0;
   
   do
   {
-    cone = random(1, NUM_CONES + 1);
+    cone =  (rand() % NUM_CONES);
+    ++cone;
+    
+    if (cone == last_cone)
+    {
+        if (rand() % 2 == 1)
+        {
+            --cone;
+            
+            if (cone <= 0)
+            {
+                cone = NUM_CONES;    
+            }    
+        }
+        else
+        {
+            ++cone;
+            
+            if (cone > NUM_CONES)
+            {
+                cone = 1;    
+            }
+        }
+    }
   }
-  while(cone == lastCone || active_cones[cone] == false);
+  while(active_cones[cone] == false && ++counter < 10);
   
-  //DEBUG("Target cone is ");
-  //DEBUG("%d\n",cone);
-  lastCone = cone;
+  if (!active_cones[cone])
+  {
+      for (int i = 0; i < NUM_CONES; ++i)
+      {
+          if (i == last_cone || !active_cones[cone])
+          {
+              continue;    
+          }    
+          
+          cone = i;
+          break;
+      }
+  }
+  
+  last_cone = cone;
   return cone;
 }