# Save Arduino input in array, and THEN plot it

**URL:** <https://discourse.processing.org/t/save-arduino-input-in-array-and-then-plot-it/11499>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [May 22, 2019, 4:07pm UTC](https://discourse.processing.org/t/save-arduino-input-in-array-and-then-plot-it/11499 "2019-05-22T16:07:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Runzolf](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Runzolf](https://discourse.processing.org/u/Runzolf)\
**Post date:** [May 22, 2019, 4:07pm UTC](https://discourse.processing.org/t/save-arduino-input-in-array-and-then-plot-it/11499/1 "2019-05-22T16:07:17Z")

</div>

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

```auto
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++;
        }
   }
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [May 22, 2019, 4:21pm UTC](https://discourse.processing.org/t/save-arduino-input-in-array-and-then-plot-it/11499/2 "2019-05-22T16:21:05Z")

</div>

> [@Using two different readStringUntil "characters"](https://discourse.processing.org/t/using-two-different-readstringuntil-characters/10769/23):
>
> Now that it seems all’s been sorted out, how about some optimization? The range [-10 to 10] is so small (5-bit) that it comfortably fits in a byte (8-bit). So, rather than send those 2 small values as TSV, w/ lotsa extra separator characters, we can simply send 2 bytes only using Serial.write(): [https://www.Arduino.cc/reference/en/language/functions/communication/serial/write/](https://www.Arduino.cc/reference/en/language/functions/communication/serial/write/) We just need to create a char array of length = 2: signed char xy[2]; #define BAUDS 115200 #define DELAY 10 #define…

---

<div class="post-metadata">

**Author:** ![Runzolf](https://avatars.discourse-cdn.com/v4/letter/r/da6949/32.png) [@Runzolf](https://discourse.processing.org/u/Runzolf)\
**Post date:** [May 22, 2019, 10:39pm UTC](https://discourse.processing.org/t/save-arduino-input-in-array-and-then-plot-it/11499/3 "2019-05-22T22:39:19Z")

</div>

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

```auto
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:

```auto
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

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [May 23, 2019, 2:31am UTC](https://discourse.processing.org/t/save-arduino-input-in-array-and-then-plot-it/11499/4 "2019-05-23T02:31:21Z")

</div>

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.
