New to processing---------motion tracking

I just learned how to use Processing, which I had known nothing about before. I didn't know how easy it was to interface with arduino!
So, my first project is a color tracking servo mounted above the camera in my imac.
I slightly modified the code (didn't write my own, i'm a noob to processing) from here:
http://www.learningprocessing.com/examples/chapter-16/example-16-11/
and then wrote some code for the arduino to receive commands and move a pointer attached to a servo accordingly.
Processing code:

import processing.serial.*;

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 16-11: Simple color tracking

import processing.video.*;

// Variable for capture device
Capture video;
Serial arduino;

// A variable for the color we are searching for.
color trackColor; 
boolean clicked;

void setup() {
  size(320,240);
  String portName = Serial.list()[0];
  arduino = new Serial(this, portName, 57600);
  video = new Capture(this,width,height,15);
  // Start off tracking for red
  trackColor = color(255,0,0);
  smooth();
}

void draw() {
  // Capture and display the video
  if (video.available()) {
    video.read();
  }
  video.loadPixels();
  image(video,0,0);

  // Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
  float worldRecord = 500; 

  // XY coordinate of closest color
  int closestX = 0;
  int closestY = 0;

  // Begin loop to walk through every pixel
  for (int x = 0; x < video.width; x ++ ) {
    for (int y = 0; y < video.height; y ++ ) {
      int loc = x + y*video.width;
      // What is current color
      color currentColor = video.pixels[loc];
      float r1 = red(currentColor);
      float g1 = green(currentColor);
      float b1 = blue(currentColor);
      float r2 = red(trackColor);
      float g2 = green(trackColor);
      float b2 = blue(trackColor);

      // Using euclidean distance to compare colors
      float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.

      // If current color is more similar to tracked color than
      // closest color, save current location and current difference
      if (d < worldRecord) {
        worldRecord = d;
        closestX = x;
        closestY = y;
      }
    }
  }

  // We only consider the color found if its color distance is less than 10. 
  // This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
  if (worldRecord < 10) { 
    arduino.write(closestX);
    arduino.write(closestY);
    println(closestX);


  }
  delay(50);
}

void mousePressed() {

  // Save color where the mouse is clicked in trackColor variable
  int loc = mouseX + mouseY*video.width;
  trackColor = video.pixels[loc];
}

Arduino code:

#include <Servo.h>

Servo neck;

int xPos=0;
int yPos = 0;

void setup()  {
  Serial.begin(57600);
  neck.attach(8);
}

void loop()  {
  if(Serial.available() >0)  {
    xPos=Serial.read();
    yPos=Serial.read();
  }
  int xServo = map(xPos, 0, 320, 20, 70);
  int yServo = map(yPos, 0, 240, 20, 70);
  neck.write(xServo);
}

I set up the code to follow a colored thing on 2 axes, but I only had 1 servo, so I wanted to only use the x axis. But the servo moves on the y axis. Why? Is it just a silly error?

I am ordering some servos today, I'll make a 2 axis version when I get them.

I set up the code to follow a colored thing on 2 axes, but I only had 1 servo, so I wanted to only use the x axis. But the servo moves on the y axis. Why? Is it just a silly error?

Wrong pin?

Sorry, I meant the code was written to recieve 2 different coordinates to drive 2 motors---not to actually drive 2 motors. So wrong pin is out of the question.

as you know not what the problem is nothing would seem out of the question
look again

One potential issue with your processing sketch is that it writes x, y, x, y, x, y, etc.

The Arduino reads x, y, x, y, etc. If a value gets lost, due to a buffer overflow, for instance, the Arduino will read y, x, y, x, etc.

You should probably have the processing sketch write something like (x y) (x y), etc. The Arduino would read the serial data, looking for a (. Then, data up to the space is x. Then, data up to the ) is y.

That way, there is no ambiguity as to which value is which, and you simply discard any values that don't come in pairs.

as you know not what the problem is nothing would seem out of the question

Well, as he stated.. he's only using 1 servo, lol... so it IS out of the question.

@Sciguy,
I'm not sure if this will solve your problem, but maybe try something like:
if(Serial.available() == 2)
You might be receiving when there is only 1 byte, so it's not giving you the 2nd. Even using > 1 might do it for you.

