Aha - old case re-awoken.
Works fine on my Duemilanove ATMega328 and it does not work on the Mega 2560. (Yes, I have compiled for the right board, choosen the right port)
I have reduced down all the testing to a very simple Turn the LED On/Off, Show state of Switch in nice Processing/Arduino pair. They are easily modified (one line/comment change ONLY) for doing input (Arduino->Processing), output (Processing->Arduino) or both.
It shows the problem is - apparently - that Processing can not send to the Arduino. I hazard a guess that the CTS equivalent is not raised at the Arduino end, but why should only the Processing Serial be sensitive to that (it works with other software, like Serial Monitor)? I also note that tvisdead says it works on his UNO which also uses the new ATMega8U2 chip for USB ... ?
Processing
/* Test program for Processing - Arduino Communication
Hardware: USB connection to the Arduino, appropiatly eqipped (See Arduino program source)
You probably need to edit the "Port = new Serial..." line with your number
Edit the final test to choose one or both test
*/
final String test = "Both" ; // Set to one of: Input, Output, Both
import processing.serial.*; // Get the Serial library
// The screen has two halves:
// The left half is sensitive to the mousecursor:
// it will send a "LED on" command to the Arduino if cursor is on it
// The colour of the half left changes to confirm command is sent
// The right half does not care about the mouse cursor.
// It's colour reflects the last switch sense recieved from the Arduino
Serial port; // "port" is now a Serial capable variable
void setup() {
size(200, 200); // Size of interaction screen
frameRate(4) ; // Quarter Seconds updates/draw cycles
println(Serial.list()); // Prints a list of serial ports (verify correct COM number is used)
// Open the port that the Arduino board is connected to (in my case "1")
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[1], 9600);
delay(2000) ;
}
void draw() {
int inChar;
if ( test.equals("Input") || test.equals("Both") ) {
// Colour the right half, depending on last received status
while ( port.available() > 0) {
inChar = port.read(); // read it and store it in val
println(inChar) ; // optional debugging
if (inChar == 'd') { // If the serial byte received is a "d"
fill(50); // set fill to dark Gray
rect( width/2, 0, width/2, height);
}
if (inChar == 'u') { // If the serial byte received is a "u"
fill(200); // set fill to light Gray
rect( width/2, 0, width/2, height);
}
// Which implies that any other characters are simply ignored/discarded
}
}
// Test if the cursor is over the box
if ( test.equals("Output") || test.equals("Both") ) {
if (mouseX > 1 && mouseX < (width/2 - 1) &&
mouseY > 1 && mouseY < (height - 1) ) {
port.write('H'); // send an 'H' (LED on)
fill(230); // set fill to very light Gray
rect( 0, 0, width/2, height);
}
else { // mouse is elsewhere
port.write('L'); // send an 'L' (LED off)
fill(20); // set fill to very dark Gray
rect( 0, 0, width/2, height);
}
}
// frameRate=4 implies a 250 ms delay before looping
}
Arduino
/* Test program for Processing - Arduino Communication
Hardware: USB connection to the computer
For the Input test a switch that shorts pin 12 to ground (unless you redefine Input)
For output the onboard LED is used (unless you redefine Output in which case you need a LED/resistor)
Comment the "#define" if you want input, output or both tests */
#define Input 12 // Leave uncommented to test Input (Arduino -> Processing)
#define Output 13 // Leave uncommented to test Output (Processing -> Arduino)
/* The "#ifdef" "#endif" stuff will automagically be ignored by the compiler if the above pindefintion is missing
so you do NOT need to edit anything below to acitavate/deactiavte the two test */
char InByte ; // a variable to read incoming serial data into
void setup() {
Serial.begin(9600); // initialize serial communication:
#ifdef Output
pinMode(Output, OUTPUT); // LED output pin
#endif
#ifdef Input
pinMode(Input, INPUT); // Input pin to read switch
digitalWrite(Input, HIGH); // Activate the pullup resistor (so open switch reads HIGH)
#endif
delay(2000) ;
}
void loop() {
#ifdef Output
while (Serial.available() > 0) { // read any and all bytes from the serial line
InByte = Serial.read();
if (InByte == 'H') { // If an "H" then turn LED on
digitalWrite(Output, HIGH);
}
if (InByte == 'L') { // If an "L" then turn LED Off
digitalWrite(Output, LOW) ;
}
//Serial.print(InByte, DEC) ; // optional debugging
// Which implies that any other characters (linefeed, spaces, junk) are simply ignored/discarded
}
#endif
#ifdef Input
if (digitalRead(Input) == HIGH) {// test switch
Serial.print('u', BYTE); // If switch is open/high send "u" to Processing
} else {
Serial.print('d', BYTE); // else switch is closed/low, send "d" to Processing
}
#endif
delay(250); // QuartetrSecond updates
}
(edit: Corrected Input/Output confusion, in error description)