Neat method!
Did you mean digits[4] instead of char[4]? it yelled at me about that
Yes.
But the bad news is, the whole code is
1. written badly
2. does not work
sorry here is a another attempt ![]()
I would implement the function (for the arduino environment only) a static array that is created on
startup.I actually never tried to create and return an array inside a function.
Putting the whole conversion into a while() loop was completely dumb of me. We are handling all 5 digits
in the code, there is nothing to iterate over.
Ok, this one should be better
//somewhere at the top of the code
char digits[5];
void intToChar(int value) {
//blank the char values
for(int i=0;i<5;i++) {
digits[i]=' ';
}
digits[4]=(char)((value%10)+48);
value=value/10;
digits[3]=(char)((value%10)+48);
value=value/10;
digits[2]=(char)((value%10)+48);
value=value/10;
digits[1]=(char)((value%10)+48);
value=value/10;
digits[0]=(char)((value%10)+48);
}
//Sorry, I never used the lcd-lib, does the lib take an array-argument ??
lcd.print(intToChar(1));
I hope this one is a bit better.
As a little extra here is some code that use in one of my projects to format the currrent value returned by millis()
into a string to be printed onto a display with an SPI-interface.
void writeUptime() {
char uptimeVal[16];
//build a String With the uptime
unsigned long ut=millis();
unsigned long m_sec=ut%1000;
ut=ut/1000;
unsigned long sec=ut%60;
ut=ut/60;
unsigned long m=ut%60;
ut=ut/60;
unsigned long h=ut%24;
ut=ut/24;
//I have 16 chars and want the uptime centered on the upper line of the display
uptimeVal[0]=' ';
uptimeVal[1]=' ';
uptimeVal[2]=(h/10)+48;
uptimeVal[3]=(h%10)+48;
uptimeVal[4]=':';
uptimeVal[5]=(m/10)+48;
uptimeVal[6]=(m%10)+48;
uptimeVal[7]=':';
uptimeVal[8]=(sec/10)+48;
uptimeVal[9]=(sec%10)+48;
uptimeVal[10]=':';
//milliseconds start here
uptimeVal[11]=(m_sec/100)+48;
m_sec=m_sec%100;
uptimeVal[12]=(m_sec/10)+48;
uptimeVal[13]=(m_sec%10)+48;
uptimeVal[14]=' ';
uptimeVal[15]=' ';
//now write that string to the display
//none of these next calls will work for the lcd-lib
clearDisplay();
moveCursor(0,0);
//now print it to the LCD
digitalWrite(CS,LOW);
delay(0);
for(int i=1;i<16;i++) {
spi_transfer(uptimeVal[i],true);
}
digitalWrite(CS,HIGH);
moveCursor(0,1);
}
Sorry for the misinformation
Eberhard