I'm not too familiar with Processing, so what exactly is being sent back to the Arduino?

he reads one of two values so he is reading wrong one

to recieve 2 different coordinates to drive 2 motors

arduino.write(closestX);
    arduino.write(closestY);

Yep, the xyxy vs yxyx is what I was thinking

i have modified this code. is this true for 2 servos?

#include <Servo.h>

Servo xeks;
Servo yeks;

int xPos=0;
int yPos=0;

void setup() {
Serial.begin(9600);
xeks.attach(3);
yeks.attach(6);

}

void loop() {
if(Serial.available() >0)
{
xPos=Serial.read();
yPos=Serial.read();
}
int xServo = map(xPos, 0, 320, 20, 70);
int yServo = map(yPos, 0, 240, 20, 70);
xeks.write(xServo);
yeks.write(yServo);
}

This is a sorta old topic.

But anyway, yeah, that code should work, as long as x and y don't get mixed up.
Which they probably will, so it would be good to somehow sync the two, maybe have the arduino send a message when it is ready, and the computer responds with the coordinates.

*I typed these codes but I couldnt manage to run it
:-/ :-/ :cry: help me

import processing.serial.*;

// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 16-11: Simple color tracking

import processing.video.*;

// Variable for capture device
Capture video;
Serial arduino;

// A variable for the color we are searching for.
color trackColor;
boolean clicked;

void setup() {
size(400,400);
String portName = Serial.list()[0];
arduino = new Serial(this, portName, 9600);
video = new Capture(this,width,height,15);
// Start off tracking for red
trackColor = color(0,0,255);
smooth();
}

void draw() {
// Capture and display the video
if (video.available()) {
video.read();
}
video.loadPixels();
image(video,0,0);

// Before we begin searching, the "world record" for closest color is set to a high number that is easy for the first pixel to beat.
float worldRecord = 500;

// XY coordinate of closest color
int closestX = 0;
int closestY = 0;

// Begin loop to walk through every pixel
for (int x = 0; x < video.width; x ++ ) {
for (int y = 0; y < video.height; y ++ ) {
int loc = x + y*video.width;
// What is current color
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);

// Using euclidean distance to compare colors
float d = dist(r1,g1,b1,r2,g2,b2); // We are using the dist( ) function to compare the current color with the color we are tracking.

// If current color is more similar to tracked color than
// closest color, save current location and current difference
if (d < worldRecord) {
worldRecord = d;
closestX = x;
closestY = y;
}
}
}

// We only consider the color found if its color distance is less than 10.
// This threshold of 10 is arbitrary and you can adjust this number depending on how accurate you require the tracking to be.
if (worldRecord < 10) {
fill(trackColor);
strokeWeight(4.0);
stroke(0);
ellipse(closestX,closestY,10,10);
arduino.write(closestX);
arduino.write(closestY);
println(closestX);
println(closestY);

}

}

void mousePressed() {

// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor = video.pixels[loc];
}

FOR ARDU[ch304]NO
////////////////////////////////////
#include <Servo.h>

Servo neck;
Servo neck2;

int xPos=0;
int yPos = 0;

void setup() {
Serial.begin(9600);
neck.attach(9);
neck.attach(10);
}

void loop() {
if(Serial.available() >0) {
xPos=Serial.read();
yPos=Serial.read();
}
int xServo = map(xPos, 0, 320, 20, 70);
int yServo = map(yPos, 0, 240, 20, 70);
neck.write(xServo);
neck2.write(yServo);
}

I dunno if I can help, I didn't completely understand ityself

copied the same code just inserted 2 servos its nearly same with yours I just changed a few parts a little thats all.

When you ran the processing sketch, a window with the video from the camera appears, right?
In this window you must click the object that is the color you want to track.
If you want to track a different color, then simply click on another object.

When you ran the processing sketch, a window with the video from the camera appears, right? yes
In this window you must click the object that is the color you want to track. yes
If you want to track a different color, then simply click on another object. yes

but servos make nothing :-/ arduino dont send any data to servo.

S the arduino actually recieving data?
Is it driving the servos at all? (are they wired correctly)