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?