xxxxxxxxxx
50
let song;
let serial;
let distance = "waiting for data";
let canPlay = false;
// let play = false;
function preload() {
song = loadSound('door_soundeffect.mp3');
}
function setup() {
createCanvas(400, 400);
// Instantiate our SerialPort object
serial = new p5.SerialPort();
serial.open("/dev/cu.usbmodem1421");
serial.on('data', gotData);
setInterval(function() {
console.log("HELLO");
serial.write(1);
}, 1000);
}
function draw() {
if (distance > 30) {
if (canPlay) {
song.play();
canPlay = false;
}
} else {
canPlay = true;
}
if (distance < 30) {
song.pause();
}
console.log(distance)
}
function gotData() {
let currentString = serial.readLine(); // read the incoming string
trim(currentString); // remove any trailing whitespace
if (!currentString) return; // if the string is empty, do no more
//console.log(currentString); // println the string
distance = Number(currentString); // save it for the draw method
}