Save Arduino input in array, and THEN plot it

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