import processing.serial.*;

Serial myPort;
float sensorValue = 15;

void setup(){
size(640, 360);
noStroke();
background(255, 255, 255);

color red = color(255, 0, 0);
color yellow = color(255, 255, 0);
color green = color(0, 255, 0);


void draw() {
  pushMatrix();
  translate(80, 80);
  if (sensorValue <= 3) {
    fill(red);
    rect(0, 0, 200, 200);
  }
  else {
    if (sensorValue <= 10) {
      fill(yellow);
      rect(10, 10, 200, 200);
    }
    else {
      fill(green);
      rect(20, 20, 200, 200);
    }
  }
  popMatrix();
  translate(150, 0);
  textSize(64);
  println(sensorValue);
  fill(0,0,0);// these are the colors inside
  text(sensorValue + " " + "cm" , 220, height/2);
}

void serialEvent(Serial myPort) { // sketch read the serial data
  String inString = myPort.readStringUntil('\n');
  if (inString != null) {
    inString = trim(inString);
    float[] values = float(split(inString, ","));
    if (values.length >=1) {
      sensorValue = values[0]; //first value in the list
    }
  }
}
