xxxxxxxxxx
115
var arr = [40, 80, 20, 160];
var flag = 0;
var rot = 0;
var scl = 0;
var mapData;
let serial; // variable for the serial object
let latestData = "waiting for data"; // variable to hold the data
function setup() {
createCanvas(windowWidth, windowHeight, WEBGL);
//background(0);
frameRate(30);
// serial constructor
serial = new p5.SerialPort();
// get a list of all connected serial devices
serial.list();
// serial port to use - you'll need to change this
serial.open('COM6');
// callback for when the sketchs connects to the server
serial.on('connected', serverConnected);
// callback to print the list of serial devices
serial.on('list', gotList);
// what to do when we get serial data
serial.on('data', gotData);
// what to do when there's an error
serial.on('error', gotError);
// when to do when the serial port opens
serial.on('open', gotOpen);
// what to do when the port closes
serial.on('close', gotClose);
}
function serverConnected() {
console.log("Connected to Server");
}
// list the ports
function gotList(thelist) {
console.log("List of Serial Ports:");
for (let i = 0; i < thelist.length; i++) {
console.log(i + " " + thelist[i]);
}
}
function gotOpen() {
console.log("Serial Port is Open");
}
function gotClose() {
console.log("Serial Port is Closed");
latestData = "Serial Port is Closed";
}
function gotError(theerror) {
console.log(theerror);
}
// when data is received in the serial buffer
function gotData() {
let currentString = serial.readLine(); // store the data in a variable
trim(currentString); // get rid of whitespace
if (!currentString) return; // if there's nothing in there, ignore it
console.log(currentString); // print it out
latestData = currentString; // save it to the global variable
}
function draw() {
background(0);
mapData = map(latestData, 100, 500, 20, 100);
for(var i = 0; i <= 3; i++){
for(var j = 0; j <= 3; j++){
push();
translate(0, 0);
rotate(rot*0.1);
//scale(scl);
noStroke();
fill(200,random(255));
circle(arr[i],arr[j]*random(50),mouseX*0.05);
pop();
}
}
//noStroke();
fill(0,255,255);
//translate(width/3,height/4);
rotateX(frameCount * 0.1);
torus(mapData*1.5, 5);
fill(0,0,255);
sphere(mapData*0.5);
fill(0,100,255);
rotateY(frameCount * 0.1);
torus(mapData, 5);
fill(200,100,255);
rotateX(frameCount * 0.5);
torus(mapData*2, 3);
rot += 1;
scl += 0.01;
}