Save Arduino input in array, and THEN plot it

So I’m getting quite mad not being able to accomplish something so easy.
I’ve an Arduino sending various values through the serial port. The values appearing are for example 152,0,140,92,53…etc.
I’d like to just save for example 100 of those values and then stop recording.
I can’t actually get a functioning sequence without the port “shutting” itself or the data not being recorded at all.

I’ve looked through TONS of tutorials but I never get it to work.
Can someone please guide me through a working version of it?

The data grabbing sequence is what follows

void serialEvent(Serial mySerial)
{
String inString = mySerial.readStringUntil('\n');
 
if (inString!=null); // this area splits the arduino inputs such that they can be received seperately
   {
     inString=trim(inString);
      float[] sensors= float (split(inString, ","));
 
      if (sensors.length>=2)
         {
          gauge0[k]=sensors[0];
          gauge1[k]=sensors[1];
          k++;
        }
   }
}
1 Like
2 Likes

Ok mate, first of all thanks A LOT, I’ve read through your post and tried to make the best out of it. I actually took the code from the 17th post and changed the draw() to get a simple timeplot.

Still, I have some doubts regarding how can I acquire/plot a limited amount of values. Sure, just setting up an if(k<values) condition and a k++ inside works, but I don’t know if it’s the best (an elegant) solution. I see the bufferUntil(ENTER), should I work with that?

Also, really less importantly, I tried to start with a blank screen with a “click start to measure”, which would send a bit to Arduino to start the measuring. Unfortunately, that first bit would instantly trigger the serial event and my program stops working.

My programs as now are:
Arduino

void setup() {
  Serial.begin(9600);

}

void loop() {
  const int x = map(analogRead(A5), 0, 1023, 0, 300);
  const int y = map(analogRead(A4), 0, 1023, 0, 300);
  Serial.print(x);
  Serial.write('\t'); // '\t'
  Serial.println(y);


  delay(20);
}

Processing:

import processing.serial.Serial;
 
Serial mySerial;

int k=0;
float max0=0;
float max1=0;
float prevY_0;
float prevY_1;

static final int VALS = 2;
int[] vals = new int[VALS];

void setup() {
  size(800, 600);
//noLoop();     <--- ALSO I CAN'T FIGURE WHAT'S THIS FOR...

  stroke(-1);
  clear();

  final String[] ports = Serial.list();
  printArray(ports);

  new Serial(this, "COM11", 9600).bufferUntil(ENTER);
}

 
 void draw() {
  if(k<500){
    println(vals[0]);
    float plotVar_0 = vals[0];
    stroke(#FF0000);
    line(frameCount-1, 150-prevY_0, frameCount, 150-plotVar_0);
    prevY_0 = plotVar_0;
    if (plotVar_0>max0){
      max0=plotVar_0;
      }
  println(vals[1]);
    float plotVar_1 = vals[1];
    stroke(#FF0000);
    line(frameCount-1, 150-prevY_1, frameCount, 150-plotVar_1);
    prevY_1 = plotVar_1;
    if (plotVar_1>max1){
      max1=plotVar_1;

  k++;
}
}


void serialEvent(final Serial s) {
  vals = int(splitTokens(s.readString()));
  redraw = true;
}

Thank A TON

1 Like

good it is working, still allow a comment on the used arduino code:

  • a map from 1023 to 300 ( int) is nearly a reduction from 10 bit to 8 bit ( 256) resolution.
    why you do that?
    just send the raw int values to processing, and there can map to float without losing
    resolution.

  • a low baud rate 9600 and a high sample rate ( 50Hz / lines per sec ) looks like a strange combination to me, but yes, wiring problems / bad communication / noise… / can be a reason for this.

1 Like