Jolyon Hill / ColourSensor

Dependencies:   MCP23S17 PCF8574 TextLCD eeprom mbed-dev

Revision:
3:b8218b61b0e2
Parent:
2:4a8eacbb3657
Child:
4:dce5fc58abe9
--- a/main.cpp	Fri Aug 12 11:01:07 2016 +0000
+++ b/main.cpp	Tue Aug 16 10:21:19 2016 +0000
@@ -10,7 +10,30 @@
 #define SENSOR_GREEN_REG 152
 #define SENSOR_BLUE_REG 154
 
-#define WHITE_THRESHOLD 20
+#define WHITE_THRESHOLD 15
+
+//--------------------------------------
+//each LED type is defined here
+#define NOTUSED    0
+#define STD_RED    1
+#define STD_GREEN  2
+#define STD_BLUE   3
+#define STD_YELLOW 4
+#define GREEN_YELLOW 5
+#define RGB        6
+
+
+//--------------------------------------
+//Current LED Colour Definitions
+#define ERROR   0       //off or wrong
+#define RED     1
+#define GREEN   2
+#define BLUE    3
+#define YELLOW  4
+
+
+
+//--------------------------------------
 
 
 
@@ -29,11 +52,6 @@
 DigitalOut RedLed(LED3);
 DigitalOut YellowLed(LED4);
 
-//DigitalOut myled(LED1);
-//DigitalOut myGreen(p21);
-//DigitalOut myYellow(p22);
-//DigitalOut myled4(LED4);
-//DigitalOut green(LED1);
 //-------------------------------------------------------------------------------
 
 //-------------------------------------------------------------------------------
@@ -92,6 +110,34 @@
 Colour SensorData;
 
 //-------------------------------------------------------------------------------
+//The system looks up the type of LED that is being detected for Each sensor and will fill in the result table & update the associated Output list
+//outputs start at 0 torough to 15
+
+char Config[32]={   RGB,    0,  //sensor 0 , uses 3 outputs  - 0,1,2
+                    NOTUSED,3,  //sensor 1
+                    NOTUSED,4,  //sensor 2
+                    NOTUSED,5,  //sensor 3
+                    NOTUSED,6,  //sensor 4                    
+                    NOTUSED,7,  //sensor 5
+                    NOTUSED,8,  //sensor 6
+                    NOTUSED,9,  //sensor 7
+                    NOTUSED,10,  //sensor 8
+                    NOTUSED,11,  //sensor 9
+                    NOTUSED,12,  //sensor 10
+                    NOTUSED,13,  //sensor 11
+                    NOTUSED,14,  //sensor 12
+                    NOTUSED,15,  //sensor 13
+                    NOTUSED,15,  //sensor 14
+                    NOTUSED,15,  //sensor 15
+                };
+
+char LEDState[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; //current detected LED states
+
+uint16_t OutputData = 0;    //word containing the current output states of the board
+ 
+uint8_t SensorNumber = 0;   //current sensor number
+char SensorResult = 0;      //detected colour
+
 
 //------------TSC Stuff----------------
 int sensor_addr = 41 << 1;
@@ -128,7 +174,7 @@
 float MyCCTi;
 uint8_t DisplayIndex = 0;
 
-uint8_t Config = 0x30;
+uint8_t SensorConfig = 0x30;
 // bit  description              setting
 //  0   SD,   0 = enabled             0
 //  1   AF,   0 = Automode            0
@@ -291,15 +337,130 @@
     RedThreshold    = RedTrimmer.read_u16()   * 100 >> 11;
     GreenThreshold  = GreenTrimmer.read_u16() * 100 >> 11;
     BlueThreshold   = BlueTrimmer.read_u16()  * 100 >> 11;
-    Hysteresis      = HysTrimmer.read_u16()   * 100 >> 11;   
+    Hysteresis      = HysTrimmer.read_u16()   * 100 >> 11;      
+    */
     
-    */
     RedThreshold    = 45;
     GreenThreshold  = 35;
     BlueThreshold   = 10;
     Hysteresis      = 5;
 }
 
