source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/hardware/arduino/avr/libraries/EEPROM/examples/eeprom_write/eeprom_write.ino @ 4837

Last change on this file since 4837 was 4837, checked in by daduve, 2 years ago

Adding new version

File size: 1.4 KB
Line 
1/*
2 * EEPROM Write
3 *
4 * Stores values read from analog input 0 into the EEPROM.
5 * These values will stay in the EEPROM when the board is
6 * turned off and may be retrieved later by another sketch.
7 */
8
9#include <EEPROM.h>
10
11/** the current address in the EEPROM (i.e. which byte we're going to write to next) **/
12int addr = 0;
13
14void setup() {
15  /** Empty setup. **/
16}
17
18void loop() {
19  /***
20    Need to divide by 4 because analog inputs range from
21    0 to 1023 and each byte of the EEPROM can only hold a
22    value from 0 to 255.
23  ***/
24
25  int val = analogRead(0) / 4;
26
27  /***
28    Write the value to the appropriate byte of the EEPROM.
29    these values will remain there when the board is
30    turned off.
31  ***/
32
33  EEPROM.write(addr, val);
34
35  /***
36    Advance to the next address, when at the end restart at the beginning.
37
38    Larger AVR processors have larger EEPROM sizes, E.g:
39    - Arduno Duemilanove: 512b EEPROM storage.
40    - Arduino Uno:        1kb EEPROM storage.
41    - Arduino Mega:       4kb EEPROM storage.
42
43    Rather than hard-coding the length, you should use the pre-provided length function.
44    This will make your code portable to all AVR processors.
45  ***/
46  addr = addr + 1;
47  if (addr == EEPROM.length()) {
48    addr = 0;
49  }
50
51  /***
52    As the EEPROM sizes are powers of two, wrapping (preventing overflow) of an
53    EEPROM address is also doable by a bitwise and of the length - 1.
54
55    ++addr &= EEPROM.length() - 1;
56  ***/
57
58
59  delay(100);
60}
Note: See TracBrowser for help on using the repository browser.