We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hello all!
I am new to Processing and want to be able to send and receive data between Processing 3 and my Arduino Mega 2560.
I have been following the examples provided in the Processing Serial library, SimpleRead and SimpleWrite.
I have managed to successfully use the SimpleRead example and have sent data from my mega to Processing.
However when I try and use the SimpleWrite example, sending data from Processing to the mega, it does not work. Basically what happens is once I have uploaded the code onto the mega and run the example in Processing the RX light of the Mega 2560 starts flashing indicating that it is receiving serial data from the program, all fine and good. The on-board LED at pin 13 also remains on continuously, don't know why.
Most importantly the user defined ledPin (as defined in the Arduino programming) does not change value regardless of whether the conditions set in the Processing code have been met or not (i.e. whether or not the mouse is over the box). I have assigned the ledPin to various different digital pins and it has not made a difference.
Any help would be most appreciated! The code I am using is below:
/** * 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
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() { 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)); }
/* // 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() { while (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 }
*/
Answers
https://forum.processing.org/two/discussion/15473/readme-how-to-format-code-and-text#latest
Thanks clankill3r!
so the Processing code is the following:
The Arduino code is below:
https://Processing.org/reference/frameRate_.html
First, I recommend to use serialEvent when receiving data. I don't have a possibility to test your code, but I am pretty sure that you receive not the char itself, but maybe its code. Try to somehow understand what you receive on the arduino side. You may try also:
val = (char)Serial.read();
Thanks Ater, I will look into using serialEvent instead.
One of the biggest pitfalls is that I cannot use the serial monitor of the Arduino to check what data its receiving while the Processing sketch is running. Is there a way to print in Processing the data contents of the serial port (myPort) being sent to the mega? I have tried using
println(myPort);
but this just seems to print the address (?) of the port rather than the data its sending.Success! Managed to get the Arduino and Processing sending and receiving data between one another!
Thanks Ater! I used a serialEvent in the Processing code and this seemed to make the difference! Once I realised that I could use serial.print in the Arduino code to display text in the Processing console/program window it made it much easier to work out what the program was doing!
Thanks again!
Great! Good job.