Last active
April 23, 2022 14:26
-
-
Save raster/ed1eb004aa17adf9f7d5e475a346e50f to your computer and use it in GitHub Desktop.
Sprintf Example for Arduino
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
// 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