this color recognition code works 10/10 times
Dependencies: mbed TCS3472_I2C
Diff: colort.cpp
- Revision:
- 0:1bf2fc8e5f8e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/colort.cpp Mon Apr 22 21:25:37 2019 +0000 @@ -0,0 +1,62 @@ +#include "mbed.h" +#include "TCS3472_I2C.h" + +TCS3472_I2C rgb_sensor( p9, p10 ); // Establish RGB sensor object +Serial pc(USBTX,USBRX); // Establish serial connection with computer + +PwmOut myled1(LED1); +PwmOut myled2(LED2); +PwmOut myled3(LED3); + +int main() +{ + rgb_sensor.enablePowerAndRGBC(); // Enable RGB sensor + rgb_sensor.setIntegrationTime(100); // Set integration time of sensor + + //pc.baud(921600); // Set Baud rate of serial connection + int rgb_data[4]; // declare a 4 element array to store RGB sensor readings + float PWMbrightness = 1.0; // float specifying brightness of LED (between 0.0 and 1.0) + + + rgb_sensor.enablePowerAndRGBC(); // Enable RGB sensor + rgb_sensor.setIntegrationTime(100); // Set integration time of sensor + rgb_sensor.getAllColors( rgb_data ); +// read the sensor to get red, green, and blue color data along with overall brightness + + while(1) { + + + rgb_sensor.enablePowerAndRGBC(); // Enable RGB sensor + rgb_sensor.setIntegrationTime(100); // Set integration time of sensor + rgb_sensor.getAllColors( rgb_data ); +// read the sensor to get red, green, and blue color data along with overall brightness + + // Print data to serial port + pc.printf( "unfiltered: %d, red: %d, green: %d, blue: %d \n\r", rgb_data[0], rgb_data[1], rgb_data[2], rgb_data[3]); + +//code for turning on leds coresponding to the color it is over + + + if((3000<rgb_data[0])&&(1000<rgb_data[2])){ + myled1 = 0; + myled2 = 1; + myled3 = 0; + printf("Green\n"); + + }else if((470>rgb_data[3])&&(rgb_data[2]<1000)){ + myled1 = 1; + myled2 = 0; + myled3 = 0; + printf("Red\n"); + + }else{ + myled1 = 0; + myled2 = 0; + myled3 = 1; + printf("Blue\n"); + } + + wait(0.1); + + }// end while +}// end main \ No newline at end of file