2015-12-21 152 views
0

我正尝试通过串口工作来构建一个Arduino聊天僵尸工具。它将这些东西发送到我的Mac。我有很多问题,错误等等。有人可以请指点我正确的方向吗?这是我的代码到目前为止。我知道这并不完美,但这就是为什么我要学习。与Arduino聊天僵尸工具

//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

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

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

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

或者你可以将字符串转换为大写,并做一个比较。参见:https://www.arduino.cc/en/Reference/StringToUpperCase