We are about to switch to a new forum software. Until then we have removed the registration on this forum.
``I am trying to build a GUI for my system to communicate to Arduino Mega 2560. The testing code works on Uno board, however, it seems does not work on Mega board. Is this still a problem for processing? Seems at 2011 someone else have reported such kind of phenomena.
The code is attached below, when it runs on Uno, there always one LED is ON, but on Mega neither of them can be switched on.
My processing code:
// Check if the mouse is over a rectangle and write the status to the serial port
final String test = "Both" ;
import processing.serial.*;
Serial port; // Create object from Serial class
void setup() {
size(200, 200);
noStroke();
frameRate(10);
// Open the port that the board is connected to and use the same speed (9600 bps)
port = new Serial(this,Serial.list()[0], 9600);
port.bufferUntil('\n');
}
void draw() {
background(255);
if (mouseOverRect() == true) { // If mouse is over square,
fill(204); // change color and
port.write('H'); // send an H to indicate mouse is over square
} else { // If mouse is not over square,
fill(0); // change color and
port.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));
}
The Arduino Mega code:
char val=0; // Data received from the serial port
int ledPin = 4; // Set the pin to digital I/O4
int ledPin1 = 6;
void setup() {
pinMode(ledPin, OUTPUT); // Set pin as OUTPUT
pinMode(ledPin1, OUTPUT);
Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available()) { // If data is available to read,
val = Serial.read();
// digitalWrite(ledPin1, HIGH);
// delay(500);
// digitalWrite(ledPin1, LOW);
// read it and store it in val
}
//Serial.print(val);
if (val == 'H') { // If H was received
digitalWrite(ledPin, HIGH); // turn the LED on
} else {
digitalWrite(ledPin, LOW); // Otherwise turn it OFF
}
if (val == 'L') { // If H was received
digitalWrite(ledPin1, HIGH); // turn the LED on
} else {
digitalWrite(ledPin1, LOW); // Otherwise turn it OFF
}
delay(100); // Wait 100 milliseconds for next reading
}
Answers
It will be really useful if you can provide the above link for completeness of your post. Nevertheless, I suggest you post in the arduino forum.
I do not know much about the mega, but my first question would be if the mega and uno have the same port mapping, at least for your above example, ledPin and ledPin1? Another question is if there is any sample code that comes with the unit that you can test together with Processing?
Kf
I'm having the same issue. The only workaround I found was putting a 10uf capacitor between ground and RST on the mega to prevent it from restarting. It works, but is just annoying because you have to pull it out every time you want to upload new code.
...Just found the solution. Put a 1 second delay after you connect to the serial port in processing.