Go Down

Topic: Controlling RGB Leds from Live video feedback(Using processing) (Read 2406 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"

- 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 ?

dattasaurabh82

This is the final code I came up with. But since I'm not having currently ant RGB LEDs with me(They are in shipment), I'm not able to test it.

Can anybody verify it and suggest where to rectify.

Notes: Processing is ending floats and Arduino is storing them as ints in the array. Hope that won't matter much.

Anyways here's the code.

Code: [Select]
//int counter = 0;
int redPin = A0;
int greenPin = A1;
int BluePin = A2;

int incomingdata[6];

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0)
  {
  incomingdata = Serial.read();
   
  analogWrite(redPin, incomingdata[0]);
  analogWrite(BluePin, incomingdata[2]);
  analogWrite(greenPin, incomingdata[4]);
}
}

PaulS

Quote
Notes: Processing is ending floats and Arduino is storing them as ints in the array. Hope that won't matter much.

Why? Why send floats at all? Cast to an int, if you need to, and send less data.

Quote
the respective chuck of processing code :

No, it isn't. That code snippet won't even compile. Post your real code.

NI$HANT

Quote
myPort.write(float r + "," + float g + ";" + float b + ".");


You can simply load the parameters with int and you won't need any floats because you can use the Bytes to store the value at the receiving arduino end and the values can be from 0 to 255 (0 for LOW and 255 for FULL HIGH to a pin  with in between numbers to dim and MIX colors)
You can edit your processing code to sport 0-255 values inspite float values because using byte seems to be much more appropriate here.
"Real Men can Accomplish  Anything"

- skype : nishants5  

ਫ਼ਤੇਹ ਕਰੁਂ!

Go Up