For Loop, populate array with data from the loop

Hi I am using an example from Arduino - processing serial communication. I am sending variables from my arduino and want to set the variables in processing from these.

import processing.serial.*;     // import the Processing serial library
Serial myPort;                  // The serial port

float bgcolor;      // Background color
float fgcolor;      // Fill color
float xpos, ypos;         // Starting position of the ball
int[] thing;
int[] array;

int V0;
int V1;
int V2;
int V3;
int V4;
int V5;
int V6;
void setup() {
  size(640, 480);

  // List all the available serial ports
  // if using Processing 2.1 or later, use Serial.printArray()
  println(Serial.list());

  // I know that the first port in the serial list on my Mac is always my
  // Arduino board, so I open Serial.list()[0].
  // Change the 0 to the appropriate number of the serial port that your
  // microcontroller is attached to.
  myPort = new Serial(this, Serial.list()[0], 9600);

  // read bytes into a buffer until you get a linefeed (ASCII 10):
  myPort.bufferUntil('\n');

  // draw with smooth edges:
  smooth();
}

void draw() {
  background(bgcolor);
  fill(fgcolor);
  // Draw the shape
  ellipse(xpos, ypos, 20, 20);
}

// serialEvent method is run automatically by the Processing applet whenever
// the buffer reaches the  byte value set in the bufferUntil()
// method in the setup():

void serialEvent(Serial myPort) {
  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');
  // if you got any bytes other than the linefeed:
  myString = trim(myString);

  // split the string at the commas and convert the sections into integers:
  int sensors[] = int(split(myString, ','));

  // print out the values you got:
  for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
    array[sensorNum] = sensors[sensorNum];
    print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
  }
  // add a linefeed after all the sensor values are printed:
  println();
  // send a byte to ask for more data:
  myPort.write("A");
}

Issue is with this line

    array[sensorNum] = sensors[sensorNum];

it works until i add this and i get this error

Error, disabling serialEvent() for COM9
null

The next piece of code to run after the for loop is to do this

V0 = array[0];
  V1 = array[1];
  V2 = array[2];
  V3 = array[3];
  V4 = array[4];
  V5 = array[5];
  V6 = array[6];
  print("Values Updated: ");
  print(V0);
  print(", ");
  print(V0);
  print(", ");
  print(V1);
  print(", ");
  print(V2);
  print(", ");
  print(V3);
  print(", ");
  print(V4);
  print(", ");
  print(V5);
  print(", ");
  print(V6);
  print(", ");
  println(" ");

Iā€™m pretty new to both arduino and processing, but am pretty sure this should be close to working.

1 Like

Fixed it, Just needed to init the array

// This example code is in the public domain.

import processing.serial.*;     // import the Processing serial library
Serial myPort;                  // The serial port

float bgcolor;      // Background color
float fgcolor;      // Fill color
float xpos, ypos;         // Starting position of the ball
int[] v = {0, 1, 2, 3, 4, 5, 6};

int V0;
int V1;
int V2;
int V3;
int V4;
int V5;
int V6;
void setup() {
  size(640, 480);

  // List all the available serial ports
  // if using Processing 2.1 or later, use Serial.printArray()
  println(Serial.list());

  // I know that the first port in the serial list on my Mac is always my
  // Arduino board, so I open Serial.list()[0].
  // Change the 0 to the appropriate number of the serial port that your
  // microcontroller is attached to.
  myPort = new Serial(this, Serial.list()[0], 9600);

  // read bytes into a buffer until you get a linefeed (ASCII 10):
  myPort.bufferUntil('\n');

  // draw with smooth edges:
  smooth();
}

void draw() {
  background(bgcolor);
  fill(fgcolor);
  // Draw the shape
  ellipse(xpos, ypos, 20, 20);
}

// serialEvent method is run automatically by the Processing applet whenever
// the buffer reaches the  byte value set in the bufferUntil()
// method in the setup():

void serialEvent(Serial myPort) {
  // read the serial buffer:
  String myString = myPort.readStringUntil('\n');
  // if you got any bytes other than the linefeed:
  myString = trim(myString);

  // split the string at the commas and convert the sections into integers:
  int sensors[] = int(split(myString, ','));

  // print out the values you got:
  for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {

    print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
     v[sensorNum] = sensors[sensorNum];
  }
  // add a linefeed after all the sensor values are printed:
  println();
   V0 = v[0];
  V1 = v[1];
  V2 = v[2];
  V3 = v[3];
  V4 = v[4];
  V5 = v[5];
  V6 = v[6];
  print("Values Updated: ");
  print(V0);
  print(", ");
  print(V0);
  print(", ");
  print(V1);
  print(", ");
  print(V2);
  print(", ");
  print(V3);
  print(", ");
  print(V4);
  print(", ");
  print(V5);
  print(", ");
  print(V6);
  print(", ");
  println(" ");
  
  // send a byte to ask for more data:
  myPort.write("A");
}

1 Like

just a idea for the printing part:

int[] v = {0, 1, 2, 3, 4, 5, 6};
/*
int V0;
int V1;
int V2;
int V3;
int V4;
int V5;
int V6;

  println();
  V0 = v[0];
  V1 = v[1];
  V2 = v[2];
  V3 = v[3];
  V4 = v[4];
  V5 = v[5];
  V6 = v[6];
 */
  print("Values Updated: ");
 /*
  print(V0);
  print(", ");
  print(V1);
  print(", ");
  print(V2);
  print(", ");
  print(V3);
  print(", ");
  print(V4);
  print(", ");
  print(V5);
  print(", ");
  print(V6);
  print(", ");
  println(" ");
 */
  for (int i=0;i<=6;i++) { print(v[i]); print(" , "); }