This function emulates the stdio.h printf() functionality in C and will send the output to the Serial connection using Serial.print(). The resulting string sent over serial is limited to 128 chars. See the sprintf man page or the examples herein for details on how to build format strings.
#include <stdarg.h>
void p(char *fmt, ... ){
char tmp[128]; // resulting string limited to 128 chars
va_list args;
va_start (args, fmt );
vsnprintf(tmp, 128, fmt, args);
va_end (args);
Serial.print(tmp);
}
Examples:
p("%s", "Hello world");
p("%s\n", "Hello world"); // with line break
unsigned long a=0xFFFFFFFF;
p("Decimal a: %l\nDecimal unsigned a: %lu\n", a, a);
p("Hex a: %x\n", a);
If anyone has a better version please feel free to remove or edit this version.
You can also avoid using Serial.print() altogether and redirect STDIN and STDOUT to UART.
The avr-gcc C library, avr-libc, provides the printf() family of functions. You can use them in Arduino sketches after some preparation. General details are in the avr-libc documentation for file stdio.h. These notes are specific to the Arduino development environment.
You must:
- create a FILE structure instance,
- create a character output function,
- write the function address into the FILE structure,
- assign the structure address to that of 'stdout'.
printf() makes your executable object ~1000 bytes larger, so you may not want to use it if size is a problem.
Example
// we need fundamental FILE definitions and printf declarations
#include <stdio.h>
// create a FILE structure to reference our UART output function
static FILE uartout = {0} ;
// create a output function
// This works because Serial.write, although of
// type virtual, already exists.
static int uart_putchar (char c, FILE *stream)
{
Serial.write(c) ;
return 0 ;
}
void setup(void)
{
// Start the UART
Serial.begin(9600) ;
// fill in the UART file descriptor with pointer to writer.
fdev_setup_stream (&uartout, uart_putchar, NULL, _FDEV_SETUP_WRITE);
// The uart is the standard output device STDOUT.
stdout = &uartout ;
}
void loop(void)
{
float seconds ;
// wait 1000 milliseconds
delay(1000) ;
// calculate seconds as a floating point value
seconds = (float) millis() /1000.0 ;
// report seconds since starting
printf("Alive %.3f sec", seconds ) ;
/*
// without printf(), you would do this:
Serial.print("Alive ") ;
Serial.print(seconds,3) ;
Serial.print("sec") ;
*/
#if 0
// you can explicitly use a FILE structure like this:
fprintf( &uartout, "Alive %.3f sec", seconds ) ;
#endif
}
Extensions
If you can define a character-based output function and reference it in a FILE structure, you can use fprintf() as well. An example for an LCD shield follows below.
Example
#include <LiquidCrystal.h>
// register the LiquidCrystal control and data pins
// for the dfrobots LCD shield.
LiquidCrystal LCD(8,9,4,5,6,7) ;
static FILE lcdout = {0} ; // LCD FILE structure
// LCD character writer
static int lcd_putchar(int char, FILE* stream)
{
LCD.write(ch) ;
return (0) ;
}
void setup(void)
{
// LCD shield is 16 columns by 2 rows
LCD.begin(16,2) ;
// fill in the LCD FILE structure
fdev_setup_stream (&lcdout, lcd_putchar, NULL, _FDEV_SETUP_WRITE);
}
void loop(void)
{
float seconds ;
// calculate seconds as a floating point value
seconds = (float) millis() /1000.0 ;
// set the LCD cursor position to home
LCD.setCursor(0,0) ;
// report seconds since starting
// in the X.xxx floating point format.
fprintf(&lcdout, "Alive %.3f sec", seconds ) ;
// wait 1000 milliseconds
delay(1000) ;
}