Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@eliezio
Created October 15, 2014 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eliezio/2b8c324534aecac6a8a7 to your computer and use it in GitHub Desktop.
Save eliezio/2b8c324534aecac6a8a7 to your computer and use it in GitHub Desktop.
Arduino readString
int readString(char *buffer, int max_len, int terminator)
{
int pos = 0;
while (true) {
if (Serial.available() > 0) {
int readch = Serial.read();
if (readch == terminator) {
buffer[pos] = '\0';
break;
} else if (readch != '\n' && pos < max_len-1) { // Ignore new-lines
buffer[pos++] = readch;
}
}
}
return pos;
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
static char buffer[80];
int len = readString(buffer, sizeof(buffer), '\r');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment