Hello everyone. I have a code which receives a temperature and humidity value through the serial and I’m trying to use the temperature value to make a working liquid thermometer. As of right now, I managed to make it change level when a new value is received, but it’s not consistent: an increase in temperature produces an increase in the level, but sometimes produces a decrease in the level as well. I also have trouble keeping the level the same when the temperature value doesn’t change.
I want to know what’s wrong with my code and how can I be able to fix it. Thank you in advance.
import processing.serial.*;
Serial myport;
String h;
String t;
float T;
float PrevT;
float w;
float W;
float PrevW;
int lf = 10;
void setup() {
size(1920, 1080);
String portname = Serial.list()[1];
myport = new Serial(this, portname, 9600);
frameRate(10);
rectMode(CENTER);
noStroke(); //thermometer shapes
fill(255);
rect(1460, 565, 150, 400);
ellipse(1460, 360, 150, 150);
ellipse(1460, 840, 280, 280);
stroke(0);
rect(960, 100 , 1000, 160); //title box
rect(270, 400, 400, 200); //tempis box
rect(270, 650, 400, 200); //humis box
rect(720, 400, 400, 200); //temp value box
rect(720, 650, 400, 200); //hum value box
String s = "BT DHT11 Temperature Sensor";
fill(0);
textSize(50);
text(s, 600, 120);
textSize(25);
text("Temperature is", 180, 400);
text("Humidity is", 200, 650);
}
void draw() {
t = myport.readStringUntil(lf);
if (t != null) {
t = t.substring(1, t.length() - 4);
println(String.format("%s~", t));
fill(255);
stroke(0);
rect(720, 400, 400, 200);
fill(0);
text(t+"°C", 700, 400);
noStroke();
fill(255);
rect(1460, 565, 150, 400);
fill(255,0,0);
ellipse(1460, 840, 260, 260);
fill(0);
text(t+"°C", 1400, 900);
T = float(t);
noStroke();
fill(255,0,0);
rect(1460, 700, 100, PrevW+150);
if (T != PrevT) {
w = T - PrevT;
W = 1/w;
noStroke();
fill(255);
rect(1460, 565, 150, 400);
fill(255,0,0);
ellipse(1460, 840, 260, 260);
fill(0);
text(t+"°C", 1400, 900);
fill(255,0,0);
rect(1460, 700, 100, 150+W); }
}
h = myport.readStringUntil(lf);
if (h != null) {
h = h.substring(1, h.length() - 1);
println(String.format("%s~", h));
fill(255);
stroke(0);
rect(720, 650, 400, 200);
fill(0);
text(h, 700, 650); }
PrevT = T;
PrevW = W;}