C strings that you should use in small memory environments are char arrays where the text is ASCII coded characters ending in a terminating 0 (ASCII NULL)
Please avoid C++ String objects with Arduino. They behave badly. You can get away with using them but it's a bad habit to learn. People who program PCs with loads of RAM use them and bring those habits along but that doesn't make it a good idea on Arduinos, not at all!
You could do something like this.
const byte buflen = 10;
char buf[ buflen ];
// ...............
if ( keyValue < buflen - 1 )
{
for (byte i = 0; i <= keyValue; i++)
{
buf[ i ] = '#'; // '#' is ASCII 35 in easy to read form
}
buf[ i ] = 0;
}
Note that with C strings you have to check limits or make sure that your code never exceeds them. That's only a minus if you don't think that hardware awareness is a good thing.
With C strings, the data stays where I put it and only uses the space I give it.
BTW, what do you need the string of #'s for? If it's for serial/print output then skip the buffering.
for (byte i = 0; i <= keyValue; i++)
{
Serial.write( '#' );
}
I'd suggest the key to this riddle is what is being held in keyValue. Whatever it is, it's likely different to what you expect. I also see no evidence of you attempting to terminate the string before printing it.
Then i have a login screen, my A,B,C,D is menu selectors, and on the login screen,
Im displaying # to hide the actual numbers, it means nothing, but it's feels right.
so first time user hits the loop, by pressing a number, it's :#, second time :##, and so on.
I was previously using a
switch(keyValue)
case 1:
display.println(" #");
break;
case 2:
display.println(" ##");
break;
//........ and so on up to 9
and I was trying to change this switch into a more fluent while/for loop
The switch works fine, but changing to the for actually saved me some space.
I have allready switched some things to hardware to save additionally,
like the mosfet, is also hooked up to a pnp transistor to indicate if the motor is running,
while the mosfet is doing the motor controls.