[SOLVED] Problems sending data from Processing to Arduino through serial port

Hello everybody!

I’m newbie in Processing. I’m writing here hoping that someone will be able to help me… Thanks in advance;)

I’m trying to communicate two XBEE S1 modules between the computer and an Arduino Mega, in AT mode. The configuration of the XBEEs were done, and works well, I just need to program the communication from Processing to Arduino through the Serial Port.

I had found a lot of examples, were the Arduino send data to Processing, but neither in the other direction (From Processing to Arduino).

This are the sketches that I’ve created, but I do no get connect them and no receive any data:

_PROCESSING_Tx

/** Processing sketch */
 
import processing.serial.*;
 
Serial xbee;
 
void setup() {
  size(300, 300);
  println(Serial.list()); // print list of available serial ports
  // you might need to change the Serial.list()[VALUE]
  xbee = new Serial(this, Serial.list()[5], 9600);
}
 
void draw() {
}

void keyPressed(){
    if (key == '0') {
      xbee.write('0');
      println("ENVIO 0");
    } 
    if (key == '1') {
      xbee.write("1");
      println("ENVIO 1");
    } 
    if (key == '2') {
      xbee.write("2");
      println("ENVIO 2");
    }     
    if (key == '3') {
      xbee.write("3");
      println("ENVIO 3");
    } 
}

_ARDUINO_Rx

/** Arduino sketch */
 
#include <SoftwareSerial.h>
 
#define xbeeRx 10 // XBee DOUT
#define xbeeTx 11 // XBee DIN
 
SoftwareSerial xbee = SoftwareSerial(xbeeRx, xbeeTx);
char serialData;

void setup() {
  Serial.begin(9600);
  xbee.begin(9600);
  
  pinMode(xbeeRx, INPUT);
  pinMode(xbeeTx, OUTPUT);
  
   serialData = (char)(('0'));
  
 if (xbee.isListening()) 
    Serial.println("El puerto XBEE esta a la escucha!");
}
 
void loop() {
  
  if (xbee.available()) {
      serialData = xbee.read();
      Serial.println("HAS REBUT: ");
      Serial.println(serialData);
      if (serialData == '0') {
          Serial.println("HAS REBUT UN 0");
      }

      if (serialData == '1') {
          xbee.println("HAS REBUT UN 1");
      }
      
      if (serialData == '2') {
          xbee.println("HAS REBUT UN 2");
      }      
      
      if (serialData == '2') {
          xbee.println("HAS REBUT UN 3");
      }      
  }
}

Waiting for your proposals, thanks for your time and help!
Best!

1 Like

Hi @bacum,

I’m confused reading your code. Your processing is talking to the com port on the PC, but the Arduino code is reading “xbee” which is using software serial on 2 pins. The basic Arduino serial uses “Serial” which is built in serial from the PC via usb.

Taking your requirement “communication from Processing to Arduino through the Serial Port” I’ve used your Processing sketch with only 1 tiny change, and a very simplified Arduino sketch.

xbee = new Serial(this, "COM17", 9600); // (calling it xbee can be confusing, at the moment it's only talking to the Arduino).

My Arduino sketch started as yours, but removed xbee and software serial. Now, as you press chars on processing sketch, it flashes one of 4 LEDs. I hope you can see this work, then progressively merge in your xbee comms.


#define dW digitalWrite
 
char serialData;

int ledA = 5;
int ledB = 4;
int ledC = 3;
int ledD = 2;

void setup() 
{
  pinMode(ledA, OUTPUT);
  pinMode(ledB, OUTPUT);
  pinMode(ledC, OUTPUT);
  pinMode(ledD, OUTPUT);
  
  Serial.begin(9600);
  
  // Test the LEDs
  dW(ledA, 1); delay(100); dW(ledA, 0); delay(100);
  dW(ledB, 1); delay(100); dW(ledB, 0); delay(100);
  dW(ledC, 1); delay(100); dW(ledC, 0); delay(100);
  dW(ledD, 1); delay(100); dW(ledD, 0); delay(100);  
}
 
void loop() 
{  
  if (Serial.available()) 
  {
    // Recieve Serial from the PC
    serialData = Serial.read();
    // turn 1 led on...
    if (serialData == '0') {dW(ledA, 1);}
    if (serialData == '1') {dW(ledB, 1);}
    if (serialData == '2') {dW(ledC, 1);}      
    if (serialData == '3') {dW(ledD, 1);}      
  }
  delay(200);
  // turn all leds off.
  dW(ledA, 0);
  dW(ledB, 0);
  dW(ledC, 0);
  dW(ledD, 0);
}
1 Like

Hello,