+
+void UpdateDisplay(void)
+{
+    //make 12 bit only (display)
+    Red >>=4;
+    Green >>=4;
+    Blue >>=4;
+    White >>=4;
+
+    lcd.locate(0, 0);
+    lcd.printf("R%02i G%02i B%02i W%04i",RedProp,GreenProp,BlueProp,White); 
+    lcd.locate(0, 1);       
+    lcd.printf("R%02i G%02i B%02i H%02i ",RedThreshold, GreenThreshold, BlueThreshold, Hysteresis);   
+}
+
+
+char GetResults(uint8_t ThisSensor)
+{
+
+    char LEDColour = ERROR;
+    
+    
+    if(White > WHITE_THRESHOLD) //looking at intensity somthing is on!
+    {  
+        switch(Config[(ThisSensor << 1)])
+        {
+            case STD_RED:
+            {
+                if((RedProp - GreenProp  > 30) && (RedProp - BlueProp  > 30))
+                {
+                    LEDColour = RED;
+                }          
+            }
+            break;
+            
+            case STD_GREEN:
+            {
+                if((GreenProp - RedProp  > 30) && (GreenProp - BlueProp  > 30))
+                {
+                    LEDColour = GREEN;
+                }             
+            }
+            break;       
+    
+            case STD_BLUE:
+            {
+                if((BlueProp - RedProp  > 30) && (BlueProp - GreenProp   > 30))
+                {
+                    LEDColour = BLUE;
+                }             
+            }
+            break; 
+            
+            case STD_YELLOW:
+            {
+                if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) 
+                {             
+                    LEDColour = YELLOW;
+                } 
+            }
+            break;   
+            
+            case GREEN_YELLOW:
+            {
+                if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) 
+                { //pretty sure it's Yellow
+                
+                    LEDColour = YELLOW;
+                } 
+                else 
+                {
+                    if( (GreenProp > (GreenThreshold+Hysteresis)) && (RedProp <=(RedThreshold-Hysteresis)) ) 
+                    { //pretty sure it's green
+                        LEDColour = GREEN;
+                    } 
+                }
+            }
+            break;
+            
+            case RGB:
+            {
+                if((RedProp - GreenProp  > 30) && (RedProp - BlueProp  > 30))
+                {
+                    LEDColour = RED;
+                }
+                else
+                {       
+                    if((GreenProp - RedProp  > 30) && (GreenProp - BlueProp  > 30))
+                    {
+                        LEDColour = GREEN;
+                    }
+                    else
+                    {           
+                        if((BlueProp - RedProp  > 30) && (BlueProp - GreenProp   > 30))
+                        {
+                            LEDColour = BLUE;
+                        }
+                    }
+                }
+            }
+            break;       
+                       
+            case NOTUSED:
+            default:
+            {
+                LEDColour = ERROR;       
+            }
+            break; 
+        }
+    }
+    return LEDColour;
+}
+
+
+
 int main()
 {
     InitOutputs();
@@ -331,65 +492,124 @@
 //----------------------My TCS3472--------------------
 
 
-        SensorData =  GetSensorData(0);
+        for(SensorNumber = 0; SensorNumber < 16; SensorNumber++)
+        {
+            if(Config[((SensorNumber << 1) + 1)] != NOTUSED) //check to see if the sensor is being used
+            {  
+                SensorData =  GetSensorData(SensorNumber);
+
+                White = SensorData.White;
+                Red   = SensorData.Red;
+                Green = SensorData.Green;
+                Blue  = SensorData.Blue;
 
-        White = SensorData.White;
-        Red   = SensorData.Red;
-        Green = SensorData.Green;
-        Blue  = SensorData.Blue;
+                UpdateDisplay();
+                
+                // work out % of colour mix
+                Temp = (Red+Blue+Green);
+                RedProp = (Red* 100) / Temp ;
+                GreenProp = (Green* 100) / Temp ;
+                BlueProp = (Blue* 100) / Temp ;
+
+
+                SensorResult = GetResults(SensorNumber);
+                
+                
 
-        //Red filter
-        //Red = Filter(&AvgRed, Red, FACTOR);
-        //Green Filter
-        //Green = Filter(&AvgGreen, Green, FACTOR);
-        //Blue Filter
-        //Blue = Filter(&AvgBlue, Blue, FACTOR);
-        //White Filter
-        //White = Filter(&AvgWhite, White, FACTOR);
+                //good LED so turn on Outputs
+                switch (Config[(SensorNumber << 1)])
+                {
+                    case STD_RED:
+                    case STD_GREEN:                    
+                    case STD_BLUE:                    
+                    case STD_YELLOW: 
+                    {
+                        if (SensorResult == Config[(SensorNumber << 1)])
+                        {
+                            SetOutputBit(Config[((SensorNumber << 1)+1)]);
+                        }
+                        else
+                        {
+                            ClearOutputBit(Config[((SensorNumber << 1)+1)]);
+                        }  
+                    }
+                    break;
+                    
+                    case GREEN_YELLOW:
+                    {
+                        if (SensorResult == YELLOW)
+                        { 
+                            SetOutputBit(Config[((SensorNumber << 1)+1)]); //base output
+                            ClearOutputBit(    Config[((SensorNumber << 1)+1)] +1);//next output
+                        }
+                        else 
+                        {   
+                            if (SensorResult == GREEN)
+                            {
+                                ClearOutputBit(Config[((SensorNumber << 1)+1)]); //base output
+                                SetOutputBit(    Config[((SensorNumber << 1)+1)] +1);//next output
+                            }
+                            else
+                            {
+                                ClearOutputBit(Config[((SensorNumber << 1)+1)]); //base output
+                                ClearOutputBit(    Config[((SensorNumber << 1)+1)] +1);//next output
+                            }
+                        }      
+                    }
+                    break;
+                    
+                    case RGB:
+                    {
+                        if (SensorResult == RED)
+                        { 
+                            SetOutputBit(Config[((SensorNumber << 1)+1)]);          //base output
+                            ClearOutputBit(Config[((SensorNumber << 1)+1)] +1); //next output
+                            ClearOutputBit(Config[((SensorNumber << 1)+1)] +2); //next output
+                        }
+                        else 
+                        {   
+                            if (SensorResult == GREEN)
+                            {
+                                ClearOutputBit(Config[((SensorNumber << 1)+1)]);          //base output
+                                SetOutputBit(Config[((SensorNumber << 1)+1)] +1); //next output
+                                ClearOutputBit(Config[((SensorNumber << 1)+1)] +2); //next output
+                            }
+                            else
+                            {
+                                if (SensorResult == BLUE)
+                                {
+                                    ClearOutputBit(Config[((SensorNumber << 1)+1)]);          //base output
+                                    ClearOutputBit(Config[((SensorNumber << 1)+1)] +1); //next output
+                                    SetOutputBit(Config[((SensorNumber << 1)+1)] +2); //next output  
+                                }
+                                else  
+                                {
+                                    ClearOutputBit(Config[((SensorNumber << 1)+1)]);          //base output
+                                    ClearOutputBit(Config[((SensorNumber << 1)+1)] +1); //next output
+                                    ClearOutputBit(Config[((SensorNumber << 1)+1)] +2); //next output               
+                                }
+                                
+                            }
+                        }          
+                    }
+                    break;  
+                    
+                    default:
+                    {
+                        ClearOutputBit(Config[((SensorNumber << 1)+1)]);          //base output  
+                    }
+                     break;                       
+                 }   //end switch                             
+            } //end if
+        } //end for 
+                   
+ 
 
 
 
-//#define USEWHITE       //normalised % for each
-#ifdef USEWHITE
-        Temp = (Red+Blue+Green)*10000;
-        Temp = Temp / White;
-        TempR = (Red* 10000) / Temp ;
-        TempG = (Green* 10000) / Temp ;
-        TempB = (Blue* 10000) / Temp ;
-
-        RedProp = (TempR*100) / White;
-        GreenProp = (TempG*100) / White;
-        BlueProp = (TempB*100) / White;
-#else
-        // Vn2 without using white....
-        Temp = (Red+Blue+Green);
-        RedProp = (Red* 100) / Temp ;
-        GreenProp = (Green* 100) / Temp ;
-        BlueProp = (Blue* 100) / Temp ;
-
-        //RedProp = (TempR*100) / White;
-        //GreenProp = (TempG*100) / White;
-        //BlueProp = (TempB*100) / White;
-#endif
-
-
-
-        //make 12 bit only (display)
-        Red >>=4;
-        Green >>=4;
-        Blue >>=4;
-        White >>=4;
-
-    
-
-        lcd.locate(0, 0);
-        lcd.printf("R%02i G%02i B%02i W%04i",RedProp,GreenProp,BlueProp,White); 
-        lcd.locate(0, 1);       
-        lcd.printf("R%02i G%02i B%02i H%02i ",RedThreshold, GreenThreshold, BlueThreshold, Hysteresis);       
- 
 
         //guess the colour
-        if(White > 15) //looking at intensity somthing is on!
+        if(White > WHITE_THRESHOLD) //looking at intensity somthing is on!
         { 
         /*
             if((GreenProp <= 30) && (RedProp >=50)) 
@@ -408,6 +628,72 @@
                 }
             }
             */
+            
+            //------------------------CHECK red-----------------------------------------
+            if((RedProp - GreenProp  > 30) && (RedProp - BlueProp  > 30))
+            {
+                RedLed = 1;
+                GreenLed = 0; 
+                BlueLed = 0;           
+                YellowLed = 0;
+            }
+            else
+            {
+                 
+                if((GreenProp - RedProp  > 30) && (GreenProp - BlueProp  > 30))
+                {
+                    RedLed = 0;
+                    GreenLed = 1; 
+                    BlueLed = 0;           
+                    YellowLed = 0;
+                }
+                else
+                {           
+                    if((BlueProp - RedProp  > 30) && (BlueProp - GreenProp   > 30))
+                    {
+                        RedLed = 0;
+                        GreenLed = 0; 
+                        BlueLed = 1;           
+                        YellowLed = 0;
+                    }
+                    else
+                    {     
+                        if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) 
+                        { //pretty sure it's Yellow
+                            //YellowLed = 1;
+                            //GreenLed = 0;
+                            
+                            RedLed = 0;
+                            GreenLed = 0; 
+                            BlueLed = 0;           
+                            YellowLed = 1;;
+                        }
+                        else
+                        { 
+                            RedLed = 0;
+                            GreenLed = 0; 
+                            BlueLed = 0;           
+                            YellowLed = 0;
+                        }
+                    }
+                }
+            }
+            
+        }
+        else
+        {
+            RedLed = 0;
+            GreenLed = 0; 
+            BlueLed = 0;           
+            YellowLed = 0;
+        }    
+            
+                           
+ 
+ 
+  /*
+  //-------------------------------------------------------------------------------------------------------------------          
+            
             if( (GreenProp <= (GreenThreshold-Hysteresis)) && (RedProp >=(RedThreshold+Hysteresis)) ) 
             { //pretty sure it's Yellow
                 YellowLed = 1;
@@ -433,8 +719,11 @@
             GreenLed = 0;
             YellowLed = 0;
         }
-    }
-}
+    
+*/ 
+//----------------------------------------------------------------------------------------------------------------       
+    } //while(1)
+}//main