Whats wrong with my code? OpenOSC, Processing and Arduino - LED brightness

Im trying to change the brightness of a LED. The brightness is controlled by a slider on my iPhone that communicates with Processing though OpenOSC. The slider sends values between 0 and 255. My idea is that OpenOSC communicates with Processing. Processing then communicates with Arduino though serial communication.

I managed to establish the connection between the iPhone and Processing. I also managed to make an Arduino code that works when i manually type the serial input. For some reason the whole thing doesn't work. I guess the problem is the serial communication between Processing and the Arduino.

Processing code:

import processing.serial.*;
import oscP5.*;
import netP5.*;

float red = 0; 

OscP5 oscP5;
Serial Arduino;

void setup() {
  size(320, 480);

  oscP5 = new OscP5(this, 8000);
  Arduino = new Serial(this, Serial.list()[2], 9600);

void oscEvent(OscMessage ThisOscMessage) {
  String addr = ThisOscMessage.addrPattern();
  float val = ThisOscMessage.get(0).floatValue();

  if (addr.equals("/1/red/")) {
    red = val;

void draw() {
  background(red+50, 0, 0);
  rect(124, 39, 60, 255);
  fill(red, 40, 40);
  rect(124, 255+39, 60, -red);

Arduino code:

int ledPin = 9;
int brightness = 0;

void setup() 
  pinMode(ledPin, OUTPUT); 
  digitalWrite(ledPin, LOW);

void loop() 
  if (Serial.available() > 0)
    brightness = Serial.parseInt();
  analogWrite(ledPin, brightness); 

My idea is that the problem is this line in the Processing code:


I would prefer the line to look like this but then it gives an error:


You are right. Where you have


you are literally writing the string "val" to the serial port instead of the of the value of val.

It would be helpful if you included what type of error you get in your processing sketch so I can figure out what is wrong. Thanks!

EDIT: I found the problem: where you are assigning a value to the variable val, it needs to be on right side of the = rather than the left. Right now, you are assigning the value of val to red.

So where you have

red = val;

You should actually have

val = red;