I have done similar with BlueTooth (serial over BT) modules.

The first thing I do is make sure the code works connecting PC directly to Arduino using the USB serial.
If this serial communication works then adapt the code (just change the serial ports) to work with Blue Tooth serial and in your case XBEE serial.

The challenge is to configure the hardware correctly and then test the serial communications (USB serial first and then BT or XBEE serial).

I have a lot of serial to USB modules I can use with the additional Arduino serial ports (Serial 1, 2, and 3 on my Mega 2560) to communicate with PC (serial USB) as monitors\listeneners; these come in handy. If you don’t have these you can light up LEDs or use LCD displays as a data monitor during initial testing.
I often use a serial monitor\listener and that can be the Arduino monitor, other terminal software or even another Processing sketch.

I see that you are using software serial which also works.

You can also send data back using the same serial port; I usually do not do this as it can complicate code and troubleshooting.

I am assuming:

Xbee (serial) on PC
XBee (serial) on Arduino
USB(serial) on Arduino used to monitor received data.

Try these:
image

This is a “fix” for SimpleWrite example:

The Arduino code is in the examples in the Processing IDE and here:

This came in handy when I was setting this up:

:)

1 Like

Thanks @glv and @RichardDL for your proposals.

I had applied your tips and I had gotten some improvements.

If I change Serial.list()[2] for “COM17”, Processing detect an error:

I had changed the name my Serial Port, to no confuse anyone ;p

Working the SimpleWrite example, it seems that there is a fast connection (I receive 4 chars (LLLL) at the begin of the connection, but any char more when I move the mouse over/out of the square). If I add a delay(1000); in the setup(), I don’t receive nothing,

These are my sketches now:

_PROCESSING_Tx (SimpleWrite)

/**
 * 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, Serial.list()[2], 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));
}

_ARDUINO_Rx


char serialData;

void setup() 
{
  Serial.begin(9600);
}
 
void loop() 
{  
  if (Serial.available()) 
  {
    // Recieve Serial from the PC
    serialData = Serial.read();

    if (serialData == '0') {Serial.println("HAS REBUT UN 0");}
    if (serialData == '1') {Serial.println("HAS REBUT UN 1");}
    if (serialData == '2') {Serial.println("HAS REBUT UN 2");}      
    if (serialData == '3') {Serial.println("HAS REBUT UN 3");}      
  }
}

Thanks for your help! :wink:

1 Like

Try the “fix” to SimpleWrite:

I corrected the link in previous post.

The Arduino code for it is in the examples in the Processing IDE and here:
https://github.com/processing/processing/tree/master/java/libraries/serial/examples:

After all that is said and done you need to get a simple working example going and then start layering more complex code.

Some references:
https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all
Serial Communication - SparkFun Learn Advanced and only here as a reference

:)

1 Like

@bacum. Good that some things are working now. There are still some possible confusions in what you present. Sorry if the confusion is mine.

COM17 is the name of my Arduino’s port. Yours will probably be different. I prefer to state the name not using the name out of the list as in the given example.

In your latest Ard IDE image you are using the Serial monitor (SM) to see the Serial.prints from the Ard sketch. This is good for testing the sketch, but you must close the SM before your Processing sketch can connect to the Ard’s serial port. Only one thing at a time can use the port. Load sketch, SM, Processing. The IDE manages load and SM, but when you start connecting Processing you have to mange the situation. When Processing is connected to Ard you can’t see the Ard’s prints unless you add Processing code to do it. That’s why in my example I used LEDs to show the action.

1 Like

Hello,

You may want to start here:
https://learn.sparkfun.com/tutorials/connecting-arduino-to-processing/all

:)

1 Like

Thanks again for your inputs, guys!:wink:

I was using the SM as you say, I didn’t know that I cannot use it to visualize the data sent from Processing, just now I understand your LEDs example. I will mount it today to try the communication.

On the other hand, I was indicating the serial port wrong in Processing, I think there was the problem.

Great input too, with this I had had to detect my error with the serial port assigned in Processing. The communication Arduino-Processing works well, now I will try in the other direction.

Thanks again, I will tell you my advances later;)

hi guys! @glv @RichardDL SOLVED!! :star_struck:

Thanks a lot for your tips and time!;)) my mistakes were several:

Now I just need to power the Arduino Mega with a battery, separated of de computer to have solved my communication system. :wink:

Thanks a lot to both!! You are masters!

2 Likes

Hello,

Make sure you power it correctly:
https://store.arduino.cc/usa/mega-2560-r3 See the FAQ section

I have seen many of these damaged from incorrect power to device; I have never damaged one.

:)

1 Like

Thanks @glv, I will take care with that;)