[英]Reading and using a string of characters through user input? C# (Arduino Uno)
I'm sure the answer is very simple, but I haven't found it anyway. 我敢肯定答案很简单,但是我还是没有找到答案。 Actually, this bit of code almost does what I want it to do... It waits for input, then once the user has entered something, it checks whether or not the first letter begins with a vowel.
实际上,这段代码几乎可以完成我想要的工作……它等待输入,然后在用户输入内容后,它检查第一个字母是否以元音开头。
For yes and no different messages are displayed. 如果是,则不会显示任何其他消息。 It will do this fine... but if I add
Serial.println(input)
to check what was actually read, I get the first letter only. 它将做得很好...但是如果我添加
Serial.println(input)
来检查实际读取的内容,则只会得到第一个字母。 It seems as though it's only actually saving the first character into the first memory slot, and nothing else. 似乎实际上只是将第一个字符保存到第一个内存插槽中,而没有其他任何事情。
I want to keep the whole word, be able to print it out and use it later. 我想保留整个单词,可以打印出来并在以后使用。
Serial.println(input[0])
prints the same thing as the previously mentioned statement, and Serial.println(input[1])
(or anything where 1 is higher) does not print anything, so I'm assuming that the issue is in reading the characters in the first place. Serial.println(input[0])
打印与前面提到的语句相同的东西,而Serial.println(input[1])
(或Serial.println(input[1])
任何东西)不打印任何东西,所以我假设问题出在首先是阅读字符。
Serial.println("Enter a word: ");
while (!Serial.available()) {
;
}
char input[100] = {Serial.read()};
if (input[0] == 'a' || input[0] == 'e' || input[0] == 'i' || input[0] == 'o' || input[0] == 'u'){
Serial.println("the word begins with a vowel\n");
}
else{
Serial.println("the word does not begin with a vowel");
}
according to Arduino::Serial.Read() documentation: 根据Arduino :: Serial.Read()文档:
Returns
退货
the first byte of incoming serial data available (or -1 if no data is available) - int
可用的传入串行数据的第一个字节(如果没有可用数据,则为-1)-int
So it appears to me, you simply use the wrong function here. 在我看来,您在这里只是使用了错误的功能。
You probably need to use Arduino::Serial.ReadBytes() 您可能需要使用Arduino :: Serial.ReadBytes()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.