We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Write Pixel data to Arduino- ARGH!
Page Index Toggle Pages: 1
Write Pixel data to Arduino- ARGH! (Read 2454 times)
Write Pixel data to Arduino- ARGH!
Sep 10th, 2009, 9:30pm
 
Ok, I know this is probably a very simple process, but I have searched for a solution to this and trial and errored until my brain hurt to no avail.

What I am trying to do is this:

Display an image (done)

get pixel data from the point where the cursor is (done)

write this pixel data to 4 variables (R,G,B,K) or K can be if R G and B are the same

output this data to Arduino for processing and output through PWM on 4 pins

The idea is that I can get different hardware responses from the user mousing over areas of different color.


Can anyone offer a newbie some assistance on this one?

Thank you!
Re: Write Pixel data to Arduino- ARGH!
Reply #1 - Sep 11th, 2009, 2:35am
 
thats pretty easy

color c = color(255,100,200);
println("red: "+ red(c));
println("green: "+ green(c));
println("blue: "+ blue(c));


there is also, brightness, hue etc available. check out the references for more information
Re: Write Pixel data to Arduino- ARGH!
Reply #2 - Sep 12th, 2009, 12:16am
 

Ok. I think I have a bit of a handle on it now:

import processing.serial.*;
Serial port;

int pixcolor;

void setup() {
  size(800, 600, P2D);
  println("Serial Port List:");
  println(Serial.list());
 
port = new Serial(this, Serial.list()[0], 9600);  
}

void draw() {
 PImage b;
 b = loadImage("PowerMouse.jpg");
 image(b, 0, 0);

pixcolor = get (mouseX, mouseY);

println("red: "+ red(pixcolor));
println("green: "+ green(pixcolor));
println("blue: "+ blue(pixcolor));

}

Now to figure out how to take the pixcolor color data and write it to the arduino.......hmmmm



Re: Write Pixel data to Arduino- ARGH!
Reply #3 - Sep 12th, 2009, 1:02am
 
Ryan B wrote on Sep 12th, 2009, 12:16am:
Now to figure out how to take the pixcolor color data and write it to the arduino.......hmmmm




This is what I have so far, and yes I seem to be answering my own questions. If anyone cares to jump into my internal discourse and correct me on my poor programming skills, please don't hesitate!!! :)

import processing.serial.*;
Serial port;

int pixcolor;
int r;
int g;
int b;

void setup() {
  size(800, 600, P2D);
  println("Serial Port List:");
  println(Serial.list());
 
port = new Serial(this, Serial.list()[0], 9600);  
}

void draw() {
 PImage x;
 x = loadImage("PowerMouse.jpg");
 image(x, 0, 0);

pixcolor = get (mouseX, mouseY);
r = int(red(pixcolor));
g = int(green(pixcolor));
b = int(blue(pixcolor));

port.write ("A");
delay(10);
println("red: "+ r);
port.write (r);
delay(10);
println("green: "+ g);
port.write (g);
delay(10);
println("blue: "+ b);
port.write (b);
delay(10);
}
Re: Write Pixel data to Arduino- ARGH!
Reply #4 - Sep 12th, 2009, 1:22am
 
cant tell you how to write it to the arduino but you should put your loadimage not in the draw function, it really slows down your programm

mport processing.serial.*;
Serial port;

color pixcolor;
int r;
int g;
int b;
PImage x;

void setup() {
 size(800, 600, P2D);
 println("Serial Port List:");
 println(Serial.list());
 x = loadImage("PowerMouse.jpg");
port = new Serial(this, Serial.list()[0], 9600);  
}

void draw() {
image(x, 0, 0);

pixcolor = get(mouseX, mouseY);
r = int(red(pixcolor));
g = int(green(pixcolor));
b = int(blue(pixcolor));

port.write ("A");
delay(10);
println("red: "+ r);
port.write (r);
delay(10);
println("green: "+ g);
port.write (g);
delay(10);
println("blue: "+ b);
port.write (b);
delay(10);
}
Re: Write Pixel data to Arduino- ARGH!
Reply #5 - Sep 13th, 2009, 2:54pm
 
]cant tell you how to write it to the arduino but you should put your loadimage not in the draw function, it really slows down your programm






Thanks for the tip, I should have caught that! Here is an update to what I have tried:


Processing code:

import processing.serial.*;
Serial port;

int pixcolor;
int r;
int g;
int b;
int k;
int inByte;

void setup() {
  size(800, 600, P2D);
  println("Serial Port List:");
  println(Serial.list());
  PImage x;
 x = loadImage("PowerMouse.jpg");
 image(x, 0, 0);
 
port = new Serial(this, Serial.list()[0], 9600);  
}

