Saving the counter value to mega 2560 memory

Hello guys,
I am working on a small project that uses a Photo Interrupter Slotted Optical Sensor to count, my question is can i keep the value of the counter saved when the power is off and reuse the same value when the power is back on?

You can save it to EEPROM.

BUT, EEPROM has a limited amount of write cycles. So if that value is changing a lot you will run out cycles pretty quick. Depending on the application you could for example only save it every hour, or when there is no change for x time. Or by having a shut down routine which you could trigger by monitoring the inout voltage but have a small cap to give it some time to save the value after the real power went out.

Thanks for your reply, EEPROM sounds like a good solution.
The project is a cash box with 3 slots for 3 different coin, when a coin is dropped the counter should add its value and display it. if the power goes off and then back on the value of the counter for each slot should be displayed with the existing amount.

Indeed sounds like an application you can save it to EEPROM every time. And you can extend the life a bit by using multiple addresses per coin counter because the limited write cycle is per address.