aboutsummaryrefslogtreecommitdiff
path: root/libraries/EEPROM/examples/eeprom_read
diff options
context:
space:
mode:
authorChris--A <chris@genx.biz>2015-03-20 12:06:20 +1000
committerChris--A <chris@genx.biz>2015-03-20 12:06:20 +1000
commit26577474efcb8874dad687467a8ba2f01678ff4c (patch)
tree4eb3a3a9dc0e6e8b8a264676be14b1f0aa362070 /libraries/EEPROM/examples/eeprom_read
parentfd4323f360885725c736c90745ee57dbca20e8a0 (diff)
Updated EEPROM examples.
Removed hard coded lengths, which were incorrect for standard Arduino's now.
Diffstat (limited to 'libraries/EEPROM/examples/eeprom_read')
-rw-r--r--libraries/EEPROM/examples/eeprom_read/eeprom_read.ino28
1 files changed, 21 insertions, 7 deletions
diff --git a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino
index ebf79d6..8567ed7 100644
--- a/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino
+++ b/libraries/EEPROM/examples/eeprom_read/eeprom_read.ino
@@ -31,13 +31,27 @@ void loop()
Serial.print(value, DEC);
Serial.println();
- // advance to the next address of the EEPROM
- address = address + 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 (address == 512)
- address = 0;
+ /***
+ Advance to the next address, when at the end restart at the beginning.
+
+ Larger AVR processors have larger EEPROM sizes, E.g:
+ - Arduno Duemilanove: 512b EEPROM storage.
+ - Arduino Uno: 1kb EEPROM storage.
+ - Arduino Mega: 4kb EEPROM storage.
+
+ Rather than hard-coding the length, you should use the pre-provided length function.
+ This will make your code portable to all AVR processors.
+ ***/
+ addr = addr + 1;
+ if(addr == EEPROM.length())
+ addr = 0;
+
+ /***
+ As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
+ EEPROM address is also doable by a bitwise and of the length - 1.
+
+ ++addr &= EEPROM.length() - 1;
+ ***/
delay(500);
}