arduino leonardo and processing serial issues

hello, I have a problem communicating with my arduino leonardo.
This is the processing code:

arduino ambilight
import java.awt.Robot;//screen colors
import java.awt.AWTException;
import java.awt.event.InputEvent;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import processing.serial.*; //serial communication
Serial port; //serial communication
Robot robby; //Robot class
 String comport = "COM3"; //arduino port 
 int r_width = 1366; //screen resolution
 int r_height = 768;
void setup()
 port = new Serial(this, comport,9600);
 size(100, 100); //Average color window
 robby = new Robot();
 catch (AWTException e)
 println("Robot class not supported");
void draw()
 int pixel;
 float r=0;
 float g=0;
 float b=0;
 //robot class with resolution by screenshot
 BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(r_width,r_height)));
 int i=0;
 int j=0;
 for(i =0;i<r_width; i=i+2){
 for(j=0; j<r_height;j=j+2){
 pixel = screenshot.getRGB(i,j); //The average color calculation
 r = r+(int)(255&(pixel>>16)); //reds
 g = g+(int)(255&(pixel>>8)); //greens
 b = b+(int)(255&(pixel)); //blues
 //sent to arduino
 println("r:" +r);
 port.write(0xff); //synchronous data
 port.write((byte)(r)); //red rate
 port.write((byte)(g)); //green rate
 port.write((byte)(b)); //blue rate
 background(r,g,b); //processing window and average color

and this is the arduino one:

Arduino Ambilight
int red, green, blue;
int RedPin = 9; //red pin
int GreenPin = 10; //green pin
int BluePin = 11; //blue pin
void setup()
 int red = 255;
 int blue = 255;
 int green = 255;
void loop()
read colors
if (Serial.available()>=4) {
 if( == 0xff){
 red =;
 blue =;
//sent to led
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);

I receive all char if I use another arduino (not leonardo), or anoter serial terminal with leonardo, but with arduino leo and processing connected on the serial port nothing is read.
Do you have any idea?
thanks in advance

The Leonardo uses the same chip for USB as for other stuff (unlike the Uno and Mega which have separate USB chips) and this means that it disconnects from the PC when it resets. It also means that, unlike the Uno and Mega, it does NOT automatically reset when the PC program opens the serial port. All in all this can be a bit of a PITA.

You can get the PC program to force the Leonardo to reset by opening the serial port at 1200 baud and then closing it. Then allow time for the Arduino to reset and open the serial port at the baud rate your Arduino program is set for.

As a crude work-around you could try communicating with the Leonardo with the Arduino Serial Monitor, then closing the monitor and then running your Processing program.


All of the issues that Robin2 mentions happen because, unlike the Serial Monitor application, most applications that try to talk to the Arduino do not set the DTR_Enable flag of the Serial class to true.

You could dig into the Serial class in Processing, and figure out how to set DTR_Enable to true.