Hi everyone,
I’m trying to integrate tabs into my code and I want to read a few waterproof temp sensors. I want to show the temperature as a sliding scale bar and I think I’m close, I have a DS18B20 in Pin 2 on my Arduino uno. When I run my sketch the box that I want shows up, however I’m not getting temperature. I ran an Arduino code that verifies the thermometer is working, so I’m assuming the error in my code has to do with getting the data from the sensor. I’m still learning so I’m not entirely sure what I need to add to get a connection to the sensor. Thanks!
Here is my sketch:
import processing.serial.*;
import cc.arduino.*;
import org.firmata.*;
Serial myPort;
PFont font;
Temp temp1;
Temp temp2;
Arduino arduino;
float temp = 0;
public void setup() {
size(800,400);
arduino = new Arduino(this, "COM3", 57600);
arduino.pinMode(2, Arduino.OUTPUT);
arduino.pinMode(7, Arduino.OUTPUT);
font = createFont("font", 16);
textFont(font);
temp1 = new Temp(200, 0, 100, 100, "Temp C");
temp2 = new Temp(200, 0, 100, 100, "Temp C");
}
public void draw() {
background(#310FF7);
temp1.displayTemp();
temp1.setSensorValue(temp);
temp2.displayTemp();
temp2.setSensorValue(temp);
}
public void serialEvent(Serial myPort) {
String myString = myPort.readStringUntil('\n');
if(myString != null){
myString = trim(myString);
int sensorData[] = int(split(myString, ','));
temp = sensorData[0];
}
}
and this is my class for temp:
class Temp {
float sensorValue;
float threshold;
String text;
int x;
int y;
int d;
Temp(int x, int y, int d, float threshold, String text) {
this.x = x;
this.y = y;
this.d = d;
this.threshold = threshold;
this.text = text;
}
void setSensorValue(float sensorValue) {
this.sensorValue = sensorValue;
}
void displayTemp() {
noStroke();
fill(255, 255, 255);
rect(this.x, this.y, this.d, 100);
float mappedSensorValue = map(sensorValue, -20, threshold, 0, 100);
//noStroke();
rect(this.x, 200-mappedSensorValue, this.d, 100);
if(this.sensorValue >= 0 && this.sensorValue < (this.threshold * 0.25)) {
fill(#11F5C9);
} else if(this.sensorValue >= (this.threshold * 0.25) && this.sensorValue < (0.50 * this.threshold)) {
fill(#23F511);
} else if(this.sensorValue >= (this.threshold * 0.50) && this.sensorValue < (0.75 * this.threshold)) {
fill(#F1FF36);
} else {
fill(#F51130);
}
textSize(35);
text(int(sensorValue), 305, 200);
textSize(25);
text(char(176) + text, 365, 200);
}
}