EEPROM.ex Breaks Code

Hello,

I am using an ATTiny84 on a custom fidget spinner PCB (schematic image below code) as inspired by this Instructable. I am programming it with the SparkFun AVR Pocket Programmer, ATTinyCore, and a pogo pin adapter. The hall effect sensor is not being used. The code below simply debounces the input from a pushbutton, which advances a mode variable. If the mode variable is equal to 7, the code blinks the 8 LEDs in such a way that a lily pad pattern shows up while spinning the fidget spinner. This is written for counterclockwise pinmapping.

The code works until I include the three EEPROM.ex code sections (currently commented out). I have the library installed. When these code snippets are uncommented, the mode variable switches state randomly and all LEDs occasionally just shut off. The mode variable does not appear to be stored in EEPROM when you turn the board on and off.

Does anyone know what is going on? Any help would be appreciated.

Thank you,

Franklin

#ifdef PINMAPPING_CW
#error "Sketch was written for counterclockwise pin mapping!"
#endif

//#include <EEPROMex.h>

int LEDS[] = {10, 9, 8, 7, 6, 5, 4, 3};  // Port A counterclockwise

//int LEDS[] = {0, 1, 2, 3, 4, 5, 7, 6}; // Port A clockwise

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 0;   // the number of the pushbutton pin

// Variables will change:
int buttonState = HIGH;             // the current reading from the input pin
int mode = 0;                // the current selected LED to blink
int modes = 8;               // number of LEDs available
int lastButtonState = HIGH;  // the previous reading from the button input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  
  for(int LED=0; LED<(sizeof(LEDS)/sizeof(int)); LED++){
    pinMode(LEDS[LED], OUTPUT);
  }
  pinMode(buttonPin, INPUT_PULLUP); //enable internal pull up resistor

  /* get saved mode from eeprom
  mode = EEPROM.read(0);
  if (mode >= modes) {
    mode = 0; // may be very large first time
  }
  */

}

  
// the loop function runs over and over again forever
void loop(){

  checkButton();
  if (mode == 7) {
    lilyPad();
  }

}

void lilyPad() {

  for(int LED=0; LED<(sizeof(LEDS)/sizeof(int)); LED++){
       digitalWrite(LEDS[LED], HIGH); 
       delay(1); 
       digitalWrite(LEDS[LED], LOW); 
  } 
  for(int LED=(sizeof(LEDS)/sizeof(int))-1; LED >= 0; LED--){
       digitalWrite(LEDS[LED], HIGH); 
       delay(1); 
       digitalWrite(LEDS[LED], LOW); 
  }     

}

void blipLEDs(){
  // something to show we are alive
  for(int LED=0; LED<(sizeof(LEDS)/sizeof(int)); LED++){
    digitalWrite(LEDS[LED], HIGH); 
    delay(10); 
    digitalWrite(LEDS[LED], LOW); 
  } 
  for(int LED=sizeof(LEDS)/sizeof(int); LED>mode; LED--){
    digitalWrite(LEDS[LED], HIGH); 
    delay(10); 
    digitalWrite(LEDS[LED], LOW); 
  } 
  digitalWrite(LEDS[mode], HIGH);
}

void checkButton() {

  // read the state of the switch into a local variable:
  int currentButtonState = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from HIGH to LOW), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch goes LOW, due to noise or pressing:
  if (currentButtonState != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has truly changed (and is not just noise):
    if (currentButtonState != buttonState) {
      //set the button state
      buttonState = currentButtonState;

      //only increment mode if the new button state is LOW

      if (buttonState == LOW) {
        mode += 1;
        // if mode is greater than or equal to the total number of LEDs, reset mode to 0
        if (mode >= modes) {
          mode = 0;
        }
        //EEPROM.write(0, mode);
        blipLEDs();
      }
    }
  }
  //Save the current button reading. Next time through the loop, it will be the last button state.
  lastButtonState = currentButtonState;
}