void draw() {
 

pixcolor = get (mouseX, mouseY);
r = int(red(pixcolor));
g = int(green(pixcolor));
b = int(blue(pixcolor));

     
while (port.available() > 0) {
  int inByte = port.read();
   println(inByte);
 }


if ((r+b+g)/3 == r)
   k = r;
     else k = 0;  
     
if (k != 0) {
   r = 0;
   g = 0;
   b = 0;
}  

if (g != 0) {
   r = 0;
   k = 0;
   b = 0;
}  

 if (b != 0) {
   r = 0;
   k = 0;
   g = 0;
}  

 if (r != 0) {
   g = 0;
   k = 0;
   b = 0;
}  

 

println("red: "+ r);
port.write (r);

println("green: "+ g);
port.write (g);

println("blue: "+ b);
port.write (b);

println("grey: "+ k);
port.write (k);
}

if (inByte != 1) noLoop();
 else loop();                     //My poor attempt at flow control, when
                                      // When the arduino returns a 1, send next
                                      // batch of data

 }




Arduino Code:



const int magPin = 9;
const int thermo1 = 5;
const int thermo2 = 6;
const int vibe = 1;
int r;
int g;
int b;
int k;


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

 pinMode(magPin, OUTPUT);
 pinMode(thermo1, OUTPUT);
 pinMode(thermo2, OUTPUT);
 pinMode(vibe, OUTPUT);
 Serial.write(2);
}

void loop() {
 
 
 int data;
 
  Serial.write(1); // here is the data bit that is sent to Processing to
                          // trigger next data send

 
     if (Serial.available())
   {
    data = Serial.read();
     }
    data = r;
     if (Serial.available())
   {
    data = Serial.read();
     }
    data = g;
     if (Serial.available())
   {
    data = Serial.read();
     }
    data = b;
     if (Serial.available())
   {
    data = Serial.read();
     }
    data = k;
   
     Serial.write(1);

 analogWrite (magPin, k);

   analogWrite (vibe, g);
 
 if (r > 10) {
   digitalWrite (thermo1, HIGH);
   digitalWrite (thermo2, LOW);
   }
 
 if (b > 10) {
   digitalWrite (thermo1, LOW);
   digitalWrite (thermo2, HIGH);
   }
 }
}


Of course this doesnt work, but I don't know enough about serial communications to make this work. I am running into a tight hard deadline to get this up and running, any HELP! would be totally appreciated!!!


Note: One of the things I need to maintain is the value of the colors as they are transferred to the Arduino. Mostly this is just for the K (greyscale) & G values as they are  going to be used to vary the power to an electromagnet and a vibration motor respectively.  The R & B are not as critical (i.e. they can be simple on off digital signals) because they are just driving the pins of an H Bridge to drive and reverse the voltage through a Peltier.

So the flow goes like this. The user mouses over colored boxes and greyscale gradients. The associated components (magnet, vibe motor and Peltier are all mounted inside the users mouse so that the user receives feedback when mousing over the test boxes. (i.e. mousing over red box makes mouse hot, blue cold, green applies varying degrees of vibration, and the grayscale gradient causes the mouse to be more resistive to movement)

Re: Write Pixel data to Arduino- ARGH!
Reply #6 - Sep 25th, 2009, 2:39am
 
There's a couple of things that you could improve really easily.

you have
Code:
     if (Serial.available())
  {
   data = Serial.read();
    }
   data = r;


You probably meant 'r = data;' rather than the other way around, no?
Also you're better off waiting for serial data to be available, since you need to receive all three bytes sequentially.

try something like:
Code:
while(!Serial.available()); // wait
r = Serial.read(); // read

and repeat for the other three parameters. Note the semicolon after while() - it does nothing until there's data available.

However there's another problem. You're reading three bytes, one at a time, but sending Java ints which are 32 bits, or 4 bytes, long. So if you want to keep this protocol then you need to change your Java code to send bytes and to make sure that the data values don't exceed 255.

If you want to stick with ints then you need to change the arduino code to read them. The equivalent C datatype is a long.
Code:
long r;
while(Serial.available() < 3); // wait
r = (Serial.read() << 3) | (Serial.read() << 2) | (Serial.read() << 1) | Serial.read();


At least I think that's right, provided Processing will send the most significant byte first... Otherwise turn the sequence around.

Now this will still not be a very robust implementation, partly because your arduino has no way of telling if it is reading the right byte. To do that you would have to use some sort of marker as part of the protocol, e. g. reserve the top 2 bits of every 4 bytes to say which colour you are sending.

Alternatively, if you prefer not to mess with any of the gory details of serial protocols you could try http://firmata.org, which has its own Processing library. Not sure if it does PWM but you could easily find out.

have fun,

/m
Re: Write Pixel data to Arduino- ARGH!
Reply #7 - Oct 2nd, 2009, 8:07am
 
Thanks marser for the ode improvements! the running time really improved!
----------------------
--------------------------------
http://mycoverthypnosis.com
Page Index Toggle Pages: 1