Hello I want to make an Arduino serial data display using processing from several sensors, but for experiments only I use 3 potentiometers. Here’s the Arduino code.
#define pinPot1 0
#define pinPot2 3
#define pinPot3 5
float pot1;
float pot2;
float pot3;
float rpm1;
float rpm2;
float Tinlet;
float Texhaust;
float Tling;
float RH;
float Load;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
pot1 = analogRead (pinPot1);
pot2 = analogRead (pinPot2);
pot3 = analogRead (pinPot3);
rpm1 = ((pot1 * 3) + 1000);
rpm2 = ((pot1 * 6) + 1000);
Tinlet = ((pot2 / 100) + 20);
Texhaust = ((pot2 / 100) + 40);
Tling = ((pot3 / 100) + 15);
RH = ((pot3 / 100) + 50);
Load = (((pot3 / 100) * 0.8) + 10);
Serial.print(rpm1);
Serial.print(",");
Serial.print(rpm2);
Serial.print(",");
Serial.print(Load);
Serial.print(",");
Serial.print(Tinlet);
Serial.print(",");
Serial.print(Texhaust);
Serial.print(",");
Serial.print(Tling);
Serial.print(",");
Serial.print(RH);
Serial.println();
delay (100);
}
But when I try to display the data with the code in processing I encounter a Nullpointerexception error. I’ve tried to learn what a Nullpointerexception is but for its use in my code, I don’t understand. Any suggestions for me?
Here’s the code processing that I made
//Simple Dyno Logger
import processing.serial.*;
import controlP5.*;
Serial serial;
ControlP5 cp5;
int speed = 9600;
String portName;
String filename = "contoh";
Table table;
//Variabel Date
int d = day();
int m = month();
int y = year();
//Variable Time
int h = hour();
int min = minute();
int s = second();
//Variable value from serial
float rpm1;
float rpm2;
float torsi;
float daya;
float load;
float tinlet;
float texhaust;
float temp;
float hum;
//variable panjang lengan dyno
float l = 0.152;
void setup() {
size(600, 400);
cp5 = new ControlP5(this);
cp5.addButton("refresh")
.setPosition(110, 0)
.setSize(50, 30);
cp5.addButton("openPort")
.setPosition(170, 0)
.setSize(50, 30);
cp5.addButton("closePort")
.setPosition(230, 0)
.setSize(60, 30);
cp5.addButton("ledOn")
.setPosition(300, 0)
.setSize(50, 30);
cp5.addButton("ledOff")
.setPosition(360, 0)
.setSize(50, 30);
cp5.addScrollableList("comlist")
.setPosition(0, 0)
.setSize(100, 150)
.setBarHeight(30)
.setItemHeight(30).close();
table = new Table();
table.addColumn("Date", Table.STRING);
table.addColumn("Nomor", Table.INT);
table.addColumn("Time", Table.STRING);
table.addColumn("RPM Roller", Table.FLOAT);
table.addColumn("RPM Engine", Table.FLOAT);
table.addColumn("Torsi (N.m)", Table.FLOAT);
table.addColumn("Daya (HP)", Table.FLOAT);
table.addColumn("Load (Kg)", Table.FLOAT);
table.addColumn("Tinlet (°C)", Table.FLOAT);
table.addColumn("Texhaust (°C)", Table.FLOAT);
table.addColumn("Temp (°C)", Table.FLOAT);
table.addColumn("Humidity (%)", Table.FLOAT);
TableRow newRow = table.addRow();
newRow.setString("Date", str(d) + "/" + str (m) + "/" + str(y));
}
void refresh() {
String list[] = Serial.list();
cp5.get(ScrollableList.class, "comlist").addItems(list);
}
void comlist(int n) {
portName = Serial.list()[n];
}
void openPort() {
serial = new Serial(this, portName, speed);
}
void closePort() {
serial.stop();
}
void ledOn() {
serial.write('n');
}
void ledOff() {
serial.write('f');
}
void draw() {
background(120);
if (serial != null) {
if (serial.available() > 0) {
String val = serial.readStringUntil('\n');
String [] data = split(val, ',');
rpm1 = float(data[0]);
rpm2 = float(data[1]);
load = float(data[2]);
tinlet = float(data[3]);
texhaust = float(data[4]);
temp = float(data[5]);
hum = float(data[6]);
//Data Torsi dan Daya
torsi = (rpm2 * l)/100;
daya = (rpm2 * torsi)/100;
}
TableRow newRow = table.addRow();
newRow.setInt("Nomor", table.getRowCount());
newRow.setString("Time", str(h) + ":" + str(min) + ":" + str(s));
newRow.setFloat("RPM Roller", rpm1);
newRow.setFloat("RPM Engine", rpm2);
newRow.setFloat("Torsi (N.m)", torsi);
newRow.setFloat("Daya (HP)", daya);
newRow.setFloat("Load (Kg)", load);
newRow.setFloat("Tinlet (°C)", tinlet);
newRow.setFloat("Texhaust (°C)", texhaust);
newRow.setFloat("Temp (°C)", temp);
newRow.setFloat("Humidity (%)", hum);
// saveTable(table, "data/new2.csv");
}
//make a rect as a value box
stroke(255);
fill(0, 50, 100);
rect(10, 80, 40, 40, 4);
rect(60, 160, 40, 40, 4);
rect(110, 80, 40, 40, 4);
rect(160, 160, 40, 40, 4);
rect(210, 80, 40, 40, 4);
rect(260, 160, 40, 40, 4);
rect(310, 80, 40, 40, 4);
rect(360, 160, 40, 40, 4);
rect(410, 80, 40, 40, 4);
//make a lable for each value
PFont f = createFont ("Georgia", 15);
textFont (f);
fill(255);
text("Rpm Roll", 0, 70);
text("Rpm Engine", 100, 70);
text("Torsi (N.m)", 200, 70);
text("Daya (HP)", 300, 70);
text("Load (Kg)", 400, 70);
text("Tinlet (°C)", 50, 150);
text("Texhaust (°C)", 150, 150);
text("Temp. (°C)", 250, 150);
text("Humidity (%)", 350, 150);
//write value on rect
int x = 105;
int y = 185;
PFont t = createFont ("arial", 15);
textFont (t);
fill(255);
text(rpm1, 15, x);
text(rpm2, 115, x);
text(torsi, 220, x);
text(daya, 320, x);
text(load, 420, x);
text(tinlet, 70, y);
text(texhaust, 170, y);
text(temp, 270, y);
text(hum, 370, y);
//Write SIMPLE DYNOTEST
stroke(255);
textFont(f);
textSize(60);
fill(255, 255, 255);
text("SIMPLE DYNOTEST", 20, 300);
}