AKUL check if this has the correct right and left turning, if not replace it Code with lots of comments

Dependencies:   m3pi_ng mbed

Fork of Working_on_Left_and_Right by der Roboter

Revision:
3:bac13ce5f5d0
Parent:
2:b5031bb5303e
Child:
4:057e904b1395
--- a/main.cpp	Mon May 26 07:40:29 2014 +0000
+++ b/main.cpp	Mon May 26 09:34:16 2014 +0000
@@ -9,7 +9,7 @@
 
 using namespace std;
 
-void cross_detection(int sensor[5], int black_thresh, int white_thresh); 
+bool cross_detection(int sensor[5], int black_thresh, int white_thresh); 
  
 m3pi thinggy; 
  
@@ -23,6 +23,7 @@
     int returned;   
     int black_thresh = 300;
     int white_thresh = 240;  
+    bool cross = 0; 
     thinggy.locate(0,1);
     thinggy.printf("Villan");
     thinggy.locate(0,0);
@@ -38,23 +39,27 @@
     returned = (sensor[1] + sensor[2] + sensor[3])/3;
  
     while(1) {
-        
-        cross_detection(sensor, black_thresh, white_thresh); 
-        
         //check if it needs to turn  
         while(returned <= 240){
-                cross_detection(sensor, black_thresh, white_thresh);
                 //turns right 
                 while(sensor[0] < sensor[4] && thinggy.line_position() != 0){
                     thinggy.left_motor(turn_speed);
                     thinggy.right_motor(-turn_speed);
                     thinggy.calibrated_sensor(sensor);
+                    cross = cross_detection(sensor, black_thresh, white_thresh);
+                    if(cross){
+                        //ask for command (left, right or forward)
+                    }
                 }
                 //turns left 
                 while(sensor[4] > sensor[0] && thinggy.line_position() != 0){
                     thinggy.left_motor(-turn_speed);
                     thinggy.right_motor(turn_speed);
                     thinggy.calibrated_sensor(sensor);
+                    cross = cross_detection(sensor, black_thresh, white_thresh);
+                    if(cross){
+                        //ask for command (left, right or forward)
+                    }
                 }
                 thinggy.calibrated_sensor(sensor);
                 returned = (sensor[1] + sensor[2]*2 + sensor[3])/4;
@@ -104,26 +109,33 @@
  
  //REQUIRES: array of 5 ints 
  //EFFECTS: stops the robot if it comes to any interesection where a decision has to be made
- void cross_detection(int sensor[5], int black_thresh, int white_thresh){
+ // and returns true if there is a cross
+ bool cross_detection(int sensor[5], int black_thresh, int white_thresh){
        //three directions to choose from NOT WORKING 
-        if(sensor[0] > black_thresh and sensor[1] < white_thresh and sensor[2] > black_thresh and sensor[3] < white_thresh and sensor[4] > black_thresh){
+        if(sensor[0] > black_thresh and sensor[2] > black_thresh and sensor[4] > black_thresh){
             thinggy.stop();
-            wait(300);  
+            wait(300); 
+            return 1;  
         }
         //left or forward 
-        else if(sensor[0] > black_thresh and sensor[1] < white_thresh and sensor[2] > black_thresh and sensor[3] < white_thresh and sensor[4] < white_thresh){
+        else if(sensor[0] > black_thresh and sensor[2] > black_thresh){
             thinggy.stop(); 
             wait(300); 
+            return 1; 
         }
         //left or right WORKING
         else if (sensor[0] > black_thresh and sensor[1] < white_thresh and sensor[2] < white_thresh and sensor[3] < white_thresh and sensor[4] > black_thresh ){
             thinggy.stop();
-            wait(300);  
+            wait(300); 
+            return 1;  
         } 
         //forward or right
-        else if (sensor[0] < white_thresh and sensor[1] < white_thresh and sensor[2] > black_thresh and sensor[3] < white_thresh and sensor[4] > black_thresh){
+        else if (sensor[2] > black_thresh and sensor[4] > black_thresh){
             thinggy.stop(); 
-            wait(300); 
+            wait(300);
+            return 1; 
         } 
+        
+        return 0; 
 }
  
\ No newline at end of file