xxxxxxxxxx
120
let rVal = 0;
let pulse = 255;
let left = 0;
let right = 0;
let r = 200;
let customFont;
function preload() {
// Load a font file
customFont = loadFont('Arial.ttf');
}
function setup() {
createCanvas(600, 600, WEBGL);
angleMode(DEGREES); // degrees for easier calculations
colorMode(HSB);
frameRate(30);
stroke(199, 190, 88);
strokeWeight(3);
noFill();
textFont(customFont);
textSize(18);
// Set lights and material properties
ambientLight(200,200,200); // Ambient light to illuminate the scene
pointLight(255, 255, 255, 0, 0, 300); // Point light to illuminate particles from the front
}
function draw() {
rotateX(frameCount % 360);
rotateY(frameCount % 360);
background(0);
orbitControl(4, 4);
for (let i = 0; i < 500; i++) {
let rho = random(50, 200);
let phi = random(0, 180);
let theta = random(0, 360);
let x = rho * sin(phi) * cos(theta);
let y = rho * sin(phi) * sin(theta);
let z = rho * cos(phi);
// Set material properties for particles
ambientMaterial(255);
normalMaterial();
push();
translate(x, y, z);
sphere(5);
pop();
}
// Draw the sphere at a fixed position
push();
translate(0, 0, 0);
noStroke();
fill(255);
sphere(80);
pop();
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
// This function will be called by the web-serial library
// with each new *line* of data. The serial library reads
// the data until the newline and then gives it to us through
// this callback function
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 2) {
// only store values here
// do everything with those values in the main draw loop
rVal = fromArduino[0];
pulse = fromArduino[1];
print(pulse);
}
//////////////////////////////////
//SEND TO ARDUINO HERE (handshake)
//////////////////////////////////
let sendToArduino = left + "," + right + "\n";
writeSerial(sendToArduino);
}
}