xxxxxxxxxx
64
let dir="F";
function setup() {
createCanvas(400, 400);
// let speech = new p5.Speech();
// speech.speak('Hi there!,This is your walking buddy. Join me to explore the world on foot. Use the commands Right, Left, Forward, Backward and Stop to navigate the directions. Finally, remember to stop when you hear the siren.')
let lang = navigator.language || 'en-US';
let speechRec = new p5.SpeechRec(lang,gotSpeech);
let continuous = true;
let interim = false;
speechRec.start(continuous,interim);
function gotSpeech(){
if (speechRec.resultValue){
createP(speechRec.resultString);
if (speechRec.resultString.toLowerCase().includes('right')) {
dir = "R";
} else if (speechRec.resultString.toLowerCase().includes('left')) {
dir = "L"
} else if (speechRec.resultString.toLowerCase().includes('forward')) {
dir = "F"
} else if (speechRec.resultString.toLowerCase().includes('backward')) {
dir = "B"
} else if (speechRec.resultString.toLowerCase().includes('stop')) {
dir = "S"
}
}
}
}
function draw() {
stroke(0)
background("teal");
textSize(15)
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
}
function keyPressed() {
if (key == " ") {
// important to have in order to start the serial connection!!
setUpSerial();
}
}
// This function will be called by the web-serial library
// with each new *line* of data. The serial library reads
// the data until the newline and then gives it to us through
// this callback function
function readSerial(data) {
if (data != null) {
writeSerial(dir);
}
}