added distance function, added turnDir function, started function that completes left turn

Dependencies:   FRDM-TFC

Fork of TFC-TEST_TXSTATE_TEAM6v2 by Anthony Lamme

Revision:
2:729063e39fb3
Parent:
1:4d7f3b5b7463
Child:
3:04f91137660a
diff -r 4d7f3b5b7463 -r 729063e39fb3 main.cpp
--- a/main.cpp	Sat Apr 04 19:41:57 2015 +0000
+++ b/main.cpp	Sat Apr 04 21:55:09 2015 +0000
@@ -29,13 +29,20 @@
     }
 }
  
+//finds light intensity values of lines.
  
+int FindBlack();
+
+// finds and sets by reference the values of the left and right bounds
+void Bounds(int &, int &, int);
  
  
 int main()
 {
     uint32_t i,t,time = 0;
     
+    int a = 200, b = 200;
+    
     int data[128];
     
     int black = 0;
@@ -126,24 +133,9 @@
                          
                          if(TFC_PUSH_BUTTON_0_PRESSED)
                     {          
-                               //Demo Mode 3 will be in Freescale Garage Mode.  It will beam data from the Camera to the 
-                //Labview Application
-                        int highest = 0;
-                        
-                         // camera 1
-                         for(i=0;i<128;i++)
-                         {
-                               if(i==0)
-                               {
-                                  highest = TFC_LineScanImage0[i];
-                                }
-                               if(TFC_LineScanImage0[i]>highest)
-                               {
-                                   highest = TFC_LineScanImage0[i];
-                               }    
-                         }
+                  
                          
-                         black = highest;
+                         black = FindBlack();
                          
                          TERMINAL_PRINTF("%i", black);
                 
@@ -185,10 +177,13 @@
                          {
                                if(TFC_LineScanImage0[i]<=black)
                                {
+                                   // zero is black
+                                   
                                    data[i]=0;
                                 }
                                 else
                                 {
+                                    // one is white
                                     data[i]=1;
                                 }
                                 TERMINAL_PRINTF("%d", data[i]);
@@ -215,4 +210,81 @@
  
 }
 }
-}
\ No newline at end of file
+}
+
+// finds and sets by reference the values of the left and right bounds
+
+void bounds(int &a,int &b,int black)
+{
+ 
+     for(int i=0; i<128; i++)
+     {
+         if(TFC_LineScanImage0[i]==black&& i==0)
+         {
+             a = i;
+         }
+     
+        else if(TFC_LineScanImage0[i]==black)
+         {
+            //if there are to black values next to eachother it sets a to newer inside value.
+            
+            if( (i-a) == 1 )
+            {
+                a = i;   
+            }    
+            
+            // if there is a space between black values the next black value is the right bound.
+            else
+            {
+              // sets the right inside bound and ends the loop.
+              
+              b = i;
+              break;   
+            }
+         }
+       } 
+}         
+ 
+    
+  
+
+int FindBlack()
+{
+    int low1=2000,low2=2000;
+    for(int i=2;i<64;i++)//first half of line scan
+    {
+        int ave=(TFC_LineScanImage0[i-2]+TFC_LineScanImage0[i-1]+TFC_LineScanImage0[i])/3;
+        if(i==2)
+        {
+            low1=ave;//first loop sets lowest average
+        }
+        if(low1>ave)
+        {
+            low1=ave;
+        }
+    }
+    for(int i=66;i<128;i++)//second half of line scan
+    {
+        int ave2=(TFC_LineScanImage0[i-2]+TFC_LineScanImage0[i-1]+TFC_LineScanImage0[i])/3;
+        if(i==66)
+        {
+            low2=ave2;
+        }
+        if(low2>ave2)
+        {
+            low2=ave2;
+        }
+        if(low1==low2)
+        {
+            return low1; //confirms bothe lines and breaks loop returns black
+        }   
+    }
+    if(low1>low2)
+    {
+        return low1;
+    }
+    else
+    {
+        return low2;
+    }
+}    
\ No newline at end of file