xxxxxxxxxx
59
let dir="S";
function setup() {
createCanvas(600, 400);
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"
}
if (serialActive) {
writeSerial(dir);
}
}
}
}
function draw() {
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();
}
}
function readSerial(data) {
if (data != null) {
let fromArduino=trim(data)
print(fromArduino)
writeSerial(dir);
}
}