// 1. Import library and create object variable
//
import processing.serial.*;     // import the Processing serial library
Serial myPort;                  // create a serial port named 'myPort'
//


// 2. Set up the serial hardware in the setup() function. I usually put it right after the size function

//
  println(Serial.list());   // List all the available hardware serial ports

// associate the hardware serial port with the software port we creted above
  // On most macOS machines, Port 1 in the serial list is
  // typically the Arduino, so we open Serial.list()[1].
  // On Windows machines, this generally opens COM3.
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[1], 9600);

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


// 3. Place code for Serial Event after, not inside, the loop() function 

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

// for 1 variable called: val
//
void serialEvent(Serial myPort) {
  // read the serial buffer
  String myString = myPort.readStringUntil('\n');   // read data on USB port up to the linefeed (ASCII 10)
  if (myString != null) {           // if there was data on the USB port
    myString = trim(myString);      // remove any non-alpha-numerical bytes
    val = int(myString);            // turns ASCII into an integer
  }
  //println(val, myPreviousMovie);
}
//


// for 3 variables called: val1, val2, val3
//<
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
  // Note: the data isn't an integer; it's ASCII which is a
  // numerical representation of an alphabetic representation of an integer
  int sensors[] = int(split(myString, ','));

  // print out the values if you wish - useful for debugging
  for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
    //print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
  }
  // add a linefeed after all the sensor values are printed
  //println();
  
  if (sensors.length > 1) {   // if more than 2 pieces of data are sent
  // read the data from the 3 array elements, map them, make sure they're
  // integers and assign them to variables
    val1 = int(map(sensors[0], 0, 255, 0, width));
    val2 = int(map(sensors[1], 0, 255, 0, height));
    val3 = int(map(sensors[2], 0, 255, 0, 255));
  }
}
//


// 4. Declare global variables for the data returned by the Serial Event.
//    Place this at the top of the code where you declare variables, generally after 
//    you import libraries and instantiate objects, right before setup().

//
int val;
//

//
float val1, val2, val3;       // Position of the ball
//


// 5. rename variables and scale as needed to match the data