Hi
I found a code that I really love but I’m having troubles understanding it. Can anyone help me understand each line? And another question: I added a screenshot to the code and want this screenshot to be connected to a printer, which means the moment you click S it prints the screenshot.
Here is the Code:
import processing.serial.*;
Serial heartbeat;
String val = “0.0”; // Wert von Arduino
float bg = 20;
float s = 255;
float xoff = 0;
float yoff = 1000;
float fval = 0;
float fvalBuffer;
float u(float n) {
return width/100 * n; //50
}
void setup() {
String portName = Serial.list()[7];
heartbeat = new Serial(this, portName,9600);
size(1000,1000);
//pixelDensity(displayDensity());
background(bg);
strokeWeight(3);
stroke(s);
smooth();
}
void draw() {
//arduino
if(heartbeat.available() >0)
{
val=heartbeat.readStringUntil(’\n’);
if(val != null && val != “”){
fval = float(val);
//langsamer
if(fval > 1){fval = 0.5;}
if(fval > fvalBuffer){
fvalBuffer = fval;
fval = fval * 8;
}
if(fval < fvalBuffer){
fvalBuffer -= 0.005f;
fval = fvalBuffer * 8;
}
println(fval);
}
}
background(bg);
for(float y = height0.3; y < height0.9; y += u(1.5)) { //height0.1
pushMatrix();
translate(20, y);
noFill();
beginShape();
for(float x = width0.1; x < width0.9; x++) {
float ypos = map(noise(x/100 + xoff, y/30 + yoff), 0, 1, -100, 100);
float magnitude = x < width0.5 ? map(x, width0.1, width0.5, 0, 1) : map(x, width0.5, width0.9, 1, 0) ;
ypos *= magnitude *fval;
if(ypos > 0) ypos = 0;
vertex(x, ypos);
}
endShape();
popMatrix();
}
xoff += 0.01;
yoff += -0.01;
}
void keyPressed(){
if (key==‘s’||key==‘S’)
saveFrame();
}