xxxxxxxxxx
92
let dir = "0";
let robotX, robotY;
function setup() {
createCanvas(600, 400);
robotX = width / 2;
robotY = height /2+60;
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") && robotX < width - 50) {
dir = "1";
robotX+=10
} else if (speechRec.resultString.toLowerCase().includes("left") && robotX > 50) {
dir = "2";
robotX-=10
} else if (speechRec.resultString.toLowerCase().includes("forward") && robotY > 100) {
dir = "3";
robotY-=10
} else if (speechRec.resultString.toLowerCase().includes("backward") && robotY < height-50) {
dir = "4";
robotY+=10
} else if (speechRec.resultString.toLowerCase().includes("stop")) {
dir = "0";
}
// if (serialActive) {
// writeSerial(dir);
// }
}
}
}
function draw() {
stroke(0);
background(255);
fill("#FFD722");
textSize(50);
textFont("Times New Roman");
text("WALKING BUDDY", width / 2 - 200, height - 20);
fill(255, 0, 0);
ellipse(robotX, robotY, 30, 30);
fill(255);
textSize(15);
if (!serialActive) {
text("Press Space Bar to select Serial Port", 20, 30);
} else {
text("Connected", 20, 30);
}
}
function keyPressed() {
if (key == " ") {
setUpSerial();
}
}
function readSerial(data) {
////////////////////////////////////
//READ FROM ARDUINO HERE
////////////////////////////////////
if (data != null) {
// make sure there is actually a message
// split the message
let fromArduino = split(trim(data), ",");
// if the right length, then proceed
if (fromArduino.length == 1) {
// only store values here
// do everything with those values in the main draw loop
// We take the string we get from Arduino and explicitly
// convert it to a number by using int()
// e.g. "103" becomes 103
console.log(fromArduino);
}
console.log(dir);
let sendToArduino=dir+"\n"
writeSerial(sendToArduino);
}
}