用 Arduino 制作聊天机器人
Making a chat bot with Arduino
我正在尝试使用通过串口工作的 Arduino 构建一个聊天机器人。它将东西发送到我的 Mac。我有很多问题、错误等等。有人可以指出我正确的方向吗?到目前为止,这是我的代码。我知道它并不完美,但这就是我努力学习的原因。
//error responses from 1 to 10
void error11() {
Serial.println("What do you mean");
}
void error10() {
Serial.println("I dont understand");
}
void error9() {
Serial.println("My Programmer didnt give me a response for that please ask another question");
}
void error8() {
Serial.println("?????");
}
void error7() {
Serial.println("Huh??");
}
void error6() {
Serial.println("Can not compute");
}
void error5() {
Serial.println("Can you say that again");
}
void error4() {
Serial.println("Im sorry what");
}
void error3() {
Serial.println("Hmmm what");
}
void error2() {
Serial.println("What");
}
void error1() {
Serial.println("Sorry What Did You Say");
}
// greeting responses from 1 to 10
void greeting10() {
Serial.println("What can i do for you");
}
void greeting9() {
Serial.println("Yo");
}
void greeting8() {
Serial.println("Hello Master");
}
void greeting7() {
Serial.println("Greetings!!");
}
void greeting6() {
Serial.println("Sup");
}
void greeting5() {
Serial.println("Hiya");
}
void greeting4() {
Serial.println("hi");
}
void greeting3() {
Serial.println("How is it going");
}
void greeting2() {
Serial.println("What's up");
}
void greeting1() {
Serial.println("hello Friend");
}
String stringRead;
long randNumber;
void setup() {
Serial.begin(9600);
}
void loop() {
randNumber = random(4);
if (Serial.available()) {
stringRead = Serial.readStringUntil('\n');
if(stringRead =="hello","Hello","HELLO") {
greeting8();
if (Serial.available()) {
stringRead = Serial.readStringUntil('\n');
if(stringRead =="hi","Hi","HI") {
greeting4();
}
} else {
error3();
}
}
}
}
我真的很想得到从 1 到 10 的随机响应,但我也无法让它工作。任何帮助将不胜感激。
if(stringRead =="hello","Hello","HELLO") {
不是比较多个项目的方法。您需要使用 logical OR:
if(stringRead =="hello" || stringRead == "Hello" || stringRead == "HELLO") {
或者您可以将字符串转换为大写并只进行一次比较。参见:https://www.arduino.cc/en/Reference/StringToUpperCase
我正在尝试使用通过串口工作的 Arduino 构建一个聊天机器人。它将东西发送到我的 Mac。我有很多问题、错误等等。有人可以指出我正确的方向吗?到目前为止,这是我的代码。我知道它并不完美,但这就是我努力学习的原因。
//error responses from 1 to 10
void error11() {
Serial.println("What do you mean");
}
void error10() {
Serial.println("I dont understand");
}
void error9() {
Serial.println("My Programmer didnt give me a response for that please ask another question");
}
void error8() {
Serial.println("?????");
}
void error7() {
Serial.println("Huh??");
}
void error6() {
Serial.println("Can not compute");
}
void error5() {
Serial.println("Can you say that again");
}
void error4() {
Serial.println("Im sorry what");
}
void error3() {
Serial.println("Hmmm what");
}
void error2() {
Serial.println("What");
}
void error1() {
Serial.println("Sorry What Did You Say");
}
// greeting responses from 1 to 10
void greeting10() {
Serial.println("What can i do for you");
}
void greeting9() {
Serial.println("Yo");
}
void greeting8() {
Serial.println("Hello Master");
}
void greeting7() {
Serial.println("Greetings!!");
}
void greeting6() {
Serial.println("Sup");
}
void greeting5() {
Serial.println("Hiya");
}
void greeting4() {
Serial.println("hi");
}
void greeting3() {
Serial.println("How is it going");
}
void greeting2() {
Serial.println("What's up");
}
void greeting1() {
Serial.println("hello Friend");
}
String stringRead;
long randNumber;
void setup() {
Serial.begin(9600);
}
void loop() {
randNumber = random(4);
if (Serial.available()) {
stringRead = Serial.readStringUntil('\n');
if(stringRead =="hello","Hello","HELLO") {
greeting8();
if (Serial.available()) {
stringRead = Serial.readStringUntil('\n');
if(stringRead =="hi","Hi","HI") {
greeting4();
}
} else {
error3();
}
}
}
}
我真的很想得到从 1 到 10 的随机响应,但我也无法让它工作。任何帮助将不胜感激。
if(stringRead =="hello","Hello","HELLO") {
不是比较多个项目的方法。您需要使用 logical OR:
if(stringRead =="hello" || stringRead == "Hello" || stringRead == "HELLO") {
或者您可以将字符串转换为大写并只进行一次比较。参见:https://www.arduino.cc/en/Reference/StringToUpperCase