//graph display data from a sensor
//Made by Gleb Bulygin for FabAcademy
//Based on this tutorial: https://itp.nyu.edu/physcomp/labs/labs-serial-communication/serial-output-from-an-arduino/

import processing.serial.*;
float xPos = 0; // horizontal position of the graph
float yPos = 0; // vertical position of the graph
Serial myPort;

void setup () {
  size(800, 600);                      // window size
  background(#081640);                // background color
  println(Serial.list());            // List all the available serial ports
  myPort = new Serial(this, Serial.list()[0] , 9600); //opens first found port on 9600bps
}

void serialEvent (Serial myPort) {
  int inByte = myPort.read();        // get the byte:
  println(inByte);                  // print it:
  //yPos = height - inByte;        // formula from exemple
  yPos = height - 260 + inByte;   // adjusted graph formula
}

void draw () {
   // draw the line in a pretty color:
   stroke(#A8D9A7);
   line(xPos, height, xPos, yPos);
   // at the edge of the screen, go back to the beginning:
  if (xPos == width) {
    xPos = 0;
    // clear the screen by resetting the background:
    background(#081640);
  } 
  else {
    // increment the horizontal position for the next reading:
    xPos++;
  }
}
 
