aboutsummaryrefslogtreecommitdiff
path: root/libraries/EEPROM/examples/eeprom_iteration/eeprom_iteration.ino
blob: a2d825c2e537260a3cca069ae1e63a172580cb09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/***
    eeprom_iteration example.
    
    A set of example snippets highlighting the
    simplest methods for traversing the EEPROM.
    
    Running this sketch is not necessary, this is 
    simply highlighting certain programming methods.
    
    Written by Christopher Andrews 2015
    Released under MIT licence.
***/

#include <EEPROM.h>

void setup() {

  /***
    Iterate the EEPROM using a for loop.
  ***/
  
  for( int index = 0 ; index < EEPROM.length() ; index++ ){

    //Add one to each cell in the EEPROM
    EEPROM[ index ] += 1;
  }
  
  /***
    Iterate the EEPROM using a while loop.
  ***/
  
  int index = 0;
  
  while( index < EEPROM.length() ){
  
    //Add one to each cell in the EEPROM
    EEPROM[ index ] += 1;  
    index++;
  }
  
  /***
    Iterate the EEPROM using a do-while loop.
  ***/
  
  int idx = 0;  //Used 'idx' to avoid name conflict with 'index' above.
  
  do{
  
    //Add one to each cell in the EEPROM
    EEPROM[ idx ] += 1;  
    idx++;
  }while( idx < EEPROM.length() );
  
  /***
    Iterate the EEPROM using a C++11 ranged for loop.
    
    This version of the loop is best explained in the example 'eeprom_pointer'
    as this kind of iteration uses pointers rather than an index/integer.
    
    !! Note: C++11 is not yet enabled by default in any IDE version.
       Unless you manually enable it, this sketch will not compile.
       You can comment the loop below to verify the non C++11 content.
  ***/
  
  for( auto cell : EEPROM ){

    //Add one to each cell in the EEPROM
    cell += 1;
  }
  
} //End of setup function.

void loop(){}