Serial coms to Arduino SLOOOW

I have a Processing program emulating a radar screen. Bogies are generated and slowly move toward the center - the ship. An operator moves a joystick cursor to one of the bogies, clicks the button, and the target is destroyed (disappears from the screen). This is on the battleship New Jersey museum ship. Now they would like the bearing and distance of the current bogie shown on existing synchro-driven displays.

I have a 4-motor stepper control running on a Nano, and under simple random data shows the control is working correctly.

However, when I use the com port (a USB-to-Serial converter CP2101) the comms between the two programs is incredibly slow - on the order of 1/2 second per exchange! Needless to say - this brings the Arduino loop to a crawl - and the steppers along with it!

Looking for some ideas or alternatives to serial to get the data from Processing to the Arduino. The coms do work but ever so slowly - even at 115200 baud.

Serial should work fine, can you post your Processing and Arduino code?

Enclosed are the sections of Arduino & Processing Functiosn

// Processing Collection of functions to send data to Ard //<>// //<>// //<>//

/*
There are flags for when a message or ping has been sent. When a flag is TRUE,
 it means a msg or data has been sent but not acknowleded. 
 
 
 
 
 */

String procACK = "P";
String inBuf = " ";
String dataPak = " ";
String[] serialInArray = new String[30];    // Where we'll put what we receive
int serialCount = 0;                 // A count of how many bytes we receive
int inByte;
int lf = 10, ctr = -1;                 // Starting position of the ball
boolean firstContact = false;        // Whether we've heard from the microcontroller
byte Mode;      // '0' = wait


void sendData(long dst, long brg) {
  dataPak = (':' + str(dst) +':' + str(brg) + ':' + '\n');
  //dataPak format = (":123:45:");
  comPort.write(dataPak);
  delay(6);
  //comPort.write(0);
  msgSent = true;
}


// Process incoming msgs from Arduino

void receiveData() {
  mcMsg = '.';
  charct = 0;
  if (comPort.available()>0) {
    comBuf = comPort.readStringUntil(lf);
    delay(2);
  }
  if (comBuf != null) mcMsg = comBuf.charAt(0);  // Message from Arduino/Motor controller

  switch(mcMsg) {
  case 'A':
    isConnected = true;
    wasConnected = true;
    //ardup = true;
    break;
  case 'K':
    msgSent = false;   // The msg was recieved at Arduino
    //isConnected = true;
    //wasConnected = true;
    break;
  case 'Z':  // P is the real Ping ack - z for debug
    //isConnected = true;   // Ard sent a Ping ack
    //wasConnected = true;
    pingsent = false; // Ack from Ard Ping has been  received
    pingAck = true;
    break;
  default:
    //wasConnected = pingAck = false;
    break;
  }
}



void sendTest() {
  String tstmsg = "123:345:567:888";
  for (i = 0; i<10; i++) {
    comPort.write(tstmsg);
    delay (200);
  }
}

Here Follows Ard Code:

boolean recieveData() {
	msgin = false;
	len = 0;
	cnt = ComLink.available();
	inbuf = ComLink.readStringUntil(10);
	delay(5);  // wait for next char

// Evalueate data

	if (inbuf[0] == 'A' && !haveConnected) {
		haveConnected = true;
		ComLink.write("K\n");    // ACK - Tell Proccing we are here
		//ComLink.read();  // Flush inbuf
	}
	else if (inbuf[0] == 'E') {  // Enable ACT Steppers
		ACTDST.enableOutputs();
		ACTBRG.enableOutputs();
	}
	else if (inbuf[0] == 'D') {   // Disable ACT Steppers
		ACTDST.disableOutputs();
		ACTBRG.disableOutputs();
	}
	else if (inbuf[0] == 'P') {
		ComLink.write("P\n");  //Ping response
		//ComLink.read();
		//Serial.println(pingCtr++);
	}
	else if (inbuf[0] == ':') {
		//buf = ComLink.readStringUntil(10);
		sDst = getValue(inbuf, ':', 1);
		sBrg = getValue(inbuf, ':', 2);
		dstA = long((sDst.toInt()) / DSTDIV);  // Set direction
		brgA = long((sBrg.toInt()) / BRGDIV);
		prvBrgA > prvBrgA ? Bdir = 1 : Bdir = -1;
		ComLink.write("K\n");  //Ack Msg Rcved
		msgin = true;
	}
	return (msgin);
}


String getValue(String data, char separator, int index) {
		int found = 0;
		int strIndex[] = { 0, -1 };
		int maxIndex = data.length() - 1;

		for (int i = 0; i <= maxIndex && found <= index; i++) {
			if (data.charAt(i) == separator || i == maxIndex) {
				found++;
				strIndex[0] = strIndex[1] + 1;
				strIndex[1] = (i == maxIndex) ? i + 1 : i;
			}
		}
		return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
	}

We need to see how you are handling the received data in draw() or in the serial event callback function. For the time being, check if this could help: https://forum.processing.org/one/topic/ridiculously-long-delay-on-processing-and-arduino-serial-communication.html

Kf

Currently the recieveData function is called in the draw loop. I had problems getting serialevent to work. I’m using framerate mod 120 to decide when to make the call. On the Arduino I’d like to use serialEvent, but have had probs as I’m using the altsoftserial library and haven’t found a way to get sE to work with that driver.

I’m also thinking I may be simply swamping the Arduino by sending data to frequently. I’m sure there has to be some balance between the Arduino loop and the Processing draw loop.

Work is on hold till Friday - 3 of the 4 DRV8825’s have failed! :frowning:

Thanks - Alan R.

What library are you using to control the stepper motors?
Try the accelstepper (if you’re not already using it), they free up the arduino to do other tasks while the steppers are moving.
I’ve used a arduino uno to send commands to processing and receive data back via serial to control some LEDs and a servo motor. It was fast enough to play.

Def using Accelstepper - my go-to driver library! It is beginning to look like I just didn’t handle the com-path too well. Will be looking into xxx.SerialEvent() - but I’ve had probs getting AltSoftSerial to recognize that function.