The update, get and put functions of the included (with the IDE) EEPROM library write and read many data types. The put function will only write data that has changed to EEPROM, preserving write cycles.

What core are you using with the tiny84? I really like the ATTinyCore core. It is available via the boards manager of the IDE.

I am using ATTinyCore

As an update. I changed the code to use the EEPROM.h library instead and got it to work. The same issues occur though when I call the blipLEDs() function during setup. Calling it causes the mode variable to cycle sporadically, incremented by a push of the button or (more frequently) by touching anything metal on the circuit. Does anyone know why this may be?

Updated code below (with blipLEDs() in setup() commented out):

/*
  LED test
*/

#ifdef PINMAPPING_CW
#error "Sketch was written for counterclockwise pin mapping!"
#endif

#include <EEPROM.h>

int LEDS[] = {10, 9, 8, 7, 6, 5, 4, 3};  // Port A counterclockwise

//int LEDS[] = {0, 1, 2, 3, 4, 5, 7, 6}; // Port A clockwise

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 0;   // the number of the pushbutton pin

// Variables will change:
int buttonState = HIGH;             // the current reading from the input pin
int mode = 0;                // the current selected LED to blink
int modes = 8;               // number of LEDs available
int lastButtonState = HIGH;  // the previous reading from the button input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {

  for(int LED=0; LED<(sizeof(LEDS)/sizeof(int)); LED++){
    pinMode(LEDS[LED], OUTPUT);
  }
  pinMode(buttonPin, INPUT_PULLUP); //enable internal pull up resistor

  // get saved mode from eeprom
  mode = EEPROM.read(0);
  if (mode >= modes) {
    mode = 0; // may be very large first time
  }

  digitalWrite(LEDS[mode], HIGH); 
  
  // calling the below causes the mode variable to cycle sporadically, incremented by a push of the button or (more frequently) by touching anything metal on the circuit)
  //blipLEDs();

}

  
// the loop function runs over and over again forever
void loop(){

  checkButton();
  if (mode == 7) {
    lilyPad();
  }

}

void lilyPad() {

  for(int LED=0; LED<(sizeof(LEDS)/sizeof(int)); LED++){
       digitalWrite(LEDS[LED], HIGH); 
       delay(1); 
       digitalWrite(LEDS[LED], LOW); 
  } 
  for(int LED=(sizeof(LEDS)/sizeof(int))-1; LED >= 0; LED--){
       digitalWrite(LEDS[LED], HIGH); 
       delay(1); 
       digitalWrite(LEDS[LED], LOW); 
  }     

}

void blipLEDs(){
  // something to show we are alive
  for(int LED=0; LED<(sizeof(LEDS)/sizeof(int)); LED++){
    digitalWrite(LEDS[LED], HIGH); 
    delay(10); 
    digitalWrite(LEDS[LED], LOW); 
  } 
  for(int LED=sizeof(LEDS)/sizeof(int); LED>mode; LED--){
    digitalWrite(LEDS[LED], HIGH); 
    delay(10); 
    digitalWrite(LEDS[LED], LOW); 
  } 
  digitalWrite(LEDS[mode], HIGH);
}

void checkButton() {

  // read the state of the switch into a local variable:
  int currentButtonState = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from HIGH to LOW), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch goes LOW, due to noise or pressing:
  if (currentButtonState != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has truly changed (and is not just noise):
    if (currentButtonState != buttonState) {
      //set the button state
      buttonState = currentButtonState;

      //only increment mode if the new button state is LOW

      if (buttonState == LOW) {
        mode += 1;
        // if mode is greater than or equal to the total number of LEDs, reset mode to 0
        if (mode >= modes) {
          mode = 0;
        }
        EEPROM.write(0, mode);
        blipLEDs();
      }
    }
  }
  //Save the current button reading. Next time through the loop, it will be the last button state.
  lastButtonState = currentButtonState;
}