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 sensorValues; public GraphPanel(LinkedList 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 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. The photo i attatched is the only graph i see with no values.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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. The photo i attatched is the only graph i see with no values. 
 
 
Carbon Sensor Graph
Transcribed Image Text:Carbon Sensor Graph
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY