I found a couple of problems with your code, on the Processing side.
First, you're missing the setup and draw functions. These perform the same role as the setup and loop functions in the Arduino.
Second, when a serial port is opened, the Arduino resets. It takes a little while before the serial port is read by the Arduino. Anything sent to the Arduino before it is ready is lost.
This is the Arduino code is ended up with:
byte inByte; // Where to store the Bytes read
int ledPin = 13; // Set the pin to digital I/O
int index = 0;
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(Serial.available() > 0)
{
inByte = Serial.read(); // Read a Byte
Serial.print(index);
Serial.print(") The arduino received: ");
Serial.println(inByte, HEX);
index++;
}
}
You can easily change this back to the code you had, since, as it turns out, the problems were all on the Processing side.
This is the Processing sketch that received all the data sent by Processing:
//This code reads the red pixel values of a 1x9 Matrix
//Then it writes to and read from the serial port
//The "writen" and "read" Bytes are then printed
import processing.serial.*;
Serial port;
int initval=0; //initial value that the matrix starts
int pixval; //pixel value each time
int rows = 9; //number of the matrix columns
PImage myImage;
int pass = 0;
void setup()
{
port = new Serial(this,Serial.list()[0], 9600);
size(300,300);
myImage = loadImage("jelly9x9.jpg");
image (myImage, 0, 0, width, height);
myImage.loadPixels();
}
void draw()
{
if(pass == 0)
{
pass = 1;
delay(2000);
for (pixval = initval; pixval < initval + 1*rows; pixval = pixval+1)
{
color a = myImage.pixels[pixval];
float ared = red(a);
int inta = int(ared);
print("Sending value #");
print(pixval);
print(" ");
println(hex(inta));
port.write(inta);
delay(500);
while(port.available()>0)
{
char inChar = port.readChar();
print(inChar);
}
println();
}
}
}
You should be able to expand on and modify this, as long as you keep the draw and setup functions, and allow time for the serial connection to be established. I had to change the index into the Serial.list array for my setup; you'll need to change it back for your setup.