Code understanding and Printer

Hi :smile:

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();
}

I might be able to help you with printing your screenshot. I use the following method to print a text file to my PC’s default printer:

I Save each line of text I want to print to a file, close the file, then use the following command to launch Notepad and command it to print the file. Once Notepad prints the file it automatically closes so it’s not in the way.
The line of code is:
launch("notepad.exe /p Filepath/Filename.txt";
where: ‘Filepath’ = the file path on your PC and 'Filename.txt is the name of your text file

In your case the filename will likely have the ‘.tif’ extension and you’ll need to find a graphics program that supports the /p (or similar command line function) to cause the graphics program to open your file and send it to the printer.
Good luck.