UPDATE: The code I am using can be found here: https://dl.dropbox.com/u/2108381/MyArduinoPlot.java I hope this helps to understand my challenge. Thanks in advance for your time.
I would like to read sensor values from an Arduino and chart them using the Java library JFreeChart.
I found some code on the internet (see below) and now, I would like to combine the code for plotting a dynamic line chart and the code for reading in the Arduino values. Both codes do work separately, but I got stuck in combining them both.
The code for plotting a dynamic line chart is from here (plots random data): http://dirtyhandsphp.blogspot.in/2012/07/how-to-draw-dynamic-line-or-timeseries.html
The code to read in Arduino values in Java is from here: http://arduino.cc/playground/Interfacing/Java
I assume (newbie in Java, though) that the relevant part is here:
/**
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
int available = input.available();
byte chunk[] = new byte[available];
input.read(chunk, 0, available);
// Displayed results are codepage dependent
System.out.print(new String(chunk));
// the code I tried
// String MyValue = new String(chunk);
// Double Value = Double.valueOf(MyValue);
} catch (Exception e) {
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other ones.
}
and here:
public void actionPerformed(final ActionEvent e) {
// Original Code
final double factor = 0.9 + 0.2*Math.random();
this.lastValue = this.lastValue * factor;
final Millisecond now = new Millisecond();
this.series.add(new Millisecond(), this.lastValue);
System.out.println("Current Time in Milliseconds = " + now.toString()+", Current Value : "+this.lastValue);
// my code
// this.series.add(new Millisecond(), Value);
}
How can I make the public synchronized void serialEvent
return the sensor value and how can I add it to the this.series.add
part?
I am a newbie in Java.
Any direct help or linkage to other websites/posts is greatly appreciated. Thanks for your time.