In this code the graph shows up, however it does not outprint a proper live graph WITH carbonsensor values from live reading. Please fix the code so i may see a proper graph with values.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

import org.firmata4j.Pin;
import org.firmata4j.ssd1306.SSD1306;

import java.util.TimerTask;

class task extends TimerTask {

private final Pin carbonSensor;
private final SSD1306 oledScreen;
private boolean lastCarbonState = false; // Track previous carbon state

task(Pin sensor, SSD1306 screen) {
this.carbonSensor = sensor;
this.oledScreen = screen;
}

@Override
public void run() {
int sensorValue = (int) carbonSensor.getValue(); // Get the sensor value
boolean currentCarbonState = sensorValue < 5000; // Change to less than 300

System.out.println("Sensor Value: " + sensorValue); // Print sensor value

if (currentCarbonState != lastCarbonState) {
oledScreen.getCanvas().clear();

if (sensorValue<500) {
try {
oledScreen.getCanvas().drawString(0, 15, "THERE IS A FIRE DETECTED");
oledScreen.getCanvas().drawString(0, 26, "YOU MUST EVACUATE BUILDING");
System.out.println("Individual in danger");
} catch (Exception e) {
throw new RuntimeException(e);
}
} if (sensorValue>500) {
oledScreen.getCanvas().drawString(0, 15, "THERE IS NO FIRE DETECTED");
oledScreen.getCanvas().drawString(0, 26, "YOU ARE SAFE");
System.out.println("Individual is safe");
}

// Simulate graph-like update
oledScreen.getCanvas().drawString(0, 38, "Sensor Value: " + sensorValue);
oledScreen.display();

lastCarbonState = currentCarbonState;
---

import org.firmata4j.IODevice;
import org.firmata4j.Pin;
import org.firmata4j.firmata.FirmataDevice;
import org.firmata4j.ssd1306.SSD1306;
import java.util.Timer;

import java.io.IOException;
import java.util.HashMap;
import java.util.TimerTask;


class major{
public static void main(String[] args) throws IOException,InterruptedException
{

String portID = "/dev/cu.usbserial-0001";
IODevice arduinoObject = new FirmataDevice(portID);
arduinoObject.start();
arduinoObject.ensureInitializationIsDone();
var i2cObject = arduinoObject.getI2CDevice((byte) 0x3C);
SSD1306 OLED = new SSD1306(i2cObject, SSD1306.Size.SSD1306_128_64);
OLED.init();
var carbonsensor = arduinoObject.getPin(14);
carbonsensor.setMode(Pin.Mode.ANALOG);



var Task = new task(carbonsensor, OLED);

Timer timerObject = new Timer();
timerObject.schedule(Task, 0, 1000);
---
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;

public class GraphPanel extends JPanel {
private LinkedList<Integer> sensorValues;

public GraphPanel(LinkedList<Integer> sensorValues) {
this.sensorValues = sensorValues;
}

@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int xOffset = 50;
int yOffset = 50;
int xSpacing = (getWidth() - 2 * xOffset) / sensorValues.size();

g.setColor(Color.BLACK);
g.drawLine(xOffset, getHeight() - yOffset, getWidth() - xOffset, getHeight() - yOffset); // X-axis
g.drawLine(xOffset, getHeight() - yOffset, xOffset, yOffset); // Y-axis

g.setColor(Color.BLUE);
int x = xOffset;
for (int i = 0; i < sensorValues.size(); i++) {
int value = sensorValues.get(i);
int y = getHeight() - yOffset - (value / 10); // Scale for display
g.fillRect(x, y, xSpacing, getHeight() - 2 * yOffset - y);
x += xSpacing;
----
import javax.swing.*;
import java.awt.*;
import java.util.LinkedList;

public class GraphDisplay {
private LinkedList<Integer> sensorValues = new LinkedList<>();
private final int MAX_VALUES = 100; // Maximum number of values to keep
private GraphPanel graphPanel;

public GraphDisplay() {
JFrame frame = new JFrame("Carbon Sensor Graph");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(800, 600);

graphPanel = new GraphPanel(sensorValues);
frame.add(graphPanel);

frame.setVisible(true);
}

public void updateGraph(int value) {
sensorValues.add(value);
if (sensorValues.size() > MAX_VALUES) {
sensorValues.removeFirst();
}
graphPanel.repaint(); // Add this line
}

public static void main(String[] args) {
SwingUtilities.invokeLater(GraphDisplay::new);
In this code the graph shows up, however it does not outprint a proper live graph WITH carbonsensor values from live reading. Please fix the code so i may see a proper graph with values. 
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Polynomial time
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education