// we need fundamental FILE definitions and printf declarations #include #include // 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) { int seconds ; // wait 1000 milliseconds delay(1000) ; seconds = millis() / 1000 ; // use flash storage for the format string printf_P(PSTR("Alive %d sec\n"), seconds); /* // without printf_P(), you would do this: Serial.print(F("Alive ")) ; Serial.print(seconds) ; Serial.println(F(" sec")) ; */ #if 0 // you can explicitly use a FILE structure like this: fprintf_P( &uartout, PSTR("Alive %d sec\n"), seconds ) ; #endif }