xxxxxxxxxx
58
let serial; // variable to hold an instance of the serialport library
let portName = 'dev/tty.usbmodem14101'; // fill in your serial port name here
let circleSize = 10;
function setup() {
createCanvas(windowWidth,windowHeight);
serial = new p5.SerialPort(); // make a new instance of the serialport library
serial.on('list', printList); // set a callback function for the serialport list event
serial.on('connected', serverConnected); // callback for connecting to the server
serial.on('open', portOpen); // callback for the port opening
serial.on('data', serialEvent); // callback for when new data arrives
serial.on('error', serialError); // callback for errors
serial.on('close', portClose); // callback for the port closing
serial.list(); // list the serial ports
serial.open(portName); // open a serial port
}
//
function draw() {
background("orange");
fill(255);
noStroke();
ellipse(width/2,height/2,circleSize,circleSize);
}
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (let i = 0; i < portList.length; i++) {
// Display the list the console:
console.log(i + portList[i]);
}
}
function serverConnected() {
console.log('connected to server.');
}
function portOpen() {
console.log('the serial port opened.')
}
function serialEvent() {
let data = serial.read();
console.log(data);
circleSize = data;
}
function serialError(err) {
console.log('Something went wrong with the serial port. ' + err);
}
function portClose() {
console.log('The serial port closed.');
}