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.
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.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 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 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.
Step by step
Solved in 3 steps