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?
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.
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.
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.