I’m writing a program that reads sensor data sent from Arduino and then processes it and turns it into a graph which is the exported as PDF. I originally managed to get the serial communications working between processing and Arduino, but after a few weeks of getting the rest of the project to work, I tried to re-implement the serial communication. I first had an if statement checking if the port was open but that statement was never true so I removed it and just asked processing to read whatever the port says. The null makes sense here i suppose, the port isn’t opening for some reason, but when i open the serial monitor on Arduino IDE it shows everything correct and working.
-Sorry about messy and roundabout code, I keep adding and removing little bits trying to get it to work so it all over the place now.
import processing.pdf.*;
import processing.serial.*;
float delayBetweenRecordings = 0;
float dataPoints = 8;
int totalGraphs = 1;
float maximum = 0;
float minimum = 0;
float data[] = new float[int(dataPoints+1)];
float i = 0;
float graphs = 0;
float buffer = 27;
float xSpacing = (400-buffer)/dataPoints;
float ySpacing = maximum/dataPoints;
float x1;
float x2;
float y1;
float y2;
int arduinoData = 0;
Serial myPort = new Serial(this, Serial.list()[0], 9600);
void setup() {
dataPoints += 1;
size(400, 400);
background(0);
data[0] = 0;
}
void draw() {
if (graphs < totalGraphs) {
graphs++;
beginRecord(PDF, "graph " + int(graphs) +".pdf");
dataCollection();
}
background(51);
gridLines();
drawGraph();
//debugging();
endRecord();
}
void debugging() {
textSize(10);
text(mouseX, 10, 10);
text(mouseY, 10, 20);
text(maximum, 100, 30);
text(minimum, 100, 40);
}
void dataCollection() {
for (int i = 1; i < dataPoints; i++) {
delay(int(delayBetweenRecordings*1000));
arduinoData = myPort.read();
data[i] = float(arduinoData);
println(data[i]);
if (data[i] > maximum-10) {
maximum = data[i]+10;
}
if (data[i] < minimum+10) {
minimum = data[i]-10;
}
}
}
void drawGraph() {
for (int i = 1; i < dataPoints; i++) {
x1 = (i-1)*xSpacing+buffer;
x2 = (i)*xSpacing+buffer;
y1 = height-map(data[i-1], minimum, maximum, 0, height);
y2 = height-map(data[i], minimum, maximum, 0, height);
stroke(0, 255, 0);
strokeWeight(10);
strokeWeight(1);
line(x1, y1, x2, y2);
}
}
void gridLines() {
for (int i = int(minimum/10); i <= (maximum/10); i++) {
textSize(10);
fill(0, 255, 0);
text(i*10, 5, height-map(i*10, minimum, maximum, 0, height));
stroke(100);
line(buffer, height-map((i-1)*10, minimum, maximum, 0, height), width, height-map((i-1)*10, minimum, maximum, 0, height));
}
}