Skip to content

Instantly share code, notes, and snippets.

@fulminator
Last active December 14, 2015 15:19
Show Gist options
  • Save fulminator/5107325 to your computer and use it in GitHub Desktop.
Save fulminator/5107325 to your computer and use it in GitHub Desktop.
// declare a character array of 50 elements (0 to 49)
char buffer[50];
// declaring the index, it should be a number, but it works as char anyhow
char i;
// this is the setup function, simple commands that occur only once
void setup(){
// let's start serialing with a baud rate of 9600 bytes per second
Serial.begin(9600);
// memory setting: all 50 elements of the char array are equal to null character
memset(buffer, '\0', 50);
}
// this is the loop function, it's an infinite loop
void loop(){
i = 0;
// while we have serial data
while (Serial.available()) {
// store this char in the char array and then increment the index by 1
buffer[i++] = Serial.read();
// put a null character after storing 1 char, however, this gets override at the next iteration of the while loop
buffer[i] = '\0';
} // then repeat the process until we will not have any data. and the last item will be null character
// if the length of the buffer will be more than 0 elements, print the buffer.
if (strlen(buffer) > 0){
Serial.println(buffer);
}
} // repeat the loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment