import processing.serial.*;

Serial myPort;

float weight = 0;
int funnel = 0;
String status = "Waiting...";

void setup() {
  size(500, 300);
  textAlign(CENTER, CENTER);
  textSize(24);

  println(Serial.list());
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.bufferUntil('\n');
}

void draw() {
  background(30);

  fill(255);
  text("Spice Dispenser Monitor", width/2, 40);

  textSize(32);
  text("Weight: " + weight + " g", width/2, 120);

  textSize(24);
  text("Funnel: " + funnel, width/2, 180);

  textSize(14);
  text(status, width/2, 250);
}

void serialEvent(Serial p) {
  String line = trim(p.readStringUntil('\n'));
  if (line == null) return;

  if (line.startsWith("Weight:")) {
    weight = float(trim(line.substring(7)));
  } 
  else if (line.startsWith("Indexing to funnel:")) {
    funnel = int(trim(line.substring(20)));
  } 
  else {
    status = line;
  }
}
