2

Hello i am making a game on an Arduino but i need to store 2 global variables that keep track of the score currScor and hiScor both of type uint32_t and to save ram space i marked them as uint32_t currScor PROGMEM = 0; once the game starts the score would begin incrementing but the compiler says i cannot assign to a constant variable. is there a way i can add to this variable that is stored in flash? should i consider using EEPROM although i am worried that too many writes will burn out that memory type.

Haris
  • 11,514
  • 6
  • 36
  • 63
blu
  • 281
  • 1
  • 5
  • 15

3 Answers3

1

The Arduino has 4 types of memory:

  1. Flash
  2. EEPROM
  3. SRAM
  4. CPU Registers

Manipulating Flash and EEPROM is not possible by simple assignments. For manipulation of EEPORM there exist well known libraries. Manipulation of Flash / Progmem is also possible from within a program. The technique is described in the ATMEL datasheet AVR105: Power Efficient High Endurance Parameter Storage in Flash Memory. However I am not aware of any open source library which supports this.

If you want to preserve RAM this is most probably not the way to go. Instead you should put strings and other large and constant stuff to progmem.

Udo Klein
  • 6,381
  • 1
  • 31
  • 53
1

For the score, you should use RAM instead since it's constantly updated and no need to retain it's value beyond single game cycle.

For hiscore, you can write it to EEPROM once the game is over. According to datasheet, it can be written at least 100,000 times, so if you play 100 times a day, 365 days a year, it won't worn out for almost 3 years. After that, simply change EEPROM location to another address. If you use Arduino Uno (with 1 Kb EEPROM), you have 32 different location to store an uint32_t data. 32 x 3 years = 96 years ;)

vcc2gnd
  • 129
  • 1
  • 5
0

Your intuition is correct, in that you will not be able to change the value that is stored in Program Memory space (aka Flash/ROM). If you are concerned about burning out the EEPROM. Have it only update periodically. However, you will still need to have RAM to buffer it.

I highly recommend conserving RAM else where. See the following examples library to measure and conserve RAM

mpflaga
  • 2,722
  • 2
  • 13
  • 19