Pages: [1]   Go Down
Author Topic: Controlling RGB Leds from Live video feedback(Using processing)  (Read 175 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
myPort.write(r + "," + g + "," + b + ".");

code:

Code:
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:

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
...
}


Please help me get the code fixed.
 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 547
Posts: 45982
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I don't know where to start from extracting the string in the Arduino code.
That statement and this statement:
Quote
Please help me get the code fixed.
don't go together. The code you posted isn't broken.

It simply doesn't do everything that you want, yet.

There are three parts to using the data that Processing sends. First, you need to collect a whole packet into an array. You know the first character that makes up the packet (x) and the last character (.). Some code like this will collect the whole packet in an array:

.
Code:
#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Then, you need to break the packet into tokens. The strtok() function is perfect for this. Finally, you need to convert some of the tokens to numbers. The strcmp() function might be useful, as will the atoi() function.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31343
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Having done that you need to change your main loop code to use the values you have received from processing not just using fixed values.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 81
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

guys!! sorry to say so but this started sounding all alien to me. You see I'm not so regular with Arduino. So if any of you can please edit the code it would be a lot of help for me.   smiley-sweat
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31343
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

No generally we offer help we do not do things for you.

If you want someone to write the code for you then you should post in the Gigs and Collaborations section and preferably offer to pay.
Logged

Pages: [1]   Go Up
Jump to: