Unsigned int not working on ATTINY1624

Hello I am working on an ATTINY1624 and cannot get unsugned int to work, here is my test code:

void setup() {
  // put your setup code here, to run once:
  Serial1.begin(57600); //serial port for the monitoring
  Serial1.println("Started");
  char tmp[20];  
  unsigned int test = 19200;
  sprintf (tmp,"Test = %d",test);
  Serial1.println(tmp);
  test=test *2;
  sprintf (tmp,"Test = %d",test);
  Serial1.println(tmp);  
}

Here is what I get in the serial monitor...

Started
Test = 19200
Test = -27136

The variable int is displaying it's value in the negative, but I need it to show the unsigned value of 38400.

I assume I need an extra library for my PIC to handle the numbers correctly but have no idea which one it should be, please can anyone make a suggestion?

Thanks

Try that instead

you don't use %d to print an unsigned int.

PS: please use CODE tags not QUOTE tags for your program. corrected for this time.

Compiler warnings, if you have those activated, can often help to identify such problems.

Well that was quick - thanks all. I now get this...

Started
Test = 19200
Test = 38400

So, the issue all along was not the value of the variable, but the way it was displayed - I'm only printing it for debug purposes, and made the assumption the variable was wrong but the display was right. :thinking:

I got the wrong quotes sorry, I'll use these in future.

thanks - I would usually not add code tags but you had tried to do the right thing.

you can read How to get the best out of this forum if you want to make the most of the support you can get from here.

By the way, megaTinyCore has Serial.printf() so you don't need to go through the tmp string.

Yay! another 3rd party core that supports a printf() method in the Print class.
If only the Arduino.cc development team would add this to their cores.
I gave trying to convince them after 10 years of trying.
Tom Igoe believes that xxprintf() formatting is too complicated and scary and has strongly always pushed against it. :scream:

--- bill

Looks like he was right - see post #1 :wink:

The int is probably 2 bytes on this board so it overflows

touche. :crossed_swords: Pretty Funny.

Granted, there can be some gotchyas for less experienced users, but I don't think that it shouldn't be done because some users may initially struggle to use it.

Overall, IMO, it is still worth having a printf() method in the Print class since there is not much formatting capability, particularly for field widths, in what Arduino.cc originally and still provides in their Print class.
It isn't like it replaces the original simpler Print class output functions.

History is showing that pretty much all the 3rd party developers share this same mindset as they have added it to their platform cores.

And after-all, it is well known and documentation on the formatting strings is literally everywhere since it has been around for over 50 years now.

--- bill

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.