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 ste

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 ( WiFi.status() != WL_CONN

Setting up CUDA 10.0 for mxnet on Google Colaboratory

Preface Recently, I was trying to train model on Google Colaboratory with mxnet. However, I found the CUDA version pre-installed on the Colab. is 10.2. Till now, mxnet only support to CUDA 10.1 Therefore, I started to think about if it is possible to setup the environment that mxnet has support. Till now CUDA 10.1 doesn’t work for me. But I do successfully installed CUDA 10.0. Also tried to trained a LeNet on Colaboratory. Since I am a mac user, NVIDIA GPU is always what we are jealous and envy. Until I found Google Colaboratory…. I’d like to share my journey with you, if you also encounter some problem on setting up environment. Google Colaboratory provide free access to the NVIDIA Tesla K80 GPU (24GB RAM, 4992 CUDA core) . It is a great gift for most of people who is eager to learn deep learning but doesn’t have good hardware systems to train deeper models. NVIDIA Tesla K80 GPU I assume that you already have background knowledge of Python and familiar with tools like jup