Problem with the use of several sensors, Arduino / Processing

Hello,

Some code I wrote to explore Arduino transmitting data to Processing.
It runs fine at a BAUD rate of 1000000. :)
I use an Arduino MEGA 2560 R3.

Arduino Code
// Serial Incoming Test (Arduino Tx)
// v1.0.0
// GLV 2020-02-29

long count;

void setup() 
  {
  //Serial.begin(9600);
  Serial.begin(1000000);
  delay(1000);
  Serial.print('\n'); //same as Serial.println()
  Serial.print("incoming");
  Serial.print('\n'); //same as Serial.println()
  }
  
void loop() 
  {         
  float x = random(0, 2)*PI;
  float y = random(2, 4)*PI;
  float z = random(4, 6)*PI;
  float u = random(8, 10*PI);
  Serial.print(count++);
  Serial.print(',');
  Serial.print(x,12);
  Serial.print(',');
  Serial.print(y,12);
  Serial.print(',');
  Serial.print(z,12);
  Serial.print(',');
  Serial.print(u,12);
  Serial.print('\n'); //same as Serial.println()
  delay(1);
  }
Processing Code
// Serial Incoming Test (Processing Rx)
// v1.0.0
// GLV 2020-02-29

import processing.serial.*; 

Serial myPort;

String inString = null;

boolean newData = false;
boolean dataValid = false;
boolean serialConnect = true;

int dataCount1, dataCount2;

static final int BAUD = 1000000;
//static final int BAUD = 9600;

//String inByte[] = {"0", "0", "0", "0", "0"};
String inByte[] = null;

public void settings()
  {  
  size(500, 600, JAVA2D);  
  }

public void setup()
  { 
  printArray(Serial.list());
  textSize(24);
 
  try
    {
    myPort = new Serial(this, Serial.list()[6], BAUD);    
    myPort.clear();
    myPort.bufferUntil('\n');  
    }
  catch (Exception e)
    {
    println("Not connected!");
    serialConnect = false;
    }
  }

public void draw()
  {
  background(0);
  
  text("Incoming Data Test", 50, 100);  
  if (newData)
    {
    for(String i: inByte) print(i + "\t");
      
    text(dataCount1, 50, 150);
    text(dataCount2, 50, 200);
    int j = 0;
    for(int i = 0; i < inByte.length; i++)
      {
      text(inByte[i], 50, 250 + 50*i);
      }
    }
  newData = false;
  }

public void serialEvent (Serial myPort) 
  {    
  inString = myPort.readString();
  //println(inString);               // See all the data include "garbage"
  inString = inString.trim();
  //println(inString);             // See all the data include "garbage"
  
  if (inString != null && inString.equals("incoming")) 
    {
    dataValid = true;
    println();
    println("Valid incoming data:");
    }
  
  else if (dataValid) 
    {               
    inByte = split(inString, ',');
    
    dataCount1++;
    
    if (inByte.length == 5) 
      {
      dataCount2++;  
      newData = true;

      //println(inByte);      //gives a warning but works; fine for debugging
      
      //printArray(inByte);

      //Cleaner print
      //for (int i = 0; i< inByte.length; i++)
      //  {
      //  print(inByte[i]+ "\t");
      //  if (i == inByte.length-1 ) println();
      //  }
     
      //Even better!
      //for(String i: inByte) print(i + "\t");
      }
    }
  }    
  

There is a lot of “garbage” data in the buffers if Arduino is powered:

  1. And you do a reset (Arduino button) while Processing is running and receiving data.
  2. And you restart Processing while Arduino is sending data.
  • I check for “incoming” string to check for valid incoming data; I added a ‘\n’ before and after so I could detect this. I am buffering until ‘\n’ so needed a clean string to detect and this did the trick!
  • There is no garbage with a “power off” and “power on” of Arduino.

I added a try\catch to see if Arduino is connected.

I have a lot of print statements for debugging; these can be commented as required.

At 9600 BAUD data is coming in slower than each frame and will flash on the screen.
At 1000000 BAUD data is coming in faster than each frame and you will skip data each frame (too fast to see anyhow); see the console and watch the counters.
You may want to consider buffering data and using this in draw as required.

I have counters on data sent from Arduino and data arriving; this is for debugging.

:)

1 Like