Back to Parent

let serial;
let latestData = "waiting for data";


function setup() {
   createCanvas(windowWidth, windowHeight, WEBGL);
   serial = new p5.SerialPort();
   serial.list();
   serial.open('/dev/tty.usbmodem101');
   serial.on('connected', serverConnected);
   serial.on('list', gotList);
   serial.on('data', gotData);
   serial.on('error', gotError);
   serial.on('open', gotOpen);
   serial.on('close', gotClose);
}


function serverConnected() {
   print("Connected to Server");
}


function gotList(thelist) {
   print("List of Serial Ports:");
   for (let i = 0; i < thelist.length; i++) {
       print(i + " " + thelist[i]);
   }
}


function gotOpen() {
   print("Serial Port is Open");
}


function gotClose() {
   print("Serial Port is Closed");
   latestData = "Serial Port is Closed";
}


function gotError(theerror) {
   print(theerror);
}


function gotData() {
   let currentString = serial.readLine();
   trim(currentString);
   if (!currentString) return;
   console.log(currentString);
   latestData = currentString;
}


function draw() {
   if (latestData > 200) {
      
       background(0, 0, 0);
   }
   else {
       document.body.style.backgroundColor = "white";
       background(255, 255, 255);
       let radius = width * 1.5;
       // ------------------------------------
       // 3D WORLD - from https://p5js.org/examples/3d-orbit-control.html
       //drag to move the world.
       orbitControl();
       normalMaterial();
       translate(0, 0, -600);
       for (let i = 0; i <= 12; i++) {
           for (let j = 0; j <= 12; j++) {
               push();
               let a = (j / 12) * PI;
               let b = (i / 12) * PI;
               translate(
                   sin(2 * a) * radius * sin(b),
                   (cos(b) * radius) / 2,
                   cos(2 * a) * radius * sin(b)
               );
               if (j % 2 === 0) {
                   cone(30, 30);
               } else {
                   box(30, 30, 30);
               }
               pop();
           }
       }
       // ------------------------------------
   }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0