Skip to content

Instantly share code, notes, and snippets.

@philippbosch
Created April 16, 2013 12:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save philippbosch/5395696 to your computer and use it in GitHub Desktop.
Save philippbosch/5395696 to your computer and use it in GitHub Desktop.
sprintf() in Arduino code
unsigned int i = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
char buffer[50];
sprintf(buffer, "the current value is %d", i++);
Serial.println(buffer);
}
@RagBillySandstone
Copy link

This is what I was looking for, and it inspired me to bypass the buffer - sprintf(Serial.println(), "the current value is %d", i++); Oddly enough, this compiled and ran but is printing blank lines. Any ideas why?

@webhat
Copy link

webhat commented Mar 20, 2018

@RagBillySandstone you can't use Serial.println() in this way.

@klein0r
Copy link

klein0r commented Mar 27, 2018

@RagBillySandstone You are not bypassing the buffer that way. In your code, Serial.println() had to return a buffer. This is not the case. The buffer is created to store the character of the string and keep them, until they are passed to println as a parameter. So your code doesn't make sense at all, since you've changed the whole logic with your change :)

@Martin7Hacker
Copy link

hola como puedo en arduino
ingresando numeros por teclado que cree una cadena numerica
enves de que se sumen?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment