-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData_Plotting.pde
More file actions
66 lines (53 loc) · 1.88 KB
/
Copy pathData_Plotting.pde
File metadata and controls
66 lines (53 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import processing.serial.*;
Serial myPort;
float distance;
ArrayList<Float> distances = new ArrayList<Float>();
void setup()
{
size(800, 600);
// Use the correct serial port for your ESP32
String portName = Serial.list()[0]; // Adjust index based on the available ports [0 for COM4]
myPort = new Serial(this, portName, 115200);
background(255);
stroke(0);
noFill();
textSize(24); // Set the text size
fill(0); // Set the text color to black
}
void draw()
{
while (myPort.available() > 0)
{
String inString = myPort.readStringUntil('\n');
if (inString != null)
{
inString = trim(inString);
distance = float(inString);
// Clip the distance to be within 100 cm
distance = constrain(distance, 0, 100);
distances.add(distance);
if (distances.size() > width) { // Limit the number of points to the width of the window
distances.remove(0);
}
background(255); // Clear the background
// Scale and plot the graph as a line
float scaleX = (float)width / distances.size();
float scaleY = (float)height / 100; // Scale for 100 cm max
if (distances.size() > 1)
{
// Draw lines between consecutive points
for (int i = 0; i < distances.size() - 1; i++) {
float x1 = i * scaleX;
float y1 = height - distances.get(i) * scaleY;
float x2 = (i + 1) * scaleX;
float y2 = height - distances.get(i + 1) * scaleY;
line(x1, y1, x2, y2);
}
}
// Display the current distance measured in the top-right corner
String distanceText = String.format("Distance: %.3f cm", distance); // %.3f for distance upto 3 decimal places
float textWidth = textWidth(distanceText);
text(distanceText, width - textWidth - 10, 30); // Position text in the top-right corner
}
}
}