Created
October 15, 2014 15:55
-
-
Save eliezio/2b8c324534aecac6a8a7 to your computer and use it in GitHub Desktop.
Arduino readString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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