Trouble using processing to control Arduino

hello everyone,

I’m posting because I am working on controlling my Arduino Mega2560 using a GUI I’m building with Processing. My goal of this project and forum is to help understand how information is passed between the two programs, so that I can later develop GUIs for other controlling purposes.

I have been reading forums and watching youtube examples to put together a simple test project. I started controlling three lights on a breadboard with the Arduino by having four buttons in a window one for each light to turn on and one to turn them all off (found on youtube). I then decided to add a different control using a slider to turn on a separate light (the slider will ultimately control the delay speed, but the light was easier to test with).

I am having trouble understanding…

  1. how/when the information is being passed between the programs to understand better how to design my later algorithms
  2. why my the lights are not responding how I intended (sometimes light comes on after pressing button seconds after I press it, and slider doesn’t always pass value string)
  3. since I’m using the serial port for the programs to talk, how do can I debug my Arduino code by viewing variables

I appreciate any help or explanations that can further my knowledge, as well as any links to other examples that might aid in my process. Thank You!!!

**I have attached my .ino and .pde files that I am using

newled.pde (1.71 KB)

ledOnOff.ino (1.17 KB)

I know almost nothing about Processing. For receiving data on the Arduino Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this (or its equivalent in another programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

I believe the problem with the delays is that you are using readStringUntil('\n') which blocks all other processing until you either read an entire line or get a timeout (??). This is true on both sides of the communication.

It is much better to check if characters are available and then store them in a buffer. If the last char received is a newline, then process the line, else move on with the rest of the program without blocking

Your Processing code opens a Serial Port   port = new Serial(this, "/dev/tty.usbmodemFD121", 9600);  // need to change port since on mac, COM3 for windows.

controlP5 is a library written for the Processing programming environment and lets you instantiates UI Controllers to build a graphical user interface on top of your processing sketch.

So your code create a few buttons and a slider with some associated callbacks

The equivalent of the arduino loop() (the draw function) then just keeps building a String as the concatenation of the speed variable and the c char variable - comma separated and terminated by a New Line character and dumps that as quickly as possible to the Serial Port.

  String strOut=char(speed)+","+char(c)+'\n';   
  port.write(strOut);

so on the Arduino side you need to be able to decode something that looks like "[color=purple]123,y\n[/color]" for example which you attempt to do with

      String str;
      char val1, val2;
      str = Serial.readStringUntil('\n');
     val1=str.charAt(0);
     //Remember: char at position 1 is the "," character
     val2=str.charAt(2);

and whilst you most likely will be able to get the proper string in, the decoding part is not what you need.

Also having delays() in your arduino code whilst the computer is blasting data through the Serial port won't work well for the small 64 bytes Serial buffer.. you'll end up loosing data

--> suggest indeed you explore Robin's tutorial