Data Storage
When you give values to variables, the Arduino will only remember those values as long as the microcontroller has power. When you disconnect the board, your data is lost. But, as you may have guessed, there is a way to hang on to that data.
Constants
If the data you want to store never changes, then you can set up that data each time the Arduino starts. But if you need to save space you might want to use the 32K of flash memory. The PROGMEM Directive allows you to store data in the flash memory.
To use it you need to include the avr/pgmspace.h library.
When using PROGMEM, you have to use certain data types. What this means is that you cannot use an array of char arrays:
#include <avr/pgmspace.h>
prog_char color_0[] PROGMEM = "Red";
prog_char color_1[] PROGMEM = "Green";
prog_char color_2[] PROGMEM = "Blue";
prog_char color_3[] PROGMEM = "Yellow";
prog_char color_4[] PROGMEM = "Magenta";
prog_char color_5[] PROGMEM = "Cyan";
prog_char color_6[] PROGMEM = "White";
PROGMEM const char *color_table[] ={
color_0,color_1,color_2,color_3,color_4,color_5, color_6
};
char c_buffer[8];
#define btn 2
void setup(){
pinMode(btn, INPUT);
}
void loop(){
if(digitalRead){
//this uses a buffer variable where the string is copied to so that it can be used as a regular char array);
strcpy_P(c_buffer, (char*)pgm_read_word(&(color_table[random(7)])));
Serial.println(c_buffer);
}else{
Serial.println("");
}
}
Using PROGMEM only works if the data is constant— not going to change during your program.
Make a toy that changes color based on some interaction.
EEPROM
The ATMega328 chip has a kilobyte of electrically erasable read-only memory (EEPROM). This type of memory is writable and is designed to remember its contents.
#include <EEPROM.h>
int addr=0;
char ch;
void setup(){
Serial.begin(9600);
ch=EEPROM.read(addr);
}
void loop(){
if(Serial.available()>0){
ch=Serial.read();
EEPROM.write(0,ch);
Serial.println(ch);
}
Serial.println(ch);
delay(1000);
}
If you run this sketch, enter a single byte (an 8-bit char is ok, but not a 16 bit int), unplug the Arduino, plug it back in and run the sketch again, you'll see that the Arduino remembered what you previously entered.
Storing an int
Create a tab and name it
EEPROMAnything.h
/*
* EEPROM Write
*
* Stores values read from analog input 0 into the EEPROM.
* These values will stay in the EEPROM when the board is
* turned off and may be retrieved later by another sketch.
*/
#include <EEPROM.h>
// the current address in the EEPROM (i.e. which byte
// we're going to write to next)
int addr = 0;
int btnRead=2;
int btnClear=3;
int btnSave=4;
byte value;
void setup(){
pinMode(btnRead,INPUT);
pinMode(btnClear,INPUT);
pinMode(btnSave,INPUT);
readVal();
}
void loop(){
// need to map because analog inputs range from
// 0 to 1023 and each byte of the EEPROM can only hold a
// value from 0 to 255.
int value = map( analogRead(0), 0, 1023, 0, 255);
if(digitalRead(btnSave)){
saveVal();
}
if (digitalRead(btnRead)){
readVal();
}
if (digitalRead(btnClear)){
clearVal();
}
}
void readVal(){
value = EEPROM.read(addr);
Serial.print(addr);
Serial.print("\t");
Serial.print(value, DEC);
Serial.println();
// advance to the next address of the EEPROM
addr = addr + 1;
// there are only 512 bytes of EEPROM, from 0 to 511, so if we're
// on address 512, wrap around to address 0
if (addr == 512)
addr = 0;
delay(500);
}
void clearVal(){
for (int i = 0; i < 512; i++)
EEPROM.write(i, 0);
}
}
void saveVal(){
// write the value to the appropriate byte of the EEPROM.
// these values will remain there when the board is
// turned off.
EEPROM.write(addr, value);
// advance to the next address. there are 512 bytes in
// the EEPROM, so go back to 0 when we hit 512.
addr = addr + 1;
if (addr == 512)
addr = 0;
}
Create a toy that takes advantage of EEPROM