Store array to eeprom help

So, when powered up after recording and removing power, the device just triggers every 2 seconds and nothing else. Any idea where I went wrong? I am still learning the whole eeprom thing.

#include <EEPROM.h>

byte trigBtn = 1;      // Assign pin as the Button that is your input to record the sequence.
byte recBtn  = 2;      // Assign pin as the Button that you will push and hold for RECORDING.
byte pin_d5   = 4;      // Assign pin d5 as a pin that will do digital output.

boolean inRecordMode = false;     // Create a boolean variable that will hold current  state of the Record mode.
boolean wasInRecordMode = false;  // Create a boolean variable that will hold previous state of the Record mode.

// buffers to record state/duration combinations
int maxSamples = 100;   // This is the maximum number of samples in bytes that can be stored.
boolean states[100];    // Create a states array to record up to this number of on and off states.
int durations[100];     // Create a durations array and we can have up to this many duration times.

int idxPlayback  = 0;    // Initialize the index for the Playback array to 0
int idxRecord    = 0;    // Initialize the index for the Record array to 1
int sampleLength = 50;   // 50 ms

void setup() {
  //Serial.begin(9600);        // Uncomment this line for setup to use the Serial Monitor for troubleshooting your software changes.
  pinMode(recBtn, INPUT);      // Setup the mode Button as an INPUT
  pinMode(trigBtn, INPUT);    // Setup the input Button as an INPUT
  pinMode(pin_d5, OUTPUT);     // Setup the pin d5 pin_d5 as an OUTPUT

  if ( EEPROM.read ( 0 ) != 0xff )
  for (int i = 0; i < 100; ++i )
    states [ i ] = EEPROM.read ( i );
  for (int i = 0; i < 100; ++i )
    durations [ i ] = EEPROM.read ( i );
}

void loop() {    
  inRecordMode = digitalRead(recBtn);                // read the mode button 
  if(inRecordMode == true) {                          // test if the mode button is pressed in record mode
    if(!wasInRecordMode) {                            // if the button was not pressed in record mode, reset the arrays and index for playback
      // reset record buffers
      memset(states, 0, sizeof(states));        // Set the size of the states array
      memset(durations, 0, sizeof(durations));  // Set the size of the durations array
      idxRecord = 0;     // reset record idx just to make playback start point obvious
    }
    recordLoop();        // perform the recording loop function
  } else if(wasInRecordMode) {                            // if the button was not pressed in record mode, reset the arrays and index for playback
    for ( int i = 0; i < 100; ++i )
    EEPROM.write ( i, states [ i ] );
    for ( int i = 0; i < 100; ++i )
    EEPROM.write ( i, durations [ i ] );
  } else {               // or else
    playbackLoop();      // perform the playback loop function
  }
  
  wasInRecordMode = inRecordMode; // record prev state for next iteration so we know whether to reset the record array index
}

void recordLoop() {
  boolean state = digitalRead(trigBtn); // read the state of the input sequence recorder button.
  digitalWrite(pin_d5, state);    // change the state of the output with the input to give feedback to person recording the loop
  
  if(states[idxRecord] == state) {   
    // if the state is not changed, add to duration of current state
    durations[idxRecord] += sampleLength;
  } else {
    // if the state is changed, go to next index (idx) and set default duration of 1ms
    idxRecord++;
    if(idxRecord == maxSamples) { idxRecord = 0; } // reset idx if max array size reached
    states[idxRecord] = state;                     // save the state to the states array by index
    durations[idxRecord] = sampleLength;           // save the duration of the sample to the durations array by index
  }
  
  delay(sampleLength); // slow the loop to a time constant so we can reproduce the timelyness of the recording
}

void playbackLoop() {
  digitalWrite(pin_d5, states[idxPlayback]);       // playback output to pin_d5
  delay(durations[idxPlayback]);                   // leave the output pin in that state for duration
  
  idxPlayback++;                                   // increment the playback index so we can play the next value
  if(idxPlayback == maxSamples) { idxPlayback=0; } // repeat the recorded loop when we reach the maximum number of samples
}

If you are reading/writing the whole array, use get() and put(). As durations is an array of ints you'll need to read two bytes for each element. Also you start from the same address for both the states and durations arrays, so you are reading the same data into the arrays.

int array1[100];
bool array2[100];

//Write array1
EEPROM.put( 0, array1 );

//Read array1
EEPROM.get( 0, array1 );

//Create address offset so Array2 is located after Array1 in EEPROM
int address = sizeof(array1);

//Write array2
EEPROM.put( address , array2 );

//Read array2
EEPROM.get( address , array2 );

//----------------

//Read a single int variable into the array element:
EEPROM.get( location, array1[0] );

//Read a single byte variable into the array element:
EEPROM.read( location, array2[0] );
1 Like

int address = sizeof(array1);

//Write array2
EEPROM.put( address , array2 );

OOOOHHHHH! Dang. Thank you! I didn't even think about the location of where it was saving.