用Arduino制作聊天机器人

时间:2015-12-21 19:09:21

标签: arduino serial-port arduino-uno

我正在尝试使用通过串口工作的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得到随机响应,但我也无法做到这一点。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

if(stringRead =="hello","Hello","HELLO") {

不是比较多个项目的方法。您需要使用logical OR

if(stringRead =="hello" || stringRead == "Hello" || stringRead == "HELLO") {

或者您可以将字符串转换为大写字母并进行一次比较。请参阅:https://www.arduino.cc/en/Reference/StringToUpperCase