I have been fighting with this for a long time. The code snipped below is to illustrate my problem.
I have a assigned a long value. This is a 4 byte signed value. I assign it a number that is larger then would fit in a 2 byte integer.
When I just print the value direct it is correctly displayed.
When I use formated printing with sprintf the result is wrong. It seems that sprintf %d only works on the least significant bytes of the 4 byte long value. The sprintf works ok for number below 32767 (0X7FFF).
Is this a bugg? Is this a restriction of the compiler? Do I need a special format string in stead of %d?
I want the formatted output as this is the way to get the number correctly alligned.
Does some body know how? Or do I have to program my own formatting routine?
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
// Pins for LCD: RS , RW , E , D4 , D5, D6 ,D7
long lvalue = 500000l;
char line[64];
void setup() {
lcd.begin(16, 2);
lcd.setCursor (0,0);
lcd.print (lvalue); // this prints correct value 500000
sprintf(line, "%6d", lvalue) ;
lcd.setCursor (0,1);
lcd.print (line); // this pints -24288
}
void loop() {
// put your main code here, to run repeatedly:
}