JMyron Brightness to Arduino

I’m trying to write a sketch to transfer the x and y values - from the p5 brightness tracker example (which I’ve re-written for JMyron as the original won’t work on my computer) - to a pan-and-tilt servo setup - with the xVal brightest point mapping to one servo and the yVal mapping to the other servo.

I’m having problems with getting processing to talk to my arduino and possibly incorrect coding. Can someone have a look at this and make suggestions please.

I’m new to these programs so any useful suggestions will be greatly appreciated.

Code in Processing:

import JMyron.;
JMyron m;//a camera object
import processing.serial.
;
Serial arduino; // Create object from Serial class

float sendTime = 0;
float intervalSendTime = 100; //milliseconds between serial data sent from p5

int img;

void setup(){
size(320,240);
m = new JMyron();//make a new instance of the object
m.start(width,height);//start a capture at 320x240
m.findGlobs(0);//disable the intelligence to speed up frame rate
println(Serial.list());
String portNames = Serial.list()[1];
arduino = new Serial(this, portNames, 19200);
}

void draw(){
m.update();//update the camera view
img = m.image(); //get the normal image of the camera

loadPixels();
for(int i=0;i<widthheight;i++){ //loop through all the pixels
pixels _= img
; //draw each pixel to the screen*_
* }*
* updatePixels();*
* 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*
* int index = 0;*
* for (int y = 0; y < 240; y++) {*
* for (int x = 0; x < 320; x++) {*
* // Get the color stored in the pixel*
* int pixelValue = img[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;*
* }*
* index++;*
* }*
* }*
* // Draw a large, red circle at the brightest pixel*
* fill(255, 0, 255, 128);*
* ellipse(brightestX, brightestY, 20, 20);*
* int xVal = int(map(brightestX, 0 , width, 0, 100));*
* int yVal = int(map(brightestY, 0 , height, 0, 100));*
* //print("x " + xVal);*
* //println(" – y " + yVal);*
* float currentTime = millis();*

* if(currentTime > sendTime + intervalSendTime){*
* arduino.write(101); *
* arduino.write(xVal); *
* arduino.write(yVal);*
* sendTime = currentTime;*
* }*
}
public void stop(){
* m.stop();//stop the object*
* super.stop();*
}
Code in Arduino:
int xVal = 0;
int yVal = 0;
void setup(){
* Serial.begin(19200);*
}
void loop(){
* if (Serial.available() > 2) {*
* //read the incoming byte:*
* int incomingByte = Serial.read();*
* if(incomingByte == 101){*
* incomingByte = Serial.read();*
* xVal = incomingByte;*
* incomingByte = Serial.read();*
* yVal = incomingByte;*
* }*

* Serial.print("x = ");*
* Serial.print(xVal);*
* Serial.print("-- y = ");*
* Serial.print(yVal);*

* }*
}
int servoPin1 = 6; // servo 1 connected to digital pin 6
int servoPin2 = 7; // servo 2 connected to digital pin 7
int Angle; // angle of servo 1
int pulseWidth; // function variable 1

void servoPulse(int Pin, int Angle) {
_ pulseWidth = (Angle * 2) + 500; // converts angle to microseconds_
* digitalWrite(Pin, HIGH); // set servo high*
* delayMicroseconds(pulseWidth); // wait a very small amount*
* digitalWrite(Pin, LOW); // set servo low*
* delay(10); // refresh cycle of typical servos (20 ms)*
* pinMode(servoPin1, OUTPUT); // set servoPin1 pin as output*
* pinMode(servoPin2, OUTPUT); // set servoPin2 pin as output*
* Serial.begin(19200);*
}

_ /Angle= xVal("x = "); _
_
//Angle= (Angle - 50);_
_
servoPulse( Angle);*_

* //Serial.println(Angle);*

* Angle= yVal(1); *
* //Angle= (Angle + 400);*
* servoPulse(yVal, Angle);*

* //Serial.println(Angle);*

*/