I am trying the following simple code to let processing and arduino communicate via serial port.
However it doesn't work.
The RX LED on the Arduino board keep flashing, but the LED won't light ON.
If I use the serial monitor to insert the character H, the LED can be ON.
Another solution is, when I added a 'delay(1000)' in the Arduino code, the LED can be ON as well.
I suppose the 'delay(1000)' is unnecessary, but it make the code work.
Does anyone know what is the problem?
char val = 0;
void setup() {
pinMode(12, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read data if available
if (Serial.available()) {
val = Serial.read(); // read one byte
//Serial.flush(); // flush other data
}
// Turn LED on if H was received
if (val == 'H') {
digitalWrite(12, HIGH);
} else {
digitalWrite(12, LOW);
}
}
import processing.serial.*;
Serial myPort;
int val;
void setup()
{
size(200, 200);
// the name of port connecting the arduino
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw() {
background(255);
// If mouse is over square
if (mouseX >= 50 && mouseX <= 150 &&
mouseY >= 50 && mouseY <= 150)
{
fill(204); // change color and
myPort.write('H'); // send an H to Arduino
}
else // If mouse is not over square,
{
fill(0); // change color and
myPort.write('L'); // send an L otherwise
}
// Draw a square
rect(50, 50, 100, 100);
//delay(1000);
}
Is there any problem if I send the L or H as fast as possible?
MY concept is
Processing send LLLLLLLLLL.......-> Arduino buffer store [L]
After mouse trigger
Processing send HHHHHHHH.......-> Arduino buffer store
And then the LED being turn on because it reads 'H', but it seems not work in realality.
It only work when I do in following way with delay 1000..
Processing send L(delay1000)L(delay1000)L(delay1000)L.......-> Arduino buffer store [L]
After mouse trigger
Processing send H(delay1000)H(delay1000)H(delay1000)H.......-> Arduino buffer store
LED turn ON
Is there anything wrong?
Get rid of that Serial.list()[0] line (who started that nonsense, anyway?). Replace portName with "COM3" or whatever port your Arduino uses. This works on my computer:
// the name of port connecting the arduino
myPort = new Serial(this, "COM6", 9600);
How about putting a few print statements in the Arduino program and have your processing expect them and echo them perhaps to console? Then you'll know what's going on.
BTW, by processing do you mean a PC program like you would write in C++, Java, Perl or Basic even?
Or is there a language called Processing? And if so, what's the draw from it, the good parts?
JavaMan:
Get rid of that Serial.list()[0] line (who started that nonsense, anyway?). Replace portName with "COM3" or whatever port your Arduino uses. This works on my computer:
// the name of port connecting the arduino
myPort = new Serial(this, "COM6", 9600);
Serial.list()[0] works on my computer, using a different operating system. There's no such thing as "COM6" on my computer. So why should OP change to using something that has to be changed for every computer he's on, rather than something that's even cross-kernel compatible?
Note that on unix environments, the location of the com port can change if you unplug-and-replug a device too quickly -- so the arduino may be on /dev/ttyACM0 and when unplug it and plug it back in, it'll be on /dev/ttyACM1. I don't want to have to figure out where it is and change the program accordingly when the program can do it itself.
Or is there a language called Processing?
Yes. It's what the arduino IDE is based off of. Many people use them together.
Serial.list()[0] works on my computer, using a different operating system. There's no such thing as "COM6" on my computer. So why should OP change to using something that has to be changed for every computer he's on, rather than something that's even cross-kernel compatible?
You're not using an Apple, are you? (who started that nonsense, anyway?) ]
OK, good point, but remember Serial.list()[0] did not work for the OP. Why not? There are no other issues with the posted code, because after changing that line I ran it and it worked fine.
GoForSmoke:
How about putting a few print statements in the Arduino program and have your processing expect them and echo them perhaps to console? Then you'll know what's going on.
BTW, by processing do you mean a PC program like you would write in C++, Java, Perl or Basic even?
Or is there a language called Processing? And if so, what's the draw from it, the good parts?
I will try it later, but my Arduino seems never detect the serial data bcoz I tried to add LED ON statement right inside this condition
if (Serial.available()) {
val = Serial.read(); // read one byte
}
It never jump into this condition, but I see the RX LED light keep flashing......
do what grumpy mike said. the problem with sending serial data as fast as possible is that if the receiving end cannot keep up with the speed you are sending at, things go haywire. what I usually do is have bidirectional communication going on, with the receiving end sending out a bite as soon as its ready for new data. There is example code for that both in the arduino as in the processing IDE. You will want to use this function http://processing.org/reference/libraries/serial/serialEvent_.html
Alternately make sure that you only send the char once, when you move on or off the square...
At first I thought it might be some kind of lab language. But somehow I don't see using this: http://processing.org/
to program an Arduino unless the Arduino has a graphics shield.
Arduino IDE has no graphics I ever saw, and I read the docs!
You do NOT use processing to program an Arduino.
Processing is a language run on the host computer that you can use to communicate with an arduino. As such Processing has access to all the computers resources, like graphics display, file handling, sound players and so on. This allows the arduino to control these.
The two systems communicate with each other using a serial port.
Note that when Processing opens up a serial port this resets the Arduino smothers is a few seconds delay before the arduino is ready to talk. The delay() function in Processing is not the same as in the Arduino and will not work in the same way, so it can't be used to add a delay after the serial port is open.