xxxxxxxxxx
128
let fVal = 0;
let micVal = 0;
let minRadius = 200;
let maxRadius = 350;
let circles = [];
let circleN = 9;
let minSpeed = 1;
let maxSpeed = 5;
let speedFactor = 1;
let baseline = 535;
function setup() {
createCanvas(1440, 780);
textSize(18);
randomize(circleN);
}
function draw() {
background(255)
if (!serialActive) {
text("Press Space Bar to select Serial Port", width/2-150, height/2);
} else {
fill(0);
// text("Connected", 20, 30);
//text("Mic", 20, 50);
//text(micVal, 100, 50);
//text("SpeedFactor", 20, 90);
//text(speedFactor, 150, 90);
if(micVal-baseline>10){
speedFactor = 1+abs(micVal-baseline)/20;
}else{
if(speedFactor>1){
speedFactor = speedFactor - speedFactor*0.01;
}
}
for (let i = 0; i < circles.length; i++) {
circles[i].display();
}
for (let i = 0; i< circles.length; i++) {
circles[i].update();
circles[i].display();
// Remove bubbles that are off-screen
if (circles[i].y < -maxRadius) {
circles.splice(i, 1);
}
}
if (frameCount % 90 === 0) {
randomize(1);
}
}
}
class myCircle {
constructor(x, y, size, speed, f) {
this.x = x;
this.y = y;
this.size = size;
this.speed = speed;
this.f = f;
this.redVal = random(200, 255);
this.blueVal = random(200, 255);
}
update() {
this.y -= this.speed*speedFactor;
}
display() {
let radius = map(fVal, 0, 1023, 0, this.size);
let colorR = map(fVal, 0, 1023, 0, 255);
let colorB = map(fVal, 0, 1023, 255, 0);
let colorG = 0;
for (let i = 0; i <= radius; i=i+2) {
noStroke();
fill(color(colorR, colorG, colorB, 2));
ellipse(this.x, this.y, i, i*this.f);
}
}
}
function randomize(N){
for (let i = 0; i < N; i++) {
let x = random(0, width);
let y = random(minRadius, height);
let r = random(minRadius, maxRadius);
if(N==1){
y = height + r;
}
let s = random(minSpeed, maxSpeed)/4;
let f = random(90, 110)/100;
circles.push(new myCircle(x, y, r, s, f));
}
}
function keyPressed() {
if (key == " ") {
setUpSerial();
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 2) {
micVal = fromArduino[0];
fVal = fromArduino[1];
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = "0,0\n";
writeSerial(sendToArduino);
}
}