Serial Write - print more than one byte?

Hello there,

I’m very new to Processing, so forgive me if this is an overly simple question or whatever.

I have created a Toggle button that I’m using to toggle an LED on/off on an Arduino. In the code below, when the “Test_LEDs” button is toggled, either “T” or “F” is written to the serial port. My Arduino code reads this byte and does the appropriate action.

The code works perfectly fine, but I’d like to use a longer string instead of “T” or “F”, e.g. “LED ON” or “LED OFF”, simply because I want to add more buttons to this GUI and long strings will be easier for me to understand what each button does.

When I try to add a longer string, e.g. port.write('LED ON') I get the error message “Invalid character constant”. Is there a way to print a whole string to the serial port instead of just one letter?

Here is my code:

// MAX7221 array GUI for Arduino
import controlP5.*; //import ControlP5 library
import processing.serial.*;

Serial port;

ControlP5 cp5; //create ControlP5 object
PFont font;

void setup(){

size(300, 450);    //window size, (width, height)
smooth();
port = new Serial(this, "COM4", 9600);  // Setup Serial port - enter appropriate COM port 
cp5 = new ControlP5(this);

// create a toggle for Test Array function
cp5.addToggle("Test_LEDs")
   .setPosition(40,100)
   .setSize(50,50)
     ;


}

void draw(){ // Loop to draw the GUI

  background(0, 0 , 0); // background color of window (r, g, b) or (0 to 255

}

void Test_LEDs(boolean Test_LEDs) {
  if(Test_LEDs == true) {
    println("Test LEDs ON");
    port.write('LED');
  } else {
    println("Test LEDs OFF");
    port.write('F');
  }
}
1 Like

https://processing.org/reference/libraries/serial/Serial_write_.html

If you are sending strings it needs to be double quotes.

Stick to single characters for now until you can handle a string at the receiving end (Arduino).

port.write("LED"); //sends ASCII characters for 'L', 'E' and 'D'.
port.write('\r'); //send an ASCII character 10 (value)
// port.write(13); // same as port.write('\r');

You will have to send a delimiter (terminating character) such as ‘\r’ after to indicate where the string ends and check for this on Arduino side.

Sorry about edits… intermittent fasting today and should not be sending posts.

:slight_smile:

1 Like

This is a simple example of Arduino sending bytes to Processing:

I used a ‘p’ to tell Processing that this was start of data sent followed by R, G, B characters.

This may help you understand what you need to do from Processing to Arduino.

:slight_smile:

1 Like