xxxxxxxxxx
84
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.usbmodem1421'; // fill in your serial port name here
var inData; // for incoming serial data
let song;
function preload() {
song = loadSound('assets/Hold_On.mp3', loaded);
}
function setup() {
//song = loadSound('assets/Hold_On.mp3', loaded);
song.setVolume(0.3);
createCanvas(400, 300);
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 loaded() {
console.log('loaded');
}
function draw() {
background(0);
fill(255);
text("button value: " + inData, 30, 30);
if (inData == 1){
togglePlaying();
}
}
function togglePlaying() {
if (song.isPlaying() ) { // .isPlaying() returns a boolean
song.stop();
} else {
song.play();
}
}
// get the list of ports:
function printList(portList) {
// portList is an array of serial port names
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
println(i + " " + portList[i]);
}
}
function serverConnected() {
println('connected to server.');
}
function portOpen() {
println('the serial port opened.')
}
function serialEvent() {
inData = Number(serial.read());
}
function serialError(err) {
println('Something went wrong with the serial port. ' + err);
}
function portClose() {
println('The serial port closed.');
}
function playSong() {
song.play();
}