Not enough Arduino storage

Dear All,

I am working on a project using an Arduino Uno. My code however uses 107% storage space. I have done my best to decrease the size of the code but I am relatively new to Arduino therefore do not know all the tricks.

Could anyone help out with decreasing the program size before I invest in a mega (which is unhandy due to size restrictions).

RFID_AccessControl_Change.ino (40.1 KB)

The problem is that you include too many libraries. They use most of the memory.

It is possible to squeeze a few bytes out of your sketch, but I don't know if that is enough.

Keep this sketch, save it somewhere or give it another name.
Maybe you have to make a few small test-sketches as well.

You use Serial.println(F("...")), that is good, but there are also without the "F()" macro.

The texts are long, you could shorten the texts. For example "Error100" instead of "Did not find fingerprint sensor :(".

The "lcd.print()" does not use the "F()" macro. Can you try that ? I think it should work.

You could use "#ifdef" to mark sections that you can include or exclude. For example all the Serial port usage or all the Nextion usage or all the led usage. Then you can test every section and in the end when it works you might omit the Serial port for the final result.

To blink a led, sometimes a for-loop is shorter.
I think there is always one led on or no leds on, that could be a function. Then you don't have to make a call for all three leds.

the F("") macro will not save flash space... just SRAM. if the current issue is Flash memory, that's not where to look..

shortening the texts is definitely one option and use error numbers (a byte is enough to represent 255 errors) rather than any text at all. get rid of all the debug stuff or use just an increasing number to keep track of where you are.

Your buttons use each many bytes for their names.

NexButton nine = NexButton(0, 3, "b0");
NexButton eight = NexButton(0, 2, "b1");
NexButton seven = NexButton(0, 1, "b2");
NexButton six = NexButton(0, 6, "b3");
NexButton five = NexButton(0, 5, "b4");
...

sure it's more readable but if memory is short then make it smaller. "A", "B", "C", ... you have plenty letters to choose from :slight_smile: . that will help with SRAM too

I'd move the whole solution to a MEGA (or mini Mega) and you'll get extra Serial Hardware ports that will be better rather than using Software Serial...

PS: you have twice   Serial.begin(9600);that's weird...

There are about 140 texts in the sketch. I guess an average of 15 characters, that makes a total of 2kbyte. You can easily reduce that to 1kbyte, but that is not enough to reduce 7%.
I agree with J-M-L, the Nextion display will work better with a hardware serial port. An Arduino Mega 2560 clone is 8 dollars.

Which Nextion library are you using?

X4ND3R:
Could anyone help out with decreasing the program size before I invest in a mega (which is unhandy due to size restrictions).

If that's really the only problem, there are other boards out there that are smaller with more memory. There are even some smaller form factor Mega's.

Is your problem with program storage space or dynamic memory? I must be doing something different here, looks like plenty of program storage space and a lack of dynamic memory (which is helped greatly by using the F() macro on all print statements).

I think there are global opportunities for code size reduction here. 40kb is more than I care to plough through entirely, but when I find things like:

///////////////////////////////////////// Access Denied  ///////////////////////////////////
void denied() {
  digitalWrite(greenLed, LED_OFF);  // Make sure green LED is off
  digitalWrite(blueLed, LED_OFF);   // Make sure blue LED is off
  digitalWrite(redLed, LED_ON);   // Turn on red LED
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("acces denied");
  delay(1000);
  lcd.clear();
}

///////////////////////////////////////// Switch Finger ////////////////////////////////////
void switchFinger() {
  digitalWrite(greenLed, LED_OFF);
  digitalWrite(blueLed, LED_ON);
  digitalWrite(redLed, LED_ON);
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("ready to learn");
  delay(1000);
  lcd.clear();
}

I see a failure to factor code. Look at the duplication in these functions - go through your entire sketch and look for more examples of this. Then design functions that differ entirely except for the relevant parameters that change - in the above example everything is the same except for the text message and LED color. So you should design ONE function that you pass text and colors to, instead of all that duplication.