xxxxxxxxxx
93
var phrases;
let serial; // variable for the serial object
let latestData = "waiting for data"; // variable to hold the data
let index = 0;
var sfx;
let portName = '/dev/tty.usbmodem14301';
let inData;
let static;
let isLooping = false;
function preload() {
phrases = loadStrings("phrases.txt");
sfx = loadSound("253168__suntemple__sfx-ui-button-click.wav")
static = loadSound("static.wav")
}
function setup() {
createCanvas(windowWidth, windowHeight);
textAlign(CENTER);
textSize(20);
if (!isLooping) {
static.loop();
isLooping = true;
}
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);
}
// function mySelectEvent() {
// let item = portSelector.value();
// // if there's a port open, close it:
// if (serial.serialport != null) {
// serial.close();
// }
// // open the new port:
// serial.open(item);
// }
// 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:
console.log(i + portList[i]);
}
}
function serverConnected() {
console.log('connected to server.');
}
function portOpen() {
console.log('the serial port opened.')
}
function serialEvent() {
inData = Number(serial.read());
console.log(inData)
}
function serialError(err) {
console.log('Something went wrong with the serial port. ' + err);
}
function portClose() {
console.log('The serial port closed.');
latestData = "Serial Port is Closed";
}
function draw() {
console.log(latestData);
background(50);
noStroke();
fill(255);
text(phrases[index], width/2, height/2)
if (inData == 1 && !sfx.isPlaying()) {
sfx.play();
inData = 2;
index++;
}
}