I want the Arduino code to wait until the user gives his/her name and hit "enter"

I want the Arduino code to wait until the user gives his/her name and hit "Enter", and then the code to proceed to the main loop.

I am using Serial.readBytesUntil('0D', name, 30);

0D is the ASCII for Enter, but it does not seem to work....

Where did you find this syntax ?


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the < CODE / > icon from the ‘posting menu’ to attach the copied sketch.

1 Like

Not that way. You forgot the HEX notation.

1 Like

Why do you put two characters between the single ticks? From the reference (Serial.readBytesUntil() - Arduino Reference) a char is expected; '0D' is not a char.

If you set compiler warnings to "ALL" in file → preferences, you will get the following warning on the use of '0D'

 warning: multi-character character constant [-Wmultichar]

In C/C++, it's a lot easier to use '\r' and/or '\n'.

Be aware that there are several possibilities for <enter>

  1. <CR> (0x0D, '\r')
  2. <LF> (0x0A, '\n')
  3. <CR><LF>
1 Like

Try this code:

char name[30];
void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println("What is your name");
}

void loop() {
  if(Serial.available())
  {
     Serial.readBytesUntil(0x0D, name, 30);
     Serial.println(name);
  }
}
1 Like

Here: https://github.com/arduino-libraries/USBHostGiga/blob/master/USBHostGiga.h#L136 and more specifically here: char getAscii(HID_KEYBD_Info_TypeDef evt);
when I press a key from an external keyboard connected to the USB of the Arduino, I get the pressed key with a square.. So when I press the 'h' I get:

h (square)

How can I get only the 'h' in the output?

How do we get from a readBytesUntil() to a Giga and USB keyboard?

Can you please post the code that gives the output of post #6. At least people can look at that.

You can print the characters using Serial.print(someChar, HEX) to see what the character actually is. Based on that you can decide what to do (e.g. ignore 0x0D).

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.