int variable = 5;
void setup() {
Serial.begin(115200);
Serial.println("Hello");
Serial.println("There's a common mistake that many Arduino beginners make when they want to Serial.print a variable with a label.");
Serial.println("This first approach looks very intuitive at first. And it works in some other programming languages.");
Serial.println("Unfortunately it doesn't work in Arduino.");
Serial.println("But there are some alternatives that you can use.");
Serial.println("");
Serial.println("This will not work:");
Serial.println(variable + "°C");
Serial.println("The variable is: " + variable);
Serial.println("This works:");
Serial.print(variable);
Serial.println("°C");
Serial.print("The variable is: ");
Serial.println(variable);
Serial.println("Alternative using the Arduino String class:");
Serial.println(String(variable) + "°C");
Serial.println(String("The variable is: ") + variable);
Serial.println("Alternative using snprintf and a buffer: ");
char buffer[30];
snprintf(buffer, 30, "%i°C", variable);
Serial.println(buffer);
snprintf(buffer, 30, "The variable is: %i", variable);
Serial.println(buffer);
//https://cplusplus.com/reference/cstdio/snprintf/
Serial.println("Scroll up in the serial monitor to read everything");
}
void loop() {
delay(10000);
}