0

I followed a guide on youtube to make a java program that reads from serial port, which is connected to arduino uno, and display the readings on a jfreechart XYSeries graph. I am trying to display heartbeats using a heartbeat sensor connected to arduino, and the arduino is connected via serial port. I have managed to display the readings on a graph, but the graph keeps track of the readings from the beginning, however, I would like it to only display the last second or something so that the graph looks like real heartbeat readings. Here is the code where I plot the values

        series = new XYSeries("Heart Reading");
        XYSeriesCollection dataset = new XYSeriesCollection(series);
        JFreeChart chart = ChartFactory.createXYLineChart(
                "Your heartbeats", "Time (seconds)", "ADC Reading", dataset);
        window.add(new ChartPanel(chart), BorderLayout.CENTER);

        port = SerialPort.getCommPort("COM5");
        if(port.openPort()){
            System.out.println("Success");
            port.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
            port.setBaudRate(9600);

            Thread thread = new Thread(){
                @Override public void run() {
                    Scanner scanner = new Scanner(port.getInputStream());
                    while(scanner.hasNextLine()) {
                        try {
                            String line = scanner.nextLine();
                            int number = Integer.parseInt(line);
                            series.add(x++, number);
                            window.repaint();
                        } catch(Exception e) {}
                    }
                    scanner.close();
                }
            };
            thread.start();

I know the problem is i'm only adding values without removing them, but how can I remove values if they are that many?

Karim Taha
  • 1,151
  • 3
  • 9
  • 14

1 Answers1

0

Since you want to delete elements of your XYSeries, why not use the delete() method of the XYSeries class?

Once your series is a certain length, you can start delete an element at the beginning for each element you add at the end.

Unless I'm missing something obvious, this seems pretty darn easy.

uint128_t
  • 335
  • 2
  • 10