Serial data from arduino to processing (Vital Signs monitor)

Hello there! I´m new coding with processing and I have some issues that I hope you guys can help me to understand and solve. I´m fascinated about vital signs and I´ve decided to build my own monitor, but I only have some experience with arduino and my processing codes aren´t working.
Here´s the following code:

import processing.serial.*;
Serial port;

float lastXvalue = 0;
float xValue = 5;
float lastYvalue = 0;
float yValue = 0;
float value = 0;

void setup(){
size(1024, 600); //Tamanho da tela LED IVO M101NWT2 R1
port= new Serial(this,"COM7",9600);
background(0);
}

void draw(){
/*for(int i = 300; i <= 600; i += 150){   //Talvez eu mude para somente três parâmetros: ECG (quem sabe duas derivações), pletismografia e temperatura
  stroke(255);
  line(0, i, 780, i);
}*/

fill(0, 127, 255);
stroke(0, 127, 255);
rect(0, 0, 1024, 20);

fill(255);
textSize(20);
text("00:00", 950, 18);

textSize(17);             //printa "Frequência" - ver como é realmente
fill(0, 250, 0);
text("FC", 794, 40); 

textSize(80);            //printa o valor de FC
fill(0, 250, 0);
text("100", 850, 85);

textSize(17);             //printa "temperatura"
fill(255);
text("TEMP", 794, 150);

textSize(30);             //printa o valor da temperatura
fill(255);
text("37.0 C", 900, 160);

textSize(17);
fill(0, 127, 255);
text("SpO2", 794, 200);

textSize(80);
fill(0, 127, 255);
text("99", 900, 250);

textSize(17);
fill(255, 255, 255);
text("PPG", 794, 300);
textSize(40);
text("100", 920, 320);

textSize(17);
fill(255, 255, 0);
text("RESP", 794, 350);
textSize(80);
text("15", 900, 400);

//int sistolica = 120;
//int diastolica = 80;

//int paMedia = (sistolica + diastolica) / 2;
textSize(17);
fill(255, 0, 0);
text("PA", 794, 440);
textSize(10);
text("mmHg", 794, 460);
textSize(80);
text("121", 850, 488);
text("84", 897, 570);

stroke(255);
line(15, 50, 20, 50);
line(15, 50, 15, 115);
line(15, 115, 20, 115);
textSize(10);
fill(255);
text("0.25 mV", 30, 60);

stroke(255);
line(15, 200, 20, 200);
line(15, 200, 15, 265);
line(15, 265, 20, 265);
textSize(10);
fill(255);
text("0.25 mV", 30, 210);

stroke(0, 250, 0);
line(0, 230, 780, 230);

stroke(0, 127, 255);
line(0, 400, 780, 400);

stroke(255, 255, 0);
line(0, 525, 780, 525);

String data= port.readStringUntil('\n');
  if(data!=null){
    yValue = float(data);
   
    yValue = map(yValue, 0, 1023, 0, 200);
   
    line(lastXvalue, lastYvalue, xValue, yValue);
    lastXvalue = xValue;
    xValue += 1;
    lastYvalue = yValue;

    if(xValue > 794){
      lastXvalue = 0;
      xValue = 0;
      lastYvalue = 0;
      yValue = 0;
      clear();
    }

  }
}

The program must read the data from an arduino sensor and print it on the screen with the “moving” line graph, the problem is that the printed data is delayed compared to the real sensor reading. Hope you guys can help me.

Thank you!!

Hi @IttaloAlvarenga

Welcome to the forum :slight_smile:

I would say that the problem is due to frameRate that you have in the processing loop and the ODR (output sample rate) that you have from your arduino.
From the arduino side you are sending the signals at which frequency?
You should also take into account the baud rate when sending data to ensure that all signals are being sent within the buffer capacity to output your data.

The processing frame rate of the loop is by default 60fps but you can change this using the frameRate method to define your own.

The maximum frameRate will depend on the sketch you are running as well as the machine. So, try to avoid at least duplicated code (like join all text with the same text size and you don’t need to redefine the stroke at all times, if the color of the text is always the same). One test that you can do is print the frame rate and modify your code to see how is it affected :slight_smile:

So in your case, and depending on your frequency from the arduino. You can modify the setup like this:

void setup(){
size(1024, 600); //Tamanho da tela LED IVO M101NWT2 R1
port= new Serial(this,"COM7",9600);
frameRate(?????) //PLACE HERE THE DESIRED FRAME RATE
background(0);
}

Also, the following link might be useful to you as well

Best regards

1 Like

@IttaloAlvarenga. Did you ever get your project to work? I’m curious what type of sensor you used to obtain the data.