Hi everyone,
I'm working on a university project where, through an RGB sensor, I should make images appear on processing.
I'm using codes that work on Arduino (I read all the RGB values) now I do not understand how to transport these values on Processing in such a way as to make me appear an image when the sensor get red color, another images when get blue color and another one when get greens.
Attached find the codes for Arduino and Processing that I am using
Thanks a lot in advance
ARDUINO:
#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
}
PROCESSING:
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;
// ANDREA, my comments are the code in the grey (commented)
void setup() {
size(805, 500);
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]);
// below are the incoming values > the variables with 2 at the end are the mapped values, the map range needs to be adjusted depending on room light sensitivite
// using my codes will need you co test a bit with the initial map value balance
// read the max values with white paper over the sensor to see the highest values coming from the sensor, Ardiono code you have may be better but you will need to know how to strip/read these values into Processing
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, 5000, 0, 255);
}
// this prints values to the console
println(mySplitString.length + " " + myR2 + " " + myG2 + " " + myB2 + " " + myString);
}
}
// this if statement is set to display a rectangle if the object over the sensor contains more red that green or blue
if (myR2 > myG2 && myR2 > myB2 && myR2 > 20) {
// PUT WHAT YOU WANT ON SCREEN HERE
// REPEAT CODE BLOCK AS NECESSARY WITH OTHER CONTENT AND OTHER CONDITIONAL STATEMENTS
rect(300, 300, 100, 100);
}
// this just puts the mapped values on the screen for testing purposes
textFont(font);
fill(0);
text(myR2 + " - red " + myG2 + " - green " + myB2 + " - blue ", 40, 40);
fill(myR2, myG2, myB2);
noStroke();
rect(100, 100, 100, 100);
}
rgbAndrea.ino (1.01 KB)
simpleForAndrea.pde (2.28 KB)