I can't seem to find an answer for this anywhere, and its surprisingly difficult to do a search on ...
I have a project thats several thousand lines compiling to 18280 bytes (56%) using 1263 bytes for global variables (61%)
All on a 328p on a custom PCB.
The area i'm having problems has not changed in months - hardware or firmware), but suddenly started doing weird things after tweaking some things in unrelated functions.
Within one function that displays data on a screen, I have:
const byte _dataX = 53;
const byte _dataY[] = {13, 26, 39, 63}; // Y value for screen lines
// These are later used in a for loop
for (byte i = 0; i <= (RFNodes - 1); i++)
{
OLED.setCursor(0, _dataY[i]); // Set cursor to prepare for write
OLED.print((char *)rmName[i]); // Print room name, char array
OLED.setCursor(_dataX, _dataY[i]); // Set cursor to prepare for write
OLED.print(data[i]); // Print Data
// other stuff that works fine
}
When I added some unrelated code that tracks data cycles in a single byte (allowing it to roll over after 255), the value stored in/as _dataY[0] started matching that counting byte.
I took me a long time to track down what was happening, as I didn't think a const value could be changed.
I attempted a:
_dataY[0] = 13;
just to see what would happen, and I got the expected error:
exit status 1
assignment of read-only location '_dataY[0]'
The only thing I can think of is that I'm somehow using all my RAM space and the constant happens to be the address that is being overwritten ??
Is that possible? I'm not sure what the memory would do in that case.
The only way I was able to track down the problem was because the data supposed to be displayed was slowly stepping down the screen and I happened to notice it corresponded with the counter increments. (based on outside info coming in via interrupt, so it isn't regular)
Any ideas?
I can't see the full code helping here, as it's a fundamental level issue - Why is my declared 'Const' not acting as a constant??
I tried moving the declarations to a global level and everything is working again, so for the time being, I'll try freeing up more memory - I have to get better at not using globals anyway. ![]()
But, if anyone can explain what's happening &/or a better solution, it would be appreciated.