// Use the serial library. Because we want to interact with a serial port.
import processing.serial.*;
// Our serial device is call heartbeat.
Serial heartbeat;
// We have a string for some value.
String val = "0.0"; // Wert von Arduino
// there are othervariables too.
float bg = 20;
float s = 255;
float xoff = 0;
float yoff = 1000;
float fval = 0;
float fvalBuffer;
// There's a function, u(), that gives us an x position that's n% of the way across the screen.
float u(float n) {
return width/100 * n; //50
}
// Here's setup. This happens once.
void setup() {
// We find the right port we want from the list of ports.
String portName = Serial.list()[7];
// And set out serial thing to use that portname.
heartbeat = new Serial(this, portName, 9600);
// Here's size. This sets the sketch size. This really should be the first thing in setup().
size(1000, 1000);
//pixelDensity(displayDensity()); // comments don't do anything
// We draw a dark background.
background(bg);
// We set the properties for lines we draw.
strokeWeight(3);
stroke(s);
// We make the sketch not look aweful.
smooth();
}
// Here's draw()! This runs over and over again to redraw our scene.
void draw() {
//arduino
// If we can read from our serial port,
if (heartbeat.available() >0)
{
// Read a value.
val=heartbeat.readStringUntil('\n');
// If we got a value,
if (val != null && val != "") {
// Make that value be of the float type.
fval = float(val);
//langsamer
// If it's more than 1, make it a half.
if (fval > 1) {
fval = 0.5;
}
// Do something with a couple of variables if they have one relation.
if (fval > fvalBuffer) {
fvalBuffer = fval;
fval = fval * 8;
}
// Do something else to them if the have a different relation.
if (fval < fvalBuffer) {
fvalBuffer -= 0.005f;
fval = fvalBuffer * 8;
}
// Print out the value we read.
println(fval);
}
}
// Dark background again. Draw it. Yeah.
background(bg);
// Do a loop,
for (float y = height*0.3; y < height*0.9; y += u(1.5)) { //height*0.1
pushMatrix(); // Remember this coordinate system.
translate(20, y); // Adjust where (0,0) is.
noFill(); // Draw shapes without any fill.
beginShape(); // Start drawing a shape.
// Do a second loop.
for (float x = width*0.1; x < width*0.9; x++) {
// Get a value that's based on random noise.
float ypos = map(noise(x/100 + xoff, y/30 + yoff), 0, 1, -100, 100);
// Work out a magnitude.
float magnitude = x < width*0.5 ? map(x, width*0.1, width*0.5, 0, 1) : map(x, width*0.5, width*0.9, 1, 0) ;
// Work out the y position.
ypos *= magnitude *fval;
// Make sure it's 0 or negative.
if (ypos > 0) ypos = 0;
// Put a vertex there. You're drawing a shape, remember?
vertex(x, ypos);
} // Stop that second loop.
endShape(); // Stop drawing that shape.
popMatrix(); // Put space back how you remembered it.
} // Stop doing that first loop.
// Move these offsets a bit.
xoff += 0.01;
yoff += -0.01;
} // The end of draw(). Drawing's all done, yo.
// If a key is pressed,
void keyPressed() {
// And it's the s key,
if (key=='s'||key=='S')
// Save what's drawn to a file.
saveFrame();
} // Don't do anything else when a key is pressed.
See comments explaining code, above.