aboutsummaryrefslogtreecommitdiff
path: root/libraries/EEPROM/examples/eeprom_pointer/eeprom_pointer.ino
blob: b56b6818785a1ed70ed800bcc1cc9220a64cf421 (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
74
/***
    eeprom_pointer example.
    
    This example shows how the built-in EEPtr
    object can be used to manipulate the EEPROM
    using standard pointer arithmetic.
    
    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() {

  Serial.begin(9600);

  /***
    In this example, we will iterate forward over the EEPROM,
    starting at the 10th cell (remember indices are zero based).
  ***/
  
  EEPtr ptr = 9;
  
  //Rather than hard coding a length, we can use the provided .length() function.

  while( ptr < EEPROM.length() ){
  
    Serial.print( *ptr, HEX );  //Print out hex value of the EEPROM cell pointed to by 'ptr'
    Serial.print( ", " );       //Separate values with a comma.
    ptr++;                      //Move to next cell
  }

  /***
    In this example, we will iterate backwards over the EEPROM,
    starting at the last cell.
  ***/
  
  ptr = EEPROM.length() - 1;
  
  do{
    
    Serial.print( *ptr, HEX );
    Serial.print( ", " );
    
  }while( ptr-- ); //When the pointer reaches zero the loop will end as zero is considered 'false'.
  
  
  /***
    And just for clarity, the loop below is an equivalent implementation
    of the C++11 ranged for loop.
  ***/
  
  for( EEPtr ptr = EEPROM.begin() ; item != EEPROM.end() ; ++item ){
    Serial.print( *ptr, HEX );
    Serial.print( ", " );
  }
  
  /*** 
    The actual C++11 version:
  
    for( auto ptr : EEPROM ){
      Serial.print( *ptr, HEX );
      Serial.print( ", " );
    }
  ***/
  

}

void loop(){}