Processing and teensy arduino problem

Hi

I have a problem with processing code and teensy 2 ++ controler. I create a ambilight fonction with teensy 2 ++. I have two programs (program for arduino and program for processing. When i start the two programs, i have a error message like this:

java.io.IOException: Input/output error in writeByte
at gnu.io.RXTXPort.writeByte(Native Method)
at gnu.io.RXTXPort$SerialOutputStream.write(RXTXPort.java:1093)
at processing.serial.Serial.write(Unknown Source)
at sketch_121106b.draw(sketch_121106b.java:86)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)
processing.app.debug.RunnerException: RuntimeException: Error inside Serial.write()
at processing.app.Sketch.placeException(Sketch.java:1543)
at processing.app.debug.Runner.findException(Runner.java:582)
at processing.app.debug.Runner.reportException(Runner.java:558)
at processing.app.debug.Runner.exception(Runner.java:498)
at processing.app.debug.EventThread.exceptionEvent(EventThread.java:367)
at processing.app.debug.EventThread.handleEvent(EventThread.java:255)
at processing.app.debug.EventThread.run(EventThread.java:89)
Exception in thread "Animation Thread" java.lang.RuntimeException: Error inside Serial.write()
at processing.serial.Serial.errorMessage(Unknown Source)
at processing.serial.Serial.write(Unknown Source)
at sketch_121106b.draw(sketch_121106b.java:86)
at processing.core.PApplet.handleDraw(Unknown Source)
at processing.core.PApplet.run(Unknown Source)
at java.lang.Thread.run(Thread.java:619)

my processing code is:

import java.awt.Robot; //java library that lets us take screenshots
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.*; //library for serial communication




Serial port; //creates object "port" of serial class
Robot robby; //creates object "robby" of robot class


void setup()
{
port = new Serial(this, Serial.list()[0],9600); //set baud rate
size(100, 100); //window size (doesn't matter)
try //standard Robot class error check 
{
robby = new Robot();
}
catch (AWTException e)
{
println("Robot class not supported by your system!");
exit();
}
}


void draw()
{
int pixel; //ARGB variable with 32 int bytes where 
//sets of 8 bytes are: Alpha, Red, Green, Blue 
float r=0;
float g=0;
float b=0;


//get screenshot into object "screenshot" of class BufferedImage 
BufferedImage screenshot = robby.createScreenCapture(new Rectangle(new Dimension(1368,928)));
//1368*928 is the screen resolution




int i=0;
int j=0;
//1368*928
//I skip every alternate pixel making my program 4 times faster
for(i =0;i<1368; i=i+2){ 
for(j=0; j<928;j=j+2){
pixel = screenshot.getRGB(i,j); //the ARGB integer has the colors of pixel (i,j)
r = r+(int)(255&(pixel>>16)); //add up reds
g = g+(int)(255&(pixel>>8)); //add up greens
b = b+(int)(255&(pixel)); //add up blues
}
}
r=r/(684*464); //average red (remember that I skipped ever alternate pixel)
g=g/(684*464); //average green
b=b/(684*464); //average blue




port.write(0xff); //write marker (0xff) for synchronization
port.write((byte)(r)); //write red value
port.write((byte)(g)); //write green value
port.write((byte)(b)); //write blue value
delay(10); //delay for safety


background(r,g,b); //make window background average color
}

My arduino code

Arduino Code: (transfer this program to the Arduino)
//Developed by Rajarshi Roy
int red, green, blue; //red, green and blue values
int RedPin = 25 //Red pin 25 has a PWM
int GreenPin = 26; //Green pin 26 has a PWM
int BluePin = 24 //Blue pin 24 has a PWM
void setup()
{


Serial.begin(9600);
//initial values (no significance)
int red = 255;
int blue = 255;
int green = 255;
}


void loop()
{


//protocol expects data in format of 4 bytes
//(xff) as a marker to ensure proper synchronization always
//followed by red, green, blue bytes
if (Serial.available()>=4) {
if(Serial.read() == 0xff){
red = Serial.read();
green= Serial.read();
blue = Serial.read();
}
}
//finally control led brightness through pulse-width modulation
analogWrite (RedPin, red);
analogWrite (GreenPin, green);
analogWrite (BluePin, blue);
delay(10); //just to be safe
}

I am a noob in programing an i don't understand this problem
can you help me please
thanks for next answers

port = new Serial(this, Serial.list()[0],9600); //set baud rate

Are you certain the Teensy is the first serial port on your computer?

hello Coding Badly, thanks for this first answer

I try every COM port available in my computer and the result is the same. My strip led doesn't light. I show you in this photo the port com available. I have COM1 and COM2

I explain you with more details:
firstly, i use only arduino software : (software parameter COM is on COM 2) I add a initialisation code in arduino program in order to know if my electric installation is ok and i program a red light during 2 seconds then green light 2 seconds and finally blue light 2 seconds. I upload the code in teensy 2++ and my strip led light correctly.

Secondely, i try to modify the processing code and change the "0" at port line to [1] or [2]. I run the processing program (arduino code is in the teensy) and the strip led doesn't light.

