This in my university final project using Arduino UNO board and Adafruit RGB sensor.
First, I soldier the RGB sensor then I have connected it to an Arduino UNO board (see image below).
#include <Adafruit_TCS34725.h>
#include <Wire.h>
#include "Adafruit_TCS34725.h"
/* Example code for the Adafruit TCS34725 breakout library */
/* Connect SCL to analog 5
Connect SDA to analog 4
Connect VDD to 3.3V DC
Connect GROUND to common ground */
/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();
/* Initialise with specific int time and gain values */
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_101MS, TCS34725_GAIN_1X);
void setup(void) {
Serial.begin(9600);
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
//while (0);
}
// Now we're ready to get readings!
}
void loop(void) {
uint16_t r, g, b, c;
// colorTemp, lux;
tcs.getRawData(&r, &g, &b, &c);
Serial.print(r, DEC);
Serial.print(",");
Serial.print(g, DEC);
Serial.print(",");
Serial.print(b, DEC);
Serial.println(","); //IMPORTANT println
Using this code I was able to recognize any values of RGB colors (more than 16 millions).
To show images correlated to the RGB colors I decided to use Processing that use the same code system of Arduino using this code:
import processing.serial.*;
Serial mySerial; // Create object from Serial class
String myString = null;
int nl = 10;
float myVal;
int myR, myG, myB, myR2, myG2, myB2;
PFont font;
void setup() {
size(1250, 800);
pixelDensity(2);
String myPort = Serial.list()[1]; //change the 0 to a 1 or 2 etc. to match your port
mySerial = new Serial(this, myPort, 9600);
//println(myPort);
font = createFont("Helvetica", 32);
}
void draw() {
background(255);
while ( mySerial.available() > 0) { // If data is available,
myString = mySerial.readStringUntil(nl);
if (myString != null) {
myVal = float(myString);
// the code below puts the information from the Serial into a processing String Array
String[] mySplitString = split(myString, ',');
if (mySplitString.length >=3) {
myR = int(mySplitString[0]);
myR2 = (int)map(myR, 0, 7000, 0, 255);
myG = int(mySplitString[1]);
myG2 = (int)map(myG, 0, 9000, 0, 255);
myB = int(mySplitString[2]);
myB2 = (int)map(myB, 0, 8000, 0, 255);
}
// this prints values to the console
println(mySplitString.length + " " + myR2 + " " + myG2 + " " + myB2 + " " + myString);
}
}
if (myR2 > myG2 && myR2 > myB2 && myR2 > 20) {
PImage img;
img = loadImage("TINTORETTO.jpg");
image(img, 0, 0);
}
if (myG2 > myR2 && myG2 > myB2 && myG2 > 20) {
PImage img;
img = loadImage("MONET.jpg");
image(img, 0, 0);
}
if (myB2 > myR2 && myB2 > myG2 && myB2 > 20) {
PImage img;
img = loadImage("BARTHES.jpg");
image(img, 0, 0);
}
I then set up the condition as you can see in the code above in order to show three different painting correspondent at 3 different colors, red green and blue.
Comments