0
votes

I have a Bluetooth module connected to my RX(0) pin on the Arduino through which I am receiving data.
I am then printing that data using a Serial.write().
These two pins correspond to COM16 in my computer.

I am now able to receive these values into processing and print them again in processing after I set the COM port to 16 in processing.

Now I want to send a particular value out from processing back to Arduino again via serial communication. I figured I could do this with the software serial. However, I have a few questions as to how the software serial works:

If I setup a software serial, what is the COM port for the software serial for which I can send values out from processing to the Arduino?

This is the command to set the COM port in processing.

 String portName ="COM16";
 myPort = new Serial(this, portName, 57600);

I then use myPort.write() to send some values back to the Arduino but how do I capture the values in the soft serial?

2

2 Answers

1
votes

The best way is to use Serial Event

Minimal code:

void setup() {
  Serial.begin(9600);   // initialize serial
}
void loop() {           // you need to define a loop function even 
                        // if not used at all.
}

// This is an interrupt. 
// Code below will only execute when something is received in the RX pin.
void serialEvent() {
    while (Serial.available()) {
        char inChar = (char)Serial.read();  // get the new byte
        // here you can process the received byte
    }
}

If you want to receive several bytes before processing them check the example in the link provided, where it concatenates the chars into a string until the \n byte is received

The advantage of this approach is that it uses interruptions, so you don't need to constantly check if something is received in the serial port.

PD: I see that in your code you use a baudrate of 57600. Just modify the code above to make sure you use the same speed, otherwise you will not receive anything (or even worse, you will receive garbage)

0
votes

I would do it the other way around: use SoftSerial / AltSoftSerial for the Bluetooth module comms and the Hardware Serial for comms with Processing.

Bare in mind SoftSerial may not be as robust as a proper hardware serial port.Baud rate 57600 should work, but bare in mind higher baud rates may be unreliable.

Alternatively, if the budget/time allows it, you could use an Arduino that has multiple Serial ports (such Arduino Mega, Due, etc. which have Serial, Serial1, etc.)