Do you think the problem is about the COM port or perhaps there are problems with Java or RXTX ?

Sorry for my english, i am french, thanks for next answers

Unfortunately, that's all the help I can offer. I have not ever used Processing. Hopefully, someone else with more (any) experience can help.

I try every COM port available in my computer and the result is the same

That is not the point. Processing has a list of com ports, it is not in the same order as the COM number.
Try printing the serial list to see what com port is the one that is first in the list. I think you will find it is not COM 2.

Hi, thanks for this

I try what you say and i haven't error message in processing but my strip led still not works.
I explain you:

My teensy is on port COM2. I choose Port COM2 on arduino option software and i upload program on teensy. I close arduino software and i open processing. I change the number port on code line "serial" (Println show me COM2 is on position [1]) and i run processing. I have no message but the new window on screen doesn't works (My wallperper is blue and there is no caption about the colour of the screen.
But if i change [1] to [2], the run processing window is blue (like my wallpaper).but my strip led doesn't light
I don't understand this problem (perhaps my usb are usb1 : old computer)
Have you a solution?
Thanks for next answers

Hello

No reply, please help me !! :~

Add this line to your processing sketch right after void setup()

println(Serial.list());

That line will print your ports that are connected in the bottom black box of the processing IDE. It will list everything connected to a port.

It will look like this in the case of the computer I am on.

[0] "COM1"
[1] "COM3"

Hi

I did this before posting my last message and processing show me in black window that COM1 on position 0 and COM2 on position 1 are available. i modify my program with "Serial.list(),[1]" and i run processing.i have no result, my strip led doesn't light. I try every COM port with concordance with arduino software option and nothing.

My strip led light with arduino software but nothing with processing

See you next post

my strip led doesn't light.

Does the RX led even blink? If not, Processing and the Arduino are not talking. If it does, the Arduino isn't getting the message or interpreting it correctly.

Hello

My rx led light
I work on this problem this week end and a find this:

if i modify the line if (Serial.available()>=4) by this line if (Serial.available()) on arduino program, my strip led light but i have only white colour (with less variations of smooth colours)
The processing little window show me the dominant colour on the screen

Have you a idea to resolve this problem ? (perhaps to many informations arrive in same time on teensy)

thanks for next answers

I don't understand some of your Processing code:

float r=0;
float g=0;
float b=0;

r = r+(int)(255&(pixel>>16)); //add up reds
g = g+(int)(255&(pixel>>8)); //add up greens
b = b+(int)(255&(pixel)); //add up blues

r=r/(684*464); //average red (remember that I skipped ever alternate pixel)
g=g/(684*464); //average green
b=b/(684*464); //average blue

port.write((byte)(r)); //write red value
port.write((byte)(g)); //write green value
port.write((byte)(b)); //write blue value

You are reading integer data, and storing it in floats. Then, you are performing integer arithmetic on floats. Why floats?

There is nothing on the Arduino side to echo what you received. Why not?

There is nothing in the Processing code to collect any replies from the Arduino, and show them. Why not?

thanks for this answer, but these questions are too difficult for me. Im a noob on programmation and i can't explain you the details of the program
I just take exemple on a web site in order to make ambilight. this link

If you want to modify the code ?

thanks for next answers

This untested but, if you add the bottom 3 lines you should see the results of r, g, b on the processing black monitor.

port.write((byte)(r)); //write red value
port.write((byte)(g)); //write green value
port.write((byte)(b)); //write blue value

//add next  lines
println((byte)(r)); //print on bottom of processing screen
println((byte)(g));
println((byte)(b));

hello

i have numbers in black window of processing when i run these 3 lines but only with this line in arduino IDE:
if(serial.available());

but my strip led light in wrong colour, only white with glitter

arduino IDE doesn't like "if(serial.available()>=4);

my port is ok because i see my strip led light.I have usb serial (abstact.....) on windows pheripheral manager. I choose good port on arduino IDE and usb type "Serial".

do you have an other idea?

thanks fot next answer

arduino IDE doesn't like "if(serial.available()>=4);

Why are you putting a semicolon at the end of an if statement? That forms the body of the if statement, and is, effectively a no-op. Do nothing if the statement is true. Do nothing if the statement is false.

By the way, surely you know that the Serial instance uses a capital S.

MisterT2000:
hello

i have numbers in black window of processing when i run these 3 lines but only with this line in arduino IDE:
if(serial.available());

but my strip led light in wrong colour, only white with glitter

arduino IDE doesn't like "if(serial.available()>=4);

Your arduino is expecting 4 bytes at a time, the original code you posted was correct. Proofread this part VERY carefully. If "the IDE doesn't like it" and shows an error when you hit "Verify", then proofread it again VERY carefully :slight_smile:

//protocol expects data in format of 4 bytes
//(xff) as a marker to ensure proper synchronization always
//followed by red, green, blue bytes
if (Serial.available()>=4) {
   if(Serial.read() == 0xff){
      red = Serial.read();
      green= Serial.read();
      blue = Serial.read();
   }
}