EEPROM library enables you to read and write those bytes only. The various Arduino and Genuino boards have different amounts of EEPROM: 1024 bytes on the ATmega328P, 512 bytes on the ATmega168 and ATmega8, 4 KB (4096 bytes) on the ATmega1280 and ATmega2560. The Arduino and Genuino 101 boards have an emulated EEPROM space of 1024 bytes.
/****************************************************** |
Arduino EEPROM Read-Write Test |
by Ted Hayes 2012 |
[email protected] |
Demonstrates the usage of the EEPROM Library for checking the state of a single value, |
changing it, and resetting it. To use: |
1) Put a momentary switch between ground and pin 11 on your Arduino. |
2) Upload this program to it. |
3) Open the serial monitor |
4) You should see: |
Starting; current EEPROM value is 0 |
EEPROM byte not set yet; Writing... |
Done. |
5) Now press the reset button on your Arduino and keep the serial monitor open. |
You should see: |
Starting; current EEPROM value is 1 |
EEPROM byte was set! |
Done. |
6) Press your momentary switch, and you should see: |
Resetting EEPROM value |
7) Now press the reset button one more time, and you should see: |
Starting; current EEPROM value is 0 |
EEPROM byte not set yet; Writing... |
Done. |
That's it! Remember that the EEPROM on ATmega chips has a specified life of 100,000 |
write/erase cycles, so be careful about putting writes/erases in loops, etc.. |
******************************************************/ |
#include<EEPROM.h> |
#definePIN_SWITCH11 |
int lastSwitch = HIGH; |
boolean didReset = false; |
voidsetup(){ |
Serial.begin(9600); |
pinMode(PIN_SWITCH, INPUT); |
digitalWrite(PIN_SWITCH, HIGH); // set internal pull-up resistor |
int val = EEPROM.read(0); |
Serial.print('Starting; current EEPROM value is '); |
Serial.println(val); |
if(val != 1){ |
Serial.println('EEPROM byte not set yet; Writing...'); |
EEPROM.write(0, 1); |
} elseif (val 1){ |
Serial.println('EEPROM byte was set!'); |
} |
Serial.println('Done.'); |
} |
voidloop(){ |
int currentSwitch = digitalRead(PIN_SWITCH); |
if(currentSwitch LOW && lastSwitch != currentSwitch && !didReset){ |
// switch has changed state |
// didReset is a simple form of debouncing, which prevents |
// this from being run too many times |
Serial.println('Resetting EEPROM value'); |
EEPROM.write(0, 0); |
didReset = true; |
} |
} |