Hey Guys!
I need help. I am doing school project with the tcs34725 color sensor and Arduino. I want to use the color rgb and temperature to visualize rain on my screen. I managed with Arduino to control the color of the rain on my computer screen, but i want to use the color temperature to control the amount of rain. The last part i can’t get to work. I am a little bit desperate. I attached my processing and Arduino code. I hope someone can help me with this!
Arduino code
#include <Wire.h>
#include "Adafruit_TCS34725.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
int R = 0;
int G = 0;
int B = 0;
int c = 0;
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
uint16_t red, green, blue, clear, colorTemp;
tcs.getRawData(&red, &green, &blue, &clear);
colorTemp = tcs.calculateColorTemperature(red, green, blue);
uint32_t sum = clear;
float r,g,b;
float c;
r= red;
r /= sum;
g= green;
g /= sum;
b= blue;
b /= sum;
r *=256;
g *=256;
b *=256;
R = r;
G = g;
B = b;
Serial.print("Color Temp");
Serial.print(":");
Serial.println(colorTemp, DEC);
Serial.flush();
Serial.print("R");
Serial.print(":");
Serial.println(R);
Serial.print("G");
Serial.print(":");
Serial.println(G);
Serial.print("B");
Serial.print(":");
Serial.println(B);
Serial.flush();
}
Processing code
import processing.serial.*;
Serial port;
//color sensor
int lf = 10; // Linefeed in ASCII.
String myString = null; //all data from arduino
float colorTemp;
float R;
float G;
float B;
color fillColor;
// falling rain
ArrayList <Drop> Drops = new ArrayList<Drop>();
Drop myDrop;
void setup(){
size(1200,700);
//color sensor
port = new Serial(this, "/dev/cu.usbmodem144101", 9600);
port.clear();
// falling rain
background(255);
for (int i=0; i <100; i++){
Drops.add(new Drop (random(width),random (-300,0),random(1,4)+2,color(random (255), random (255),random (255)),0,0,height+100-(random(300))));
}
}
void draw(){
// color sensor
while (port.available() > 0) {
myString = port.readStringUntil(lf); //read it until you hit the end of the line.
if (myString != null) { //if we did that successfully
print(myString); // Prints String
int valueStart = myString.indexOf(":");
if (valueStart > 0) {
String mySensor = myString.substring(0, valueStart); // substring from 1st to 2nd character
String myData = myString.substring(valueStart+1, myString.length()-1);
// println(mySensor + " I am the sensor");
// println(myData + " I am the data");
// println(mySensor + ":" + myData);
myData = myData.trim();
if (mySensor.equals("colorTemp"))
{
colorTemp = float(myData);
println("colorTemp:" + myData);
}
if (mySensor.equals("R"))
{
R = float(myData);
println("r:" + myData);
}
if (mySensor.equals("G"))
{
G = float(myData);
println("g:" + myData);
}
if (mySensor.equals("B"))
{
B = float(myData);
println("b:" + myData);
}
fillColor = color(R,G,B);
}
}
}
// falling rain
fill(0,70);
rect(0,0,width,height);
for (int i=0; i <100; i++){
Drop b = Drops.get(i);
b.c = fillColor;
if(b.y> b.endPos){
b.splash();
}
else{
b.rain();
}
}
port.clear(); // clean up the port so it's ready for new data
}
void stop() {
port.stop();
super.stop();
}
Second tablat
class Drop {
float x;
float y;
float speed;
color c;
float ellipseX;
float ellipseY;
float endPos;
Drop(float rainX, float rainY, float rainSpeed, color rainColor, float rainEllipseX, float rainEllipseY, float rainEndPos){
x = rainX;
y = rainY;
speed = rainSpeed;
c = rainColor;
ellipseX = rainEllipseX;
ellipseY = rainEllipseY;
endPos = rainEndPos;
}
void init(){
x = random(width);
y = random (-300,0);
speed = random(1,4)+2;
c = color(random (255), random (255),random (255));
ellipseX =0;
ellipseY =0;
endPos =height+100-(random(300));
}
void update(){
y = y + speed;
}
void rain(){
fill (c);
noStroke();
rect(x,y,5,10,20);
update();
}
void splash(){
stroke(c);
noFill();
ellipse(x,y,ellipseX,ellipseY);
ellipseY += speed * 0.2;
ellipseX += speed * 0.5;
if(ellipseX>50){
init();
}
}
}