I am very new at this and probably took on a project that was too big for my experience level. Here's to hoping that the brains in the Arduino forum can help!
I am trying to create a hardware response to data fed to Arduino from Processing. I have a looping stream of 4 values (999,R,G,B) constantly flowing from processing. 999 is a constant that I was using to keep the two in sync. R,G,and B are int variables.
First my code from Processing:
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"); // display test image
image(x, 0, 0);
//read pixel data at current mouse position and write to 3 variables
pixcolor = get (mouseX, mouseY);
r = int(red(pixcolor));
g = int(green(pixcolor));
b = int(blue(pixcolor));
//sequentially write variables to serial port
port.write (999);
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);
}
I think the Processing portion is ok(probably better ways to do it but it seems to work), but I am totally unsure of my 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);
}
// looping function to read serial data in 4 chunks
void loop() {
byte data;
if (Serial.available()) {
data = Serial.read(); // initial read
}
// if 999 is not read go back to loop() and start over
if (data != 999) loop();
else {
data = Serial.read(); //read next data and write to variable r
data = r;
data = Serial.read(); //read next data and write to variable g
data = g;
data = Serial.read(); //read next data and write to variable b
data = b;
// if R,G and B are the same write this value to K (Greyscale)
if (r == g)
{if (g == b) k = r; //i'm sure this is wayyy wrong
else loop();
}
else k == 0;
}
analogWrite (magPin, k); // Write K value to PWM pin
//if pixel is red turn on H Bridge direction 1
if (r > 10) {
digitalWrite (thermo1, HIGH);
digitalWrite (thermo2, LOW);
}
//if pixel is blue turn on H bridge direction 2
if (b > 10) {
digitalWrite (thermo1, LOW);
digitalWrite (thermo2, HIGH);
}
}
OK, I know that was ugly, but I don't know another way to do this. From my initial tests, this doesn't seem to work anyway. Please help a noob! I'm trying to learn, but running out of time. Feel free to pester me on AIM/iChat at bremnerr@me.com