eeprom2.cpp

This sketch uses four bytes of EEPROM to remember how many times the button has been pushed, even across power failures and reboots. It illustrates a way to do writing and reading of multibyte values to and from the EEPROM.

// Eeprom sketch 2: Count number of times the button has ever been
// pushed (while running this sketch).

#define DATA_ADDR 1024  // Any number in 0..eepromSize()-4 (since sizeof(n) is 4)

void setup() { /* Nothing to do */ }

void loop() { 
  if (buttonDown()) {
    u32 n;                                       // Scratch storage
    eepromRead(DATA_ADDR, (u8*) &n, sizeof(n));  // Read current value in EEPROM
    ++n;                                         // Increment it
    eepromWrite(DATA_ADDR, (u8*) &n, sizeof(n)); // Write back updated value
    println(n);                                  // Print new count
    while (buttonDown()) delay(1);               // Wait for button up
  }
}

Generated on Fri Apr 22 06:54:10 2011 for SFB by doxygen 1.5.9