Coding Problem - Button Sequence replication on LED

Heyy guys, after all your suggestions in another post of mine that got totally congested, I tweaked and came up with this code. My problem is that the morseBtn sequence gets recorded only when the modeBtn is kept pressed.Can you help me tweak this code further to make sure the arduino starts recording the morseBtn sequence when the modeBtn is pressed and released ?

The actual question goes as follows: When the modeBtn is pressed, the arduino reads the status of the morseBtn. The morseBtn sequence is now recorded and when the modeBtn is now pressed again, the sequence gets played on the LED. The sequence on the LED now stops playing when the modeBtn is pressed again and the arduino gets ready to record the morseBtn sequence again.

byte morseBtn = 6;
byte modeBtn = 4;
byte led = 9;

boolean inRecordMode = false;
boolean wasInRecordMode = false;

int maxSamples = 100;
boolean states[100];
int durations[100];

int idxPlayback = 0;
int idxRecord = 0;
int sampleLength = 1; // 1 ms


void setup() {
 //Serial.begin(9600);  
 pinMode(modeBtn, INPUT);
 pinMode(morseBtn, INPUT);
 pinMode(led, OUTPUT);
}

void loop() {
 inRecordMode = digitalRead(modeBtn);
 if(inRecordMode == true) {
   if(!wasInRecordMode) {
     // reset record buffers
     memset(states, 0, sizeof(states));
     memset(durations, 0, sizeof(durations));
     idxRecord = 0; // reset record idx just to make playback start point 
obvious
   }
   recordLoop();
 } else {
   playbackLoop();
 }

 wasInRecordMode = inRecordMode; // record prev state for next iteration so 
we know whether to reset the record arr index
}

void recordLoop() {
 boolean state = digitalRead(morseBtn);
 digitalWrite(led, state); // give feedback to person recording the loop

 if(states[idxRecord] == state) {
   // state not changed, add to duration of current state
   durations[idxRecord] += sampleLength;
 } else {
   // state changed, go to next idx and set default duration
   idxRecord++;
   if(idxRecord == maxSamples) { idxRecord = 0; } // reset idx if max array 
size reached
   states[idxRecord] = state;
   durations[idxRecord] = sampleLength;
 }

     delay(sampleLength); // slow the loop to a time constant so we can 
   reproduce the timelyness of the recording
   }

void playbackLoop()
{
 // play the loop back at the desired speed
 digitalWrite(led, states[idxPlayback]); // set led
 delay(durations[idxPlayback]; // leave led in that state for duration

 idxPlayback++;
 if(idxPlayback == maxSamples) { idxPlayback=0; } // repeat the recorded 
loop
}

Please modify your post and insert [code] before the first line of code and [/code] after the last line, so it looks like this:

 This is my code

Thank you for the suggestion @FredScuttle. This looks presentable now.

I've tried altering the code to meet my above stated requirements, Can someone check this out and see if i'm doing this correctly ?

byte morseBtn = 6;
byte modeBtn = 4;
byte led = 9;

boolean inRecordMode = false;
boolean wasInRecordMode = false;

int maxSamples = 100;
boolean states[100];
int durations[100];

int idxPlayback = 0;
int idxRecord = 0;
int sampleLength = 1; // 1 ms
int modeBtnPushCounter =0;
int modeBtnState=0;
int lastmodeBtnState=0;


void setup() {
  //Serial.begin(9600);  
  pinMode(modeBtn, INPUT);
  pinMode(morseBtn, INPUT);
  pinMode(led, OUTPUT);
}

void loop() {
  modeBtnState = digitalRead(modeBtn);
  if(modeBtnState != lastmodeBtnState){
    if(modeBtnState == HIGH){
      
    modeBtnPushCounter++ ;
    }
    delay(50);
   }
    lastmodeBtnState = modeBtnState;
  
  if( modeBtnPushCounter%2 != 0 )
  {
    inRecordMode == true;
  }
 // inRecordMode = digitalRead(modeBtn);
  if(inRecordMode == true) {
    if(!wasInRecordMode) {
      // reset record buffers
      memset(states, 0, sizeof(states));
      memset(durations, 0, sizeof(durations));
      idxRecord = 0; // reset record idx just to make playback start point 
//obvious
    }
    recordLoop();
  } else if(modeBtnPushCounter%2 == 0)
   {
    playbackLoop();
   }

  wasInRecordMode = inRecordMode; // record prev state for next iteration so 
//we know whether to reset the record arr index
}

void recordLoop() {
  boolean state = digitalRead(morseBtn);
  digitalWrite(led, state); // give feedback to person recording the loop

  if(states[idxRecord] == state) {
    // state not changed, add to duration of current state
    durations[idxRecord] += sampleLength;
  } else {
    // state changed, go to next idx and set default duration
    idxRecord++;
    if(idxRecord == maxSamples) { idxRecord = 0; } // reset idx if max array 
//size reached
    states[idxRecord] = state;
    durations[idxRecord] = sampleLength;
  }

      delay(sampleLength); // slow the loop to a time constant so we can 
//    reproduce the timelyness of the recording
    }

void playbackLoop()
{
  // play the loop back at the desired speed
  digitalWrite(led, states[idxPlayback]); // set led
  delay(durations[idxPlayback]); // leave led in that state for duration

  idxPlayback++;
  if(idxPlayback == maxSamples) { idxPlayback=0; } // repeat the recorded 
//loop
}

Couple of remarks:

Do you have a pull down resistor on the button? You do know it'easier to just connect the button between a pin and GND and use INPUT_PULLUP?

No need for memset. With 'idxRecord' you know how many records you have. Just overwrite the old value. No need to "clear" it.

No need to count mode button presses. Simply toggle between recording or not (with 'inRecordMode').

No need for 'wasInRecordMode'. If you are not 'inRecordMode' and you have recorded stuff ('idxRecord' > 0) ou already know that ;)

You know it's a lot easier to use millis() to keep track of time instead of delay() with a 'sampleLength'? And if you use millis() for playback as well you're also able to act on button presses will doing so. See Blink without delay.

And I like to simplify buttons by using a library (Bounce2). After you understand the basics of buttons (state change and debounce) it is just easier that way.

And the 'states' array is redundant... You simply know that after the duration has passed the state flips. Aka, you know just store {true, false, true, false, true, false...} in the states array :D

I would suggest you give my recommendations a try and come back if you're stuck. I think it's easier to start over. Yes, that seems harsh, but you tend to get stuck in a way of thinking when you try to recycle code. Just see it as version 2.0 ;)