Archive

Posts Tagged ‘printf’

printf in arudino works.

December 21st, 2010

I’ve always had to deal with the fact that printf() is missing from the arduino environment.  I would have to come up with these huge blocks of code consisting of multiple Serial.print() commands to put together a simple message.  Well, no more!  It seems that printf works just fine, and for some unknown reason, it’s just not used or talked about. 

 

In order for it to work, you only need to specify a target for STDOUT by adding this function..

 

int my_putc( char c, FILE *t) {
  Serial.write( c );
}

 

and inside the setup() function you need to point to that function

 

void setup() {
…
fdevopen( &my_putc, 0);
}

That it!  Now printf() works!

 

  Serial.println("START");
  fdevopen( &my_putc, 0);
  printf("hello, world!");

 

image

 

Why this isn’t documented, I have no idea!

Uncategorized