hello, I have a problem communicating with my arduino leonardo.
This is the processing code:
/*
arduino ambilight
*/
//Grades
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
//----variables
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
try
{
robby = new Robot();
}
catch (AWTException e)
{
println("Robot class not supported");
exit();
}
}
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
}
}
r=r/(684*464);
g=g/(684*464);
b=b/(684*464);
//sent to arduino
println("r:" +r);
println("g:"+g);
println("b:"+b);
port.write(0xff); //synchronous data
port.write((byte)(r)); //red rate
port.write((byte)(g)); //green rate
port.write((byte)(b)); //blue rate
delay(10);
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()
{
Serial.begin(9600);
int red = 255;
int blue = 255;
int green = 255;
}
void loop()
{
/*
read colors
*/
if (Serial.available()>=4) {
if(Serial.read() == 0xff){
red = Serial.read();
green= Serial.read();
blue = Serial.read();
}
}
//sent to led
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
delay(10);
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