Skip to content

Instantly share code, notes, and snippets.

@raster
Last active April 23, 2022 14:26
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 raster/ed1eb004aa17adf9f7d5e475a346e50f to your computer and use it in GitHub Desktop.
Save raster/ed1eb004aa17adf9f7d5e475a346e50f to your computer and use it in GitHub Desktop.
Sprintf Example for Arduino
// Sprintf Example for Arduino
// https://www.programmingelectronics.com/sprintf-arduino/
// Note that sprintf for Arduino does not support float:
// https://forum.arduino.cc/t/solved-why-cant-i-print-a-float-value-with-sprintf/367971
char buffer[25]; // create a buffer long enough to hold string
void setup() {
Serial.begin(115200);
randomSeed(32); // needed only because we use random below in this example
}
void loop() {
int x = random(9);
// below is the use of sprintf to format serial output
sprintf(buffer, "%4d %4d %4d %4d", x, (x * 2), (x * 10), (x * 20));
Serial.println(buffer);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment