xxxxxxxxxx
129
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);
}
function draw() {
// one value from Arduino controls the background's red color
// background(0);
// the other value controls the text's transparency value
// fill(255, 255, 255);
// strokeWeight(0);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
// drawingContext.shadowBlur = 80;
// drawingContext.shadowColor = color(255);
// ellipse(width/2, height/2, map(pulse, 0, 255, 10, 100))
rotateX(frameCount%360)
rotateY(frameCount%360)
background(230,50,15);
orbitControl(4,4) // allows rotation + value 4 is for the speed of the rotation or perhaps how much the camera rotates per "mouse swipe"
for(let a1 = 0; a1 < 180; a1 += 5 ){ //a1 = phi
beginShape(POINTS);
for(let a2 = 0; a2<360; a2 += 5){ //a2 = theta
let rtx = 25000
let x = r * (3+1*sin(a2*6)*cos(a1*9))*cos(a1)
let y = r * (3+1*sin(a2*6)*cos(a1*9))*sin(a1) * sin(a2);
let z = r * (3+1*sin(a2*6)*cos(a1*9))*sin(a1) * cos(a2);
fill(255);
vertex(x,y,z)
}
endShape(CLOSE)
}
}
translate(0, 0, 0); // Translate to the center of the canvas
noStroke();
fill(255);
background(10);
sphere(100);
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);
}
}