// Etch-a-Sketch
// based on a sketch by Trevor Shannon
import processing.serial.*;
Serial port;
String serialInterface = "COM5";
int lastX = -1;
int lastY = -1;
void setup() {
size(1024, 920);
background(205);
port = new Serial(this, serialInterface, 9600);
}
void handleData(int x, int y) {
if (lastX >= 0 && lastY >= 0) {
line(x, y, lastX, lastY);
}
lastX = x;
lastY = y;
}
void draw() {
readSerial();
}
void mouseClicked() {
saveFrame();
// added to save the drawing
}
void readSerial() {
int x; int y; int z;
String s;
while ((s = port.readStringUntil('\n')) != null) {
String[] parts = s.substring(0, s.length()-2).split(",");
if (parts.length == 3) {
x = int(parts[0]);
y = int(parts[1]);
z = int(parts[2]);
if (z==0) {
background(205);//clears the screen when shaken
}
handleData(x, y);
}
}
}