xxxxxxxxxx
96
let serial, music, questions, anmtn;
//let speech;
let process = false;
var portSelector;
let latestData = "waiting for data";
var i = 0;
let timer = 0;
let options = {
baud: 9600, // Serial baud Rate
};
function preload() {
music = loadSound("soothingmusic.mp3");
questions = loadStrings("questions.txt");
font = loadFont("Inconsolata-VariableFont_wdth,wght.ttf");
anmtn = createVideo("animation.mov", loadAnmtn);
}
function loadAnmtn() {
return;
}
function setup() {
createCanvas(windowWidth, windowHeight);
serial = new p5.SerialPort(options); // Create the serial object
let portlist = serial.list(); // get an array of available serial ports
// Register some callbacks
serial.on("connected", serverConnected); // print feedback on connection
serial.on("list", gotList); // print out list of available ports
serial.on("error", gotError); // print out any errors when they occur
serial.on("open", gotOpen); // print out feedback when the port is opened
serial.on("data", gotData); // callback for when p5 receives data from arduino, usually called serialEvent
anmtn.hide();
anmtn.position(0, 0);
//setTimeout(drawText, 10000);
//speech = new p5.Speech();
//speech.onLoad = voiceReady;
}
// function voiceReady() {
// console.log(speech.voices);
// speech.setVoice("Alice");
// }
// function mousePressed() {
// speech.speak(questions[i]);
// }
function drawText() {
fill("#ED225D");
textFont(font);
textSize(65);
textAlign(CENTER);
text(questions[i], width / 5, height / 2.5, 800, 500);
setTimeout(drawText, 10000);
}
function keyPressed() {
i++;
// speech.speak(questions[i]);
}
function draw() {
background(220);
image(anmtn, 0, 0);
drawText();
}
function gotData() {
let currentString = serial.readLine(); // store the data in a variable
trim(currentString); // get rid of whitespace
if (!currentString) return; // if there's nothing in there, ignore it
latestData = currentString; // save it to the global variable
if (currentString) {
console.log(latestData);
if (latestData == 1) {
if (process == false) {
if (!music.isPlaying()) {
music.loop();
anmtn.loop();
process = true;
}
}
}
serial.write("x");
}
}