/*############################################################# Program Name : ex1_TCS3200_measure Author : Grant Phillips Date Modified : 03/05/2016 Compiler : ARMmbed Tested On : STM32F4-Discovery Description : Example program that demonstrates the use of a TCS3200 Color Sensor Module. The sensor uses an output pulse with a varying frequency based on the amount of color detected for the selected color. This example makes and attempt to detect if an object is RED, GREEN, or BLUE by comparing the pulse values. The output is displayed on a PC using the Serial Wire Viewer (SWV). See the SWV examples for an explanation of the use of this feature. Requirements : * STM32F4-Discovery Board Circuit : * The TCS3200 Color Sensor Module is connected as: GND - GND VCC - 3V S0 - PD8 S1 - PD9 S2 - PD10 S3 - PD11 OUT - PA1 LED - PD12 (optional - otherwise connect this to 3V permanantly) * A wire link between PB3 and pin6 of the SWD connector (CN2) as explained in the SWV examples section ##############################################################*/ #include "mbed.h" #include "SWO.h" #include "TCS3200.h" SWO_Channel SWO; TCS3200 color(PD_8, PD_9, PD_10, PD_11, PA_1); //Create a TCS3200 object // S0 S1 S2 S3 OUT DigitalOut whiteleds(PD_12); //Output pin to drive white LEDs int main() { long red, green, blue, clear; //Set the scaling factor to 100% color.SetMode(TCS3200::SCALE_100); //other options: SCALE_2 and SCALE_20 - depending on lightning conditions whiteleds = 1; while(1){ /* Read the HIGH pulse width in nS for each color. The lower the value, the more of that color is detected */ red = color.ReadRed(); green = color.ReadGreen(); blue = color.ReadBlue(); clear = color.ReadClear(); /* Display pulse values in nano seconds (nS) */ SWO.printf("RED: %10d GREEN: %10d BLUE: %10d CLEAR: %10d ", red, green, blue, clear); /* Determine if the color detected is most likely RED, GREEN, or BLUE */ if((red < green) && (red < blue)) SWO.printf("RED\n"); else if((green < red) && (green < blue)) SWO.printf("GREEN\n"); else if((blue < red) && (blue < green)) SWO.printf("BLUE\n"); else SWO.printf("????\n"); wait(0.1); } }