
// Interface between Processing and ATTiny3216
// Jans Hendry @2022

import processing.serial.*; // lib for serial communicatiokn

Serial ser; // create object for serial
PImage img; // create object for image
int dt; // buffer variable
PFont f; // set font object


void setup() {
  size(600, 400); // in pixels
  //background(152, 190, 100); // background color
  background(0); // background color
  f = createFont("Arial", 30, true);
  
  // load fablab image
  img = loadImage("R.jpg");
  image(img, 0, 0, width+5, height/3);
  
  // default conditiokn
  def_draw();
  
  noStroke();
  frameRate(1);
  ser = new Serial(this, "COM20", 9600); // Get com from device manager or arduino IDE
 
  delay(1000);
}

void draw() {
  
  if (0 < ser.available()){      
    dt = ser.read();
  }

    // measurement value   
    textFont(f);
    textSize(30);
    fill(255);
    text(" ", 280, 200);
    text(dt, 280, 200);
    
  if (dt >= 35){
    fill(255, 0, 0);
    circle(100, 350, 80); // red 

    fill(255);
    circle(300, 350, 80); // yellow
    
    fill(255);
    circle(500, 350, 80); // green 
  }
  else if (dt >= 30){
    fill(255, 242, 0);
    circle(300, 350, 80); // yellow

    fill(255);
    circle(100, 350, 80); // red 
    
    fill(255);
    circle(500, 350, 80); // green 
  }
  else{
    fill(0, 255, 0);
    circle(500, 350, 80); // green 
    
    fill(255);
    circle(100, 350, 80); // red 
    
    fill(255);
    circle(300, 350, 80); // yellow
  }
  println(dt);
  //delay(1000); // take a breath
}

void def_draw(){
    // caption for real measurement
    textFont(f);
    textSize(30);
    fill(255);
    text("TEMPERATURE:", 10, 200);
    
    // caption for input
    textSize(32);
    fill(185, 122, 87);
    text("SENSOR", 240, 160);
  
    // caption for output/indicator
    textSize(32);
    fill(0, 162, 232);
    text("INDICATOR", 230, 300);
    
    // make 3 circles
    fill(255);
    circle(100, 350, 80); // red 
    
    fill(255);
    circle(300, 350, 80); // yellow
    
    fill(255);
    circle(500, 350, 80); // green 
}
