I have been trying to get my Arduino Uno to only answer once whenever I send a string over the serial monitor, but it does not seem to work.
Here is my code:
byte buf;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
while(Serial.available()) buf = Serial.read();
Serial.write("a");
}
}
I would expect to get back "a" when I write "asdf", but I am getting "aaaa". As far as I know, the while loop should empty the serial read buffer, but why does it repeat the next time the loop function is called?
yes, I know that I am writing "a". But when I write "asdf" in the serial monitor, should it not be emptied when the while loop is running? That would mean it only prints "a", because the next time the loop method is called, Serial.available() should return false, right? And then it would not write "a" to the computer. But I am getting back "aaaa" in the serial monitor when I write "asdf". That means, it executes the while loop 4 times, but why?
I have been trying to get my Arduino Uno to only answer once whenever I send a string over the serial monitor, but it does not seem to work.
I would expect to get back "a" when I write "asdf", but I am getting "aaaa". As far as I know, the while loop should empty the serial read buffer, but why does it repeat the next time the loop function is called?
Thanks in advance
kev03
Since you are typing by hand you can only get one character in before Serial.available() becomes false and loop() gets called again. It is probably only executing the while loop once per loop() call.
I bet if you try the following you will get "abababab" which illustrates what I am saying.
byte buf;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while(!Serial);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
while(Serial.available()) buf = Serial.read();
Serial.write("a");
}
Serial.write("b");
}
It going faster than you can type so when you type your first 'a' it is read and prints out an a. Does this for each letter you type so you end up with aaaa
Try the following tested codes and compare them with your codes and learn the style of UART Port Programming.
char buf[5]; //buffer to hold charcater coming from InputBox of Serial Monitor
int j = 0;
void setup()
{
Serial.begin(9600);
}
void loop()
{
byte n = Serial.available(); //check if charcater has arrived from Serial Monitor
if (n == 4) //four charcaters: asdf have arrived from Serial Monitor
{
for (int i = 0; i < 4; i++)
{
buf[i] = Serial.read(); //save the arrived charcater in buf[] array
}
if (strcmp(buf, "asdf") == 0) //check if arrived string is asdf
{
Serial.println('a'); //string matched; send a to OutputBox of Serial Monitor
}
}
}
If you enter 4 characters then press return they become available() one after the other, not all at once. Serial.read() reads them one at a time so your code does 4 Serial.read()s and prints "a" each time
Try printing buf before printing the "a" then it might make more sense
kev03:
I have been trying to get my Arduino Uno to only answer once whenever I send a string over the serial monitor
how does the Arduino know you are done sending a string?
you need an end marker to your message. the serial console can send CR ('\r') or LF ('\n') or both (CR+LF). if you set that up, you need to wait for that char to come in and then you know the user has finished sending his line.