Skip to main content

Sprintf function in Arduino

Arduino doesn’t support printf function like C and C++. Which makes formatting string with numbers a little bit harder. But there’s a few ways to generate similar effect.

Using char buffer and sprintf

int height = 8223;
char buffer[100];
void setup() {
  Serial.begin(9600);
  sprintf(buffer, "Aconcagua is %d metres height.", height);
  Serial.println(buffer);
}
void loop() {
}

Create a char buffer and use sprintf to format text with int numbers. However, the sad thing is that this method does not support float, double and long long.


Using String with println
Another way to join the string with an integer or float number together is by adding String(“”) into your println() function. Which will make string and float number join together.

float height = 8223.05;
void setup() {
  Serial.begin(9600);
  Serial.println(String("") + "Aconcagua is " + height + " metres height.");
}
void loop() {
}

However, we still cannot control the decimal of output float number by this way.

Comments

  1. Thank you for a very interesting article. I greatly appreciate the time you take to do all the research to put together your posts. I especially enjoyed this one!!
    Cnc controller

    ReplyDelete

Post a Comment

Popular posts from this blog

Arduino CNC shield control Stepper motor with DRV8825

CNC shield is quite useful for stepper motor driving. Here, I demonstrated how to use simple arduino code to drive stepper motor with DRV8825. First, just simply mount CNC shield onto Arduino Uno. Make sure the direction of the shield was right, where both the USB port and power supply wire was on your left hand site. The blue wire is my power supply which can be connect to 12-36V of power source. Next step you can mount the DRV8825 chip onto the CNC shield. Make sure the DRV8825 chip goes like this direction. If you put the chip in the wrong direction, you will probably damage it. By adjusting the jumber underneath the DRV8825 chip, motors can be driven with different kind of microstepping mode. I put three number here so it means that I set it into 1/32 step driving mode. Which is the most precise  one of DRV8825. The motor can connect to the right site of the DRV8825. Plug in the usb to your computer and upload these coded which will generate...

Connect Arduino Wemos D1 ESP8266 to Internet/Wi-Fi Router

Connect ESP8266 to Wi-Fi Router Upload these code to your Arduino WeMos D1 ESP8266 W-Fi board. #include <ESP8266WiFi.h> //SSID of your network char ssid[] = " myRouter"; //SSID of your Wi-Fi router char pass[] = " myPassWord"; //Password of your Wi-Fi router void setup() {   Serial.begin(115200);   delay(10);   // Connect to Wi-Fi network   Serial.println();   Serial.println();   Serial.print("Connecting to...");   Serial.println(ssid);   WiFi.begin(ssid, pass);   while ( WiFi.status() != WL_CONNECTED) {     delay(500);     Serial.print(".");   }   Serial.println("");   Serial.println("Wi-Fi connected successfully"); } void loop ( ) {} Using ESP8266 to connect to Wi-Fi need to use the function of: WiFi.begin(ssid, pass); // connect to target Wi-Fi SSID is the name of the Wi-Fi you want to connect to.  while ( ...

Can D13 pin of Arduino Uno and nano be use as input pin?

D13 pin can be use as  digital output pin. D13 pin can be use as i nput pin  together with  external pull-up  and  pull-down resistor . It is not recommand to use the  D13 internal pull-up  resistor because instead of the regular 5V, the voltage will be hanging around 1.7V. Most of Arduino newbies have their first code to blink the LED with D13 pin as an output pin. This really give us big confident to know the code can be successfully executed on the microcontroller. However, due to the build-in D13 pin on different Arduino board has slightly different circuit design,  is there any limitation to use the D13 GPIO pin as input pin ? Let’s find out more! Experiment condition I investigate both the value of D13 digital input and LED light status with the following code with  Arduino Uno Rev3 . and  Arduino nano 3.0.  void setup() { pinMode(13, INPUT); Serial.begin(9600); } void loop() ...