Serial reading values is duplicate during about 0.3s

HI guys, I’m new to both processing and JAVA. I need to build a serial project to complete my research. I used a Arduino MEGA 2560 and a AD7606 to capture volt signals from 8 channels and the value will be sent to processing with a “|” as an end character of 8 channels. Besides, I use “enter” to end capture and save my data as csv format. However, I found that I received duplicated values during about every 0.3 seconds but not the true value my chipboard captured.
This is my code:

import processing.io.*;
import processing.serial.*;

////varibles setting
Serial myPort;
String inString;
int flag = 0;
Table table;

int starTime = 0;
float t = 0;
int Year,Month,Day,Hour,Minute;
String testname;

String R0str = new String();
float R0;

void setup() 
{
    ////setup for serial
    myPort = new Serial(this, "/dev/cu.usbmodem1101", 115200);
    myPort.bufferUntil('|');///set a buffer to divide A and B channel signal
    delay(100);
    
    

    //////setup for tips
    //print("Please input the value of R0\n");
    //print("press 'ENTER' to end input and start recording, press 'ESC' to exit program\n");
    
    //////setup for saving
    table = new Table();
    table.addColumn("time");
    table.addColumn("RA");
    starTime=0;
  
  } 

////////read buffer
void serialEvent(Serial p) {
    inString = p.readString();
}

// function 
void draw() {
    
    if (inString!= null) 
        {
          t = float(millis() - starTime) / 1000;
          print(inString);
            Year = year();
            Month = month();
            Day = day();
            Hour = hour();
            Minute = minute();
            testname = "NormR-" + str(Year) + '-' + str(Month) + '-' + str(Day) + '_' + str(Hour) + '-' + str(Minute) + ".csv";
            flag = 1;
            
      
        String list1 = inString;
            TableRow newRow = table.addRow();
            newRow.setFloat("time",t);
            newRow.setString("RA",list1);
        }
    keyPress();
    
    //println(flag,starTime);
} // func 

///detect if key is pressed
void keyPress() {
if (keyPressed)
{
     
    if (key ==  ENTER)
    {
        print("'ENTER' is pressed\n");
        saveTable(table, testname);
        delay(300);
        exit();
    }
}
}

And the value saved in csv is
截屏2022-03-18 下午4.45.01

As you can see, altough values changes in every milliseconds , the value of PROCESSING won’t change immediately but showing duplicate value for about 0.3 s. I tried to use another serial reading app to read values transfered from my ARDUINO and it shows intime and continous value change during the test, which indicates duplicate value is not a result of sampling rate of Arduino but the PROCESSING.
the value of another serial read app under here:
屏幕截图 2022-03-18 163041

I want to read values in processing because I can use it to easily deal with my datasave. However those duplicate values keep confusing me. I can’t find the reason they generated so I can’t fix this problem.
If you have any ideas?

Hello,

Try something like this that will update only when there is new data:

import processing.serial.*;

Serial myPort;
String inString;
boolean newData;
int count;

void setup() 
  {
  // List all the available serial ports
  printArray(Serial.list());  
  
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
  delay(100);
  } 

void serialEvent(Serial p) 
  {
  inString = p.readString();
  newData = true;
  count++;
  }

void draw() 
  {
  if (inString!= null && newData) 
  //if (inString!= null)  // Try it with this to see the difference.
    {
    println(frameCount, count, inString);
    newData = false;
    }
  }    

In your case only update the table when there is new data.

:)

it does work!!! Now the value is not duplicate anymore. Thank you a lot! I have never thought of this way :partying_face:

1 Like