From arduino to processing - strange values coming in?

Hi,

I am simply trying to get values from a Arduino to Processing:
A gray scale sensor is measuring light perfectly, but the values appearing in processing are flickering between to numbers - regardless the amount of light...

The serial monitor in Arduino is working great - the values are changing with amount of light, so why dont the values appear in Processing?

Thanks.
Morten

The code in Processing:

import processing.serial.*;

Serial port; // Create object from Serial class
int val; // Data received from the serial port

void setup() {
size(400, 400);
noStroke();
frameRate(10); // Run 10 frames per second
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this, "COM3", 9600);
}

void draw() {
if (0 < port.available()) { // If data is available to read,
val = port.read(); // read it and store it in val
println(val);
}

background(204); // Clear background
fill(val); // Set fill color with the value read
rect(100, 100, 200, 200); // Draw square

}

In Processing, serial data handling should NOT be done in draw(). It should be done by implementing the serialEvent() method with the proper signature.

The Serial::bufferUntil() method should be called in setup() to trigger the serialEvent() method only when a complete packet arrives.

What is the Arduino actually sending? You are only reading one character at a time in the Processing sketch, and assuming that you have read a complete value that the Arduino sent. That seems unlikely.

You are not discarding any carriage returns or line feeds, either.

Display GPS data sent over UDP to PC running Processing.

// Processing UDP example to send and receive binary data
// https://thearduinoandme.wordpress.com/t utorials/esp8266-send-receive-binary-data/
// Badly hacked by Ray B. to display the UDP broadcast from ESP8266 

import hypermedia.net.*;             // http://ubaa.net/shared/processing/udp/udp_class_udp.htm

String ip = "10.10.10.1";            // the remote IP address of ESP8266
int port = 8888;                     // the destination port - any unused port
long previousMillis = 0;
long interval = 500;

UDP udp;


void setup() {
  udp = new UDP(this, 8888);
  udp.listen( true );
}

void draw() {
    if (previousMillis < millis() - interval) {
      previousMillis = previousMillis + interval;

        byte[] message = new byte[2];
        message[0] = 0; message[1] = 0;
        udp.send(message, ip, port);
    }
}

void keyPressed() {
  if (key == '-')
  {
      byte[] message = new byte[2];
      message[0] = 0; message[1] = 0;
      udp.send(message, ip, port);
  }
}

void keyReleased() {
  if (key != '-') {
      byte[] message = new byte[2];
      message[0] = 0; message[1] = 0;
      udp.send(message, ip, port);
  }
}

void receive( byte[] data ) {        // <– default handler
  for (int i=0; i < data.length; i++)//void receive( byte[] data, String ip, int port ) { // <– extended handler
      print(char(data[i]));
  println();
}

Entire UDP project here