xxxxxxxxxx
162
let rVal = 0;
let pulse = 255;
let left = 0;
let right = 0;
let stars = [];
let ellipseVisible = false;
let starsVisible = true;
let keyPressedOnce = false;
let showAdditionalText = false;
let glowFactor = 0;
let mPressed = false;
function setup() {
createCanvas(600, 600);
textSize(18);
createStars();
}
function draw() {
background(0);
fill(255);
textAlign(CENTER, CENTER);
if (!serialActive) {
text("PRESS SPACE TO SELECT SERIAL PORT", width / 2, height / 2);
} else if (!ellipseVisible && !starsVisible) {
text(
"LIGHTLY TOUCH YOUR FINGER TO THE PULSE SENSOR",
width / 2,
height / 2
);
} else if (ellipseVisible || starsVisible) {
drawingContext.shadowBlur = 80;
drawingContext.shadowColor = color(255);
let ellipseSize = map(pulse, 0, 255, 10, 100);
ellipse(width / 2, height / 2, ellipseSize);
}
if (ellipseVisible && !starsVisible) {
text(
"GIVE THE SENSOR TIME TO PICK UP ON YOUR PULSE\nBUT ONCE YOU SEE YOUR HEART BEATING, ACKNOWLEDGE\nTHAT THIS PULSE IS YOU, ALIVE AND BREATHING\n\nPress 'n'",
width / 2,
height - 110
);
} else if (starsVisible && !keyPressedOnce) {
moveStars();
displayStars();
text(
"YOU ARE ALIVE AND BREATHING IN THE UNIVERSE\nLOOK AT AT ALL THE STARS\nBUT SOMETIMES, THE UNIVERSE SEEMS SO BIG\n IT'S EASY TO FEEL SMALL\n\nPress 'm'",
width / 2,
height - 110
);
} else if (starsVisible && !showAdditionalText) {
moveStars();
displayStars();
text(
"BUT THE UNIVERSE IS ALIVE WITH YOU TOO\n\nPress 'v'",
width / 2,
height - 110
);
} else if (showAdditionalText) {
moveStars();
displayStars();
text(
"TELL YOUR FRIENDS YOU LOVE THEM\nREMEMBER TO FEEL YOUR HEARTBEAT WHEN YOU\n LOOK UP AT THE SKY, BECAUSE SOMEDAY\nYOU'LL WISH YOU COULD'VE LIVED IT ALL OVER AGAIN",
width / 2,
height - 110
);
}
}
function createStars() {
for (let i = 0; i < 100; i++) {
let x = random(width);
let y = random(height);
let radius = random(1, 3);
let speedX = random(1, 3) * (random() > 0.5 ? 1 : -1);
let speedY = random(1, 3) * (random() > 0.5 ? 1 : -1);
stars.push({ x, y, radius, speedX, speedY });
}
}
function moveStars() {
for (let star of stars) {
star.x += star.speedX;
star.y += star.speedY;
if (star.x < 0 || star.x > width || star.y < 0 || star.y > height) {
star.x = random(width);
star.y = random(height);
}
}
}
function displayStars() {
noStroke();
for (let star of stars) {
// draw shadow if m is pressed, remove if pressed again
if (mPressed) {
fill(255, 255, 255, 6);
for (i = 0; i < 100; i++) {
ellipse(star.x, star.y, (star.radius * i * 1) / 20);
}
}
// draw star
fill(255);
ellipse(star.x, star.y, star.radius * 2, star.radius * 2);
}
}
function updateStars() {
let rValMapped = map(rVal, 0, 255, -0.1, 0.1);
let pulseMapped = map(pulse, 0, 255, 1, 3);
let pulsingFactor = map(pulse, 0, 255, 0.5, 2);
for (let star of stars) {
star.speedX += rValMapped;
star.speedY += rValMapped;
star.radius = pulseMapped * pulsingFactor;
}
}
function keyPressed() {
if (key == " " && !serialActive) {
setUpSerial();
} else if (key == "n" && ellipseVisible && !starsVisible) {
starsVisible = true;
} else if (key == "m" && starsVisible && !mPressed) {
keyPressedOnce = true;
mPressed = true;
} else if (key == "m" && starsVisible && mPressed) {
mPressed = false;
} else if (key == "v" && starsVisible && keyPressedOnce) {
showAdditionalText = true;
}
}
function readSerial(data) {
if (data != null) {
let fromArduino = split(trim(data), ",");
if (fromArduino.length == 2) {
rVal = fromArduino[0];
pulse = fromArduino[1];
}
let sendToArduino = left + "," + right + "\n";
writeSerial(sendToArduino);
if (pulse > 0 && !ellipseVisible) {
ellipseVisible = true;
}
}
}
function mousePressed() {
if (!starsVisible && ellipseVisible) {
keyPressedOnce = true;
starsVisible = true;
}
}