xxxxxxxxxx
39
var serial; // variable to hold an instance of the serialport library
var fromSerial = 0; //variable to hold the data
function setup() {
createCanvas(320, 240);
serial = new p5.SerialPort(); // make a new instance of serialport library
serial.on('list', printList); // callback function for serialport list event
serial.on('data', serialEvent); // callback for new data coming in
serial.list(); // list the serial ports
serial.open("/dev/cu.usbmodem1411"); // open a port
}
function draw() {
// do your drawing stuff here
noFill();
stroke (random(255),random(255), random(255));
ellipse (width/2, height/2, fromSerial, fromSerial);
}
// get the list of ports:
function printList(portList) {
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
println(i + " " + portList[i]);
}
}
function serialEvent() {
// this is called when data is recieved, data will then live in fromSerial
asciiFromSerial = serial.readLine();
if (asciiFromSerial.length>0){
asciiFromSerial = trim (asciiFromSerial);
fromSerial = Number (asciiFromSerial);
println(fromSerial);
}
}