hi forum. i just combining processing code brightest tracking to send x val to arduino but the servo wont move. this is the code
import processing.video.*;
import processing.serial.*; //import the serial library
Capture video;
int new_angle, this_angle, temp_angle;
Serial port; //define a port of theSerial type
void setup() {
size(640, 480);
video = new Capture(this, width, height);
video.start();
strokeWeight(15); //for the graphics objects
port = new Serial(this, "COM14", 9600); //connect to the Arduino serial port
noStroke();
smooth();
}
void draw(){
if (video.available()) {
video.read();
image(video, 0, 0, width, height); // Draw the webcam video onto the screen
int brightestX = 0; // X-coordinate of the brightest video pixel
int brightestY = 0; // Y-coordinate of the brightest video pixel
float brightestValue = 0; // Brightness of the brightest video pixel
// Search for the brightest pixel: For each row of pixels in the video image and
// for each pixel in the yth row, compute each pixel's index in the video
video.loadPixels();
int index = 0;
for ( int y = 0; y < video.height; y++) {
for ( int x = 0; x < video.width; x++) {
// Get the color stored in the pixel
int pixelValue = video.pixels[index];
// Determine the brightness of the pixel
float pixelBrightness = brightness(pixelValue);
// If that value is brighter than any previous, then store the
// brightness of that pixel, as well as its (x,y) location
if (pixelBrightness > brightestValue) {
brightestValue = pixelBrightness;
brightestY = y;
brightestX = x;
float X = map(brightestX, 0, width, 10, 170);
int Xx = round(X);
runServo ( Xx);
}
index++;
}
}
// Draw a large, yellow circle at the brightest pixel
fill(255, 204, 0, 128);
ellipse(brightestX, brightestY, 200, 200);
}
}
int runServo (int XX){
int angle = XX;
this_angle = angle;
new_angle = 90;
//background(angle_color, 0, 255); //clear the canvas
if (this_angle < new_angle) {
//println("1) ", new_angle, angle);
this_angle = this_angle + 5;
}
if (this_angle > new_angle) {
//println("2) ", new_angle, angle);
this_angle = this_angle - 5;
}
if (temp_angle != this_angle) {
port.write(this_angle); //write the servo angle to the serial port
temp_angle = this_angle;
println(this_angle);
}
return angle;
}
thank you for any help.