There are lots of repetitions in your code. This is something you should
generally avoid, not only because of memory usage, but also, and most
importantly, because of maintainability.
The most common way of getting rid of repetitions is to write a loop
that walks through an array. For the first part of your program (the
rack/gap scheme), that array could look like this:
// Data structure describing a recorded location.
struct Location {
uint16_t id;
uint8_t rack;
uint8_t gap;
};
// List of recorded product locations.
const Location locations[] = {
{ 9, 109, 63 },
{ 15, 109, 63 },
{ 20, 109, 63 },
{ 22, 110, 68 },
{ 23, 109, 63 },
// etc...
};
// Number of items in the above list.
const size_t nb_locations = sizeof locations / sizeof locations[0];
Note that the structure uses the smallest possible integers for each
field, in order to save memory. The main benefit, however, is that the
data is now in a format that is easier to update: a simple list of
numbers.
Then, all the code that handles this data becomes a loop, instead of a
repetition. For example, the following function can print the location
of any item on the LCD. It returns true or false to let you know
whether it did find the requested item:
// If the item is found, print its location and return true.
// Otherwise return false.
bool print_location(uint16_t id) {
for (size_t i = 0; i < nb_locations; i++) {
if (locations[i].id == id) {
IDScrn();
lcd.print(locations[i].rack);
lcd.setCursor(12,1);
lcd.print(locations[i].gap);
delayfunc();
return true; // found
}
}
return false; // not found
}
This scheme still has the problem that all the data is copied to RAM at
initialization. If you have 4,000 items, that would require
16,000 bytes of RAM, or about twice the RAM of your Mega. You can
save RAM by keeping all constant data in flash, using the PROGMEM
qualifier. You have 32 times more flash than RAM. You would then
need, however, to bring each item back to RAM in order to use it, which
can be done with the memcpy_P() function. Here is a version of the
previous code that stores the array in flash:
// List of recorded product locations.
PROGMEM const Location locations[] = {
{ 9, 109, 63 },
{ 15, 109, 63 },
{ 20, 109, 63 },
{ 22, 110, 68 },
{ 23, 109, 63 },
// etc...
};
// Number of items in the above list.
const size_t nb_locations = sizeof locations / sizeof locations[0];
// If the item is found, print its location and return true.
// Otherwise return false.
bool print_location(uint16_t id) {
for (size_t i = 0; i < nb_locations; i++) {
Location location;
memcpy_P(&location, &locations[i], sizeof location);
if (location.id == id) {
IDScrn();
lcd.print(location.rack);
lcd.setCursor(12,1);
lcd.print(location.gap);
delayfunc();
return true; // found
}
}
return false; // not found
}
This approach should enable you to store all the database in the flash
of your Mega, and maybe even on an Uno. That being said, I second the
commenters that say that using an SD card should make upgrading the
database a lot more convenient.