I'm working on a project where I need to show the tempetures of 2 DS18B20's on individual bar graphs.
So far I'v been able to send the 2 tempetures from my Arduino to Processing through
through serial communication, seperate the 2 values, and "kind of" display them as bar graphs... It shows the Bar graphs for a second, and then they disappear for a second.
Is this caused by the serialEvent method ? what other approach is there?
Arduino Code:
#include <OneWire.h>
#include <DallasTemperature.h>
OneWire oneWire(10); // port 10
DallasTemperature sensors(&oneWire); // Pass our oneWire reference to Dallas Temperature.
DeviceAddress sensorA, sensorB; // arrays to hold device addresses
void setup(void)
{
Serial.begin(115200); // start serial port
sensors.begin(); // Start up the library
}
void loop(void)
{
sensors.requestTemperatures();
Serial.print(sensors.getTempCByIndex(0)); //Send SensorA data to computer
Serial.print(","); // Seperater...
Serial.print(sensors.getTempCByIndex(1)); //Send SensorB data to computer
Serial.println();
}
Processing Code:
import processing.serial.*;
Serial myPort; // Create object from Serial class
void setup() {
size(800, 600);
myPort = new Serial(this, "COM4", 115200);
myPort.bufferUntil('\n'); // read it and store it in val
}
void draw(){
background(255);
fill(0, 0, 255);
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
myString = trim(myString);
// split the string at the commas
// and convert the sections into Fahrenheit float values:
float sensors[] = float(split(myString,','));
float SensorB_f = ((sensors[0]*9)/5) + 32;
float SensorA_f = ((sensors[1]*9)/5) + 32;
//Draw Bar Graph --- BROKEN!
rect(200, 500,20, -SensorA_f / 1.5);
rect(600, 500,20, -SensorB_f / 1.5);
// print out the values you got:
print("SensorA " + SensorA_f);
print(" , ");
print("SensorB " + SensorB_f);
// add a linefeed after all the sensor values are printed:
println();
}