0

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.

Strohmi
  • 523
  • 5
  • 21

1 Answers1

1

In this example, a javax.swing.Timer periodically adds a new value to the dataset in the timer's ActionListener. You'll want to do it from outside. Here's an outline of how to proceed:

  1. Move dataset and newData up to become instance variables:

    DynamicTimeSeriesCollection dataset;
    float[] newData = new float[1];
    
  2. Add a method that appends your data to the chart's dataset:

    public synchronized void addData(byte[] chunk) {
        for (int i = 0; i < chunk.length; i++) {
            newData[0] = chunk[i];
            dataset.advanceTime();
            dataset.appendData(newData);
        }
    }
    
  3. Invoke the method from serialEvent():

    demo.addChunk(chunk);
    
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thanks for your answer @trashgod. Unfortunately, I do not fully understand it. Where do I need to put the "demo.addChunk(chunk)" and what to do with the "this.series.add(new Millisecond(), this.lastValue);" or the method "actionPerformed(final ActionEvent e)", respectively. Again, thanks for your time. – Strohmi Aug 22 '12 at 21:15
  • a) Item 3 says to call `addChunk()` from `serialEvent()`; b) I cited a different example that uses `advanceTime()`; c) no `Timer` means no `actionPerformed()`. BTW, I don't know what's in your data chunk. – trashgod Aug 23 '12 at 04:21
  • Dear @trashgod, thanks again for your time and help. Unfortunately, I couldn't get back to this issue for a while and now, I could not get it to work. [Here](https://dl.dropbox.com/u/2108381/MyArduinoPlot.java), is the link to my java file which contains all of the code. Would be great if someone got the time and skills to help me out. Thanks for your time. – Strohmi Aug 28 '12 at 15:57
  • What happens when you call `addData()` from `serialEvent()`? – trashgod Aug 28 '12 at 16:14
  • I am not sure where to make this call, @trashgod. I tried various ways but to no avail. Sorry, I am a newbie. I still have the "actionPerformed" in my code. Hope that is ok. Thanks again for helping me out. – Strohmi Aug 28 '12 at 17:06
  • I don't have your hardware, so I can't test/debug your code. You might want to update your question to include the link to your code. – trashgod Aug 28 '12 at 23:17