Go Down

Topic: Controlling RGB Leds from Live video feedback(Using processing) (Read 2 times) previous topic - next topic

dattasaurabh82

Hello.
I'm trying to build a project where in a acrylic pillar will change it's color according to the ambiance.
Now to do that I've written (With taking some help) a program in Processing which would send serially the R G B values from the live camera video.
(The Processing sketch is attached below)
here's the snippet from the processing sketch which sends R G B Values to the serial port.
Code: [Select]
myPort.write(r + "," + g + "," + b + ".");
code:
Code: [Select]
import processing.video.*;
import processing.serial.*;
Serial myPort;
Capture video;
float m,r,g,b;
int loc;
boolean R = false;

PFont f;

void setup() {
  size(640,480);
  myPort = new Serial(this, "COM11", 9600);
  video = new Capture(this,width,height,15);
  f = createFont("Haettenschweiler",12);
}

void draw() {
  if (video.available()) {
    video.read();
  }
  image(video,0,0);
 
  for (int i = 0; i < video.width; i+=10) {
    for (int j = 0; j < video.height; j+=10) {
      loc = i + j * width;     

      color c = video.pixels[loc]; // Get a pixel   
     
      r = red(c);
      g = green(c);
      b = blue(c); // Get the red value

      m = map(i, 0, width-50,50, 100);
     
      stroke(255,0,0);
      point(m-40,-r/2+height-5);
     
      stroke(0,255,0);
      point(m+20,-g/2+height-5);
     
      stroke(0,0,255);
      point(m+80,-b/2+height-5);
    }
  }

  textSize(10);
  fill(255,0,0); 
  text(nf(r,2,0),200,height-60);
  fill(0,255,0); 
  text(nf(g,2,0),200,height-45);
  fill(0,0,255); 
  text(nf(b,2,0),200,height-30);
  myPort.write(r + "," + g + "," + b + ".");
  println(r + "," + g + "," + b);
}



Now I need an Arduino sketch which would utilize this received string to write, with PWM, the RGB LED's Pins Accordingly for according color settings.
I don't know where to start from extracting the string in the Arduino code.
I found a simple RGB control program on web which I'm attaching here(with a bit of my own twitching)

Code: [Select]
int redPin = 11;
int greenPin = 10;
int bluePin = 9;

void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT); 
 
  Serial.begin(9600);
}

void loop()
{
  setColor(255, 0, 0);  // red
  delay(1000);
  setColor(0, 255, 0);  // green
  delay(1000);
  setColor(0, 0, 255);  // blue
  delay(1000);
  setColor(255, 255, 0);  // yellow
  delay(1000); 
  setColor(80, 0, 80);  // purple
  delay(1000);
  setColor(0, 255, 255);  // aqua
  delay(1000);
}

void setColor(int red, int green, int blue)
{
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue); 
}

void serialEvent() {
  while (Serial.available())
{
//from here on I need some help
...
}


I got this reply here :

http://forum.arduino.cc/index.php?topic=185841.new;topicseen#new

Can anybody help me here?

Arrch

So the Arduino program that you posted works with regards to controlling the LEDs? You just need to parse the string coming from processing and set the appropriate colors based on that serial input?

dattasaurabh82

That is where I'm stuck. I know parsing from strings and bytes in Processing but is a bit confuse in terms of Arduino. So I need the help in code.

NI$HANT

You will need to get the data sent from the processing side sent within Packets , Data packets which will be sent to the Arduino serially, your current function will deal with it you have to read into a byte until you encounter COMMA and then read again also again till COMMA
"Real Men can Accomplish  Anything"
-  Website  
- skype : nishants5  

????? ???? !

dattasaurabh82

Hi.
Thanks for replying.
It would be a pleasure if you could tell me which data type to assign as the suffix to an array which contains both character and float .
I mean, my processing sketch is say writing these values in serial port at some instance.

23.00,34.00,54.00.

here 23.00, 34.00 and 54.00 are floats and "," is the character.

the respective chuck of processing code :
Code: [Select]
myPort.write(float r + "," + float g + ";" + float b + ".");


Now to assign an array and access from it would need

Code: [Select]
//for defining the variable
int incomingdata[16]; //say
// or
char incomingdata[16];


And for accessing the data from the array and assigning it to something may be like this :

Code: [Select]
analogWrite(A2, incomingdata[10]);// writing say Pin A2 with the incoming value stored in 10th chamber of the array "incomingdata"

Now the questions are:

How can I Afford both "," and "floats" in the same array (Do I really need to do that)
and then how to convert floats in to integers ?

Go Up