[英]How do I read a char array from the Serial in setup() and wait long enough for the user to hit send?
I am using an Arduino Mega, and it will be bluetooth controlled with a Bluetooth Terminal app for Arduino.我正在使用 Arduino Mega,它将通过 Arduino 的蓝牙终端应用程序进行蓝牙控制。 What I want it to do is, when it starts up, at setup- I want it to prompt the user for a 4 digit PIN, and once the user is finished entering the pin and hits send the program should proceed further and not before that.
我想要它做的是,当它启动时,在设置时 - 我希望它提示用户输入 4 位 PIN,一旦用户完成输入 PIN 并点击发送,程序应该继续进行,而不是在此之前. This is what I tried
这是我试过的
unsigned char buff[4];
void getPassword() {
int j = 0;
Serial1.println("Enter the PIN: ");
for (;;) {
while (Serial1.available() > 0) {
buff[j] = Serial1.read();
j++;
}
if (j >= 3) {
break;
}
}
}
void setup() {
getPassword();
}
It doesn't seem to work at all.它似乎根本不起作用。 How do I achieve this?
我如何实现这一目标?
I notice that your password buffer is 4 chars long.我注意到您的密码缓冲区有 4 个字符长。 But you are only reading 3 characters from Serial.
但是您只能从 Serial 中读取 3 个字符。 IS the password actually 3 or 4 characters long?
密码实际上是 3 或 4 个字符长吗? If it is 4 characters long, then when j is 3 it isn't time to exit that loop yet.
如果它是 4 个字符长,那么当 j 是 3 时还不是退出该循环的时候。 When j is 3 you are waiting for the fourth character.
当 j 为 3 时,您正在等待第四个字符。 You want to exit AFTER you read the fourth character and j gets incremented again and becomes 4.
您想在读取第四个字符后退出,并且 j 再次递增并变为 4。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.