Code for my Arduino:
#include<EngduinoThermistor.h>
void setup()
{
Serial.begin(9600);
EngduinoThermistor.begin();
}void loop()
{
float temp;
temp = EngduinoThermistor.temperature();
Serial.println(temp);
delay(1000);
}
Code for my Processing:
import processing.serial.*;
Serial port;
float x = 0;
void setup() {
size(500, 400);
println(Serial.list());
String portName = Serial.list()[0];
port = new Serial(this, "/dev/tty.usbmodem1411", 9600);
}
void draw() {
}
void serialEvent(Serial port) {
float inByte = port.read();
println(inByte);
stroke(90, 76, 99);
line(x, height, x, height - inByte);
if (x >=width) {
x=0;
background(200);
}
x++;
}
I have tried really hard to understand processing but I still don't understand how to draw a graph based on the data sent by the arduino. I think the problem that I have mostly is the line part. I don't know how to draw a line connecting the previous point and the new point.
But overall the problem does not even work.... Where is the problem :( ?
It's hard to answer general "how do I do this" type questions. Stack Overflow is designed for more specific "I tried X, expected Y, but got Z instead" type questions. That being said, I'll try to help in a general sense:
Break your problem down into smaller steps.
Forget about Arduino for a second. Can you create a small example sketch that shows a graph based on some hard-coded data? Get that working first.
When you get that working, then try basing your graph on something other than hard-coded values. Maybe try random values, or values based on the mouse position, etc. The point is not to connect your Arduino yet, but to get a dynamic graph working.
Separate from that, get another basic example sketch that ignores the graphing and just connects to the Arduino. Print the values you're getting from the Arduino to the console.
Finally, when you have all of those working separately, then it'll be easier to connect them to create a graph based on data from the Arduino.
If you get stuck on one of the steps, you can post an MCVE along with a specific question. Good luck.
According to the Engduino Thermistor Documentation (pdf link), the value is a float.
Because you're sending the value using println()
, there is a new line character ('\n'
) sent along with the floating point value. This can actually be useful in conjuction with Processing Serial's bufferUntil() function.
The main missing ingredient is actually parsing a float
from the received String.
Here's a basic conversion example:
String temperatureString = "37.5";
float temperatureFloat = float(temperatureString);
You can use Serial's readString() to get the string, then trim() it to remove whitespace, then finally convert it to a float:
temperature = float(port.readString().trim());
It's also a good idea to check if the conversion was successful:
if(!Float.isNaN(temperature)){
println("read temperature",temperature);
x++;
}
Overall it's a good idea to check for errors, so here's a version that does the above and checks the serial ports as well and comments:
import processing.serial.*;
//serial port
Serial port;
//x position of current value on graph
float x = 0;
//current temperature reading
float temperature;
void setup() {
size(500, 400);
background(200);
String[] portNames = Serial.list();
println(portNames);
String portName = "not found";
//loop through available serial ports and look for an Arduino (on OSX something like /dev/tty.usbmodem1411)
for(int i = 0 ; i < portNames.length; i++){
if(portNames[i].contains("tty.usbmodem")){
portName = portNames[i];
break;
}
}
//try to open the serial port
try{
port = new Serial(this, portName, 9600);
//buffer until new line character (since values are send via println() from Arduino)
port.bufferUntil('\n');
}catch(Exception e){
System.err.println("Arduino port " + portName);
e.printStackTrace();
}
}
void draw() {
//draw graph
stroke(90, 76, 99);
//you might want to map the temperature to sketch dimensions)
line(x, height, x, height - temperature);
if (x >=width) {
x=0;
background(200);
}
}
void serialEvent(Serial port) {
try{
//read the string, trim it (in case there's a '\n' character), then convert it to a float
temperature = float(port.readString().trim());
//check if the float conversion was successfull (didn't get a NaN value)
if(!Float.isNaN(temperature)){
println("read temperature",temperature);
x++;
}
}catch(Exception e){
System.err.println("error parsing value from serial port");
e.printStackTrace();
}
}