Storing variables in flash memory with sketch

Adafruit's tiny microcontroller platform. Please tell us which board you are using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
tcarey
 
Posts: 29
Joined: Sun Jan 18, 2015 1:06 pm

Storing variables in flash memory with sketch

Post by tcarey »

Is there a way to save some variable values in the flash memory along with the sketch in the pro trinket and then when the sketch loads the variables would be filled with that value? Only need to store maybe six integers. Or would I have to pay into something like SD cards?

User avatar
adafruit_support_bill
 
Posts: 88155
Joined: Sat Feb 07, 2009 10:11 am

Re: Storing variables in flash memory with sketch

Post by adafruit_support_bill »

You can't write to the Flash from your program, but you can write to EEPROM. An example of that can be seen here:
https://learn.adafruit.com/sous-vide-po ... stent-data

tcarey
 
Posts: 29
Joined: Sun Jan 18, 2015 1:06 pm

Re: Storing variables in flash memory with sketch

Post by tcarey »

Does pro trinket have EEPROM built in?

User avatar
adafruit_support_bill
 
Posts: 88155
Joined: Sat Feb 07, 2009 10:11 am

Re: Storing variables in flash memory with sketch

Post by adafruit_support_bill »

Yes. It has 1K - same as the UNO.

tcarey
 
Posts: 29
Joined: Sun Jan 18, 2015 1:06 pm

Re: Storing variables in flash memory with sketch

Post by tcarey »

I just watch a video that said EEPROM Wears out after so many reads and writes?
Also can I save a value like 1500

User avatar
Funman1
 
Posts: 376
Joined: Fri Nov 28, 2014 2:13 am

Re: Storing variables in flash memory with sketch

Post by Funman1 »

Yes you can easily put that number in there.
It's good for 100,000 writes and unlimited reads.
So unless you are data logging 100,000 will go a long way

http://arduino.cc/en/Reference/EEPROMWrite

tcarey
 
Posts: 29
Joined: Sun Jan 18, 2015 1:06 pm

Re: Storing variables in flash memory with sketch

Post by tcarey »

I just want to store the speed settings for two linear actuators that need to be in sync. In case the speeds vary in the 2 actuators. And also slow it down with a third number. And then possibly one or two additional timing numbers. So probably 5. So that would be 5 writes? So that's why they compare if it changes and only save if different.

tcarey
 
Posts: 29
Joined: Sun Jan 18, 2015 1:06 pm

Re: Storing variables in flash memory with sketch

Post by tcarey »

I just read something by Lady Ada that said you have to be careful when you save to the flash memory so you don't overwrite your program. So must be able to be done. I'm trying to use a display screen, infrared receiver, and 2 hour controller pins, and then would like SD memory. I don't think I would have enough pins. Is there a way to share pins. What is the lowest number of pin displays not counting ground and 5 volts?

User avatar
hiduino
 
Posts: 862
Joined: Sat Sep 01, 2012 7:05 pm

Re: Storing variables in flash memory with sketch

Post by hiduino »

Flash memory and EEPROM memory are totally separate areas, so don't confuse the two. You should be fine reading and writing to EEPROM memory. That will save you some pins.

tcarey
 
Posts: 29
Joined: Sun Jan 18, 2015 1:06 pm

Re: Storing variables in flash memory with sketch

Post by tcarey »

Thank you all

User avatar
hiduino
 
Posts: 862
Joined: Sat Sep 01, 2012 7:05 pm

Re: Storing variables in flash memory with sketch

Post by hiduino »

The default EEPROM library only supports byte size variables, values from 0 to 255. For your needs you need to be able to support word size variables (unsigned int), values from 0 to 65535.

Here is an example to extend the default EEPROM library to support word size variables.

Code: Select all

/*
 * EEPROM Word read/write example
 */

#include <EEPROM.h>

// EEPROM location addresses should be multiples of 2 for Word size variables
int value1_addr = 0;
int value2_addr = 2;
int value3_addr = 4;
int value4_addr = 6;

// A word stores a 16-bit unsigned number, from 0 to 65535. Same as an unsigned int.
word value1, value2;

// Default to no EEPROM updates, to save EERPOM write life.
int update = false;

void setup()
{
}

void loop()
{
  // read a word from the value1 address of the EEPROM
  value1 = readWord(value1_addr);

  // read a word from the value2 address of the EEPROM
  value2 = readWord(value2_addr);

  // Process stuff...
  if (value1 < 1500) {
    value1 = 1500;
    value2 = 3000;
    update = true;
  }

  // You only want to write an update if necessary, to save EEPROM life.
  if (update) {
    update = false;
    // write a word to the value1 address of the EEPROM
    writeWord(value1_addr, value1);

    // write a word to the value2 address of the EEPROM
    writeWord(value2_addr, value2);
  }
}

void writeWord(int addr, word value)
{
  EEPROM.write(addr, highByte(value));
  EEPROM.write(addr+1, lowByte(value));
}

word readWord(int addr)
{
   return word(EEPROM.read(addr), EEPROM.read(addr+1));
}

tcarey
 
Posts: 29
Joined: Sun Jan 18, 2015 1:06 pm

Re: Storing variables in flash memory with sketch

Post by tcarey »

hiduino does this use to two memory locations per word? Does each word count as two saves when saved? Also thank you much

User avatar
hiduino
 
Posts: 862
Joined: Sat Sep 01, 2012 7:05 pm

Re: Storing variables in flash memory with sketch

Post by hiduino »

Yes, it is two bytes per word. Each save is one byte, so it requires two saves.

tcarey
 
Posts: 29
Joined: Sun Jan 18, 2015 1:06 pm

Re: Storing variables in flash memory with sketch

Post by tcarey »

hiduino, I adjusted my code using the map function, so I don't have to use more space and more saving. I only need to save up to a int value of 100. And since there are unlimited reads, I'm going to compare the already saved data to the data I want to save and only save if it has changed in value.

Can you save a float?

User avatar
adafruit_support_bill
 
Posts: 88155
Joined: Sat Feb 07, 2009 10:11 am

Re: Storing variables in flash memory with sketch

Post by adafruit_support_bill »

I only need to save up to a int value of 100.
In that case, you only need to save a single byte. A byte can hold a value up to 255.
Can you save a float?
Yes. Please see my initial response to this thread.
There is a link to some code that loads and stores floating point values:
https://learn.adafruit.com/sous-vide-po ... stent-data

Locked
Please be positive and constructive with your questions and comments.

Return to “Trinket ATTiny, Trinket M0”