Storing all incoming data and button not working

Hello,

Here is the simplest example of collecting data into an ArrayList of Integers.

Example Code
//This program takes ASCII-encoded strings

//from the serial port at 9600 baud and graphs them. It expects values in the
//range 0 to 1023, followed by a newline, or newline and carriage return

IntList data;

import processing.serial.*;

Serial myPort;                           // The serial port - object from serial class
//String[] list = new String[3];           // For serial port

int inData;

boolean newData = false;
boolean record = false;
boolean dataReady = false;

int counter;

void setup () 
  {
  size(200, 400);                               // Set the window size:   
  background(0);

  data = new IntList();
  
  // List all the available serial ports
  printArray(Serial.list());
  
  String portName = Serial.list()[2];
  myPort = new Serial(this, portName, 9600);    // A serialEvent() is generated when a newline character is received
  myPort.bufferUntil('\n');                     // Sets a specific byte to buffer until before calling serialEvent()
  delay(1000);
 
  textSize(16);
  //noLoop();
  }

void draw () 
  {
  //background(0);
  if(dataReady)
    {
    println(data);
    for(int i=0; i<16; i++)
      {
      text(data.get(i), 10, i*20+20);
      }
    dataReady = false;
    data.clear();
    }
  }

void serialEvent (Serial myPort) 
  {
  // get the ASCII string:
  String inString = myPort.readStringUntil('\n');
  //println(inString);
  if (inString != null) 
    {
    inString = trim(inString);                // trim off whitespaces.
    
    //inByte = float(inString);                 // convert to a number
    //inByte = map(inByte, 0, 1023, 0, height); // map to the screen height.
    //newData = true; 
    
    if (record)
      {
      data.append(int(inString));
      println(counter++);
      if (data.size() >= 16)
        {
        record = false;
        dataReady = true;
        println("Data ready!");
        counter = 0;
        }
      }
    }
  }

void mousePressed()
  {
  record = true;
  background(0);
  }

The incoming data is a string (integer); I converted the string to an integer.
mousePressed() will store 16 integer values and display to screen.

You can easily adapt these concepts with a timer and store data.

There are plenty of examples of timers in this forum.

:)

1 Like