Hi,
Not exactly that advanced in Arduino but have got it working in the past however, just lately I've been having a huge, 20 second plus delay when sending anything to my Arduino Diecimila.
Basically I'm trying to get the simpleWrite program working in Processing but as I said there's a huge delay of about 20 seconds from when I put the cursor over the rectangle to when arduino actually executes the command.
When I send an H (turns on the LED) in arduino IDE serial monitor it works fine.
Hey just to add to my own post... Found some more out about my problems. I've edited the code slightly by putting in a delay in processing at the beginning of 'void draw()' so it the code now looks like this...
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
println(portName); // To see which port it picks
}
void draw() {
delay(500);
println(done);
background(255);
if (mouseOverRect() == true) { // If mouse is over square,
fill(204); // change color and
myPort.write('H'); // send an H to indicate mouse is over square
}
else { // If mouse is not over square,
fill(0); // change color and
myPort.write('L'); // send an L otherwise
}
rect(50, 50, 100, 100); // Draw a square
}
boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
and there's no lag! (apart from the delay) so it's kind of good news, kind of bad. As soon as I remove the delay() it reverts back to a 20 secondish delay.
Any ideas anyone? I'm thinking something along the lines of bandwidth in the processing serial library!?
My guess with what is going on with your code right, without seeing your Arduino sketch. When you scroll over your box without the delay you are flooding your arduino with serial information, not all of which might be the code H you want... When you have the delay you are sending a nice H every half second.
You might want to try decreasing the delay more and more seeing what works best.
Hey thanks for the reply,
I kind of half guessed what you were saying and so I decreased the delay() time slowly and low and behold the lag got larger and larger. Here's the arduino code and it would seem to check out with what you're saying:
// Wiring/Arduino code:
// Read data from the serial and turn ON or OFF a light depending on the value
char val; // Data received from the serial port
int ledPin = 4; // Set the pin to digital I/O 4
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
}
if (val == 'H') { // If H was received
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
}
delay(100); // Wait 100 milliseconds for next reading
}
The only thing I don't get is that this example (that's in the processing examples as a basic example) used to work but now doesn't.
I've also tried running old versions of processing and arduino but to no avail.
Surely Arduino should be able to cope with having data that it doesn't understand and just filter it out or should that be done processing side?
In fact Arduino does understand this as seen in the 'else {digitalWrite(ledPing, LOW);
}'
statement.
hmm the arduino has it's own delay... When I get home I will try it out.
If you are actually interested in a project which will be using serial communications using Firmata, or my soon to be released firmware Breakfast, is probably a better option.
Sorry I may not have made myself clear. I had another project in the summer that was basically processing interpreting sound into colours and from that it sent the data to arduino and the arduino took the colour values and sent them to 3 leds (red, green, blue). Now this doesn't seem to be working either and I'm stumped!
i had the same problem and i figured it out, i think the serial port was getting jammed with too much data, so i changed the code so that it only sends if there is a change.
here is the processing side (the arduino stayed the same)
/**
Simple Write.
Check if the mouse is over a rectangle and writes the status to the serial port.
This example works with the Wiring / Arduino program that follows below.
*/
import processing.serial.*;
Serial myPort; // Create object from Serial class
int val; // Data received from the serial port
int last = 'L';
void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}
void draw() {
//delay(500);
background(255);
if ((mouseOverRect() == true)&&(last=='L')) { // If mouse is over square,
fill(204); // change color and
myPort.write('H'); // send an H to indicate mouse is over square
last = 'H';
}
if((mouseOverRect() == false)&&(last=='H')){ // If mouse is not over square,
fill(0); // change color and
myPort.write('L'); // send an L otherwise
last = 'L';
}
rect(50, 50, 100, 100); // Draw a square
}
boolean mouseOverRect() { // Test if mouse is over square
return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
}
Thanks sreicks, I tried it and it seemed to work for a while! Although when sending lots of data (moving the mouse on and off very quickly) a delay occured. I'm sure this didn't used to happen! I used to send 256 item arrays without any lag.