Write and read a sine wave simultaneously

So I'm trying to generate a signal through DAC1 and instantly read it with an analog pin, multiple times.

I can't get it to work at all and I don't know if I'm doing something wrong or is an issue with timings within the arduino, but it crashes whenever I press the button that activates this process.

Here is the code:


#include <Arduino_AdvancedAnalog.h>
#include <Arduino.h>

#define N_SAMPLES (4096)
#define SAMPLINGFREQ (96000)
const float freq[19] = {2500, 1500, 1000, 800, 600, 400, 200, 0, 100, 0, 80, 0, 600, 600, 600, 600, 600, 600, 600};
const float ampl[19] = {1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0.1780, 0.2373, 0.3164, 0.4219, 0.5625, 0.75, 1};
const float ncyc[19] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 6, 6, 6, 6, 6, 6};
const int pwin = round(SAMPLINGFREQ * 0.0269);
const int paq_len = 64;

bool boton = false;
int ctrl = 0;

AdvancedADC adc1(A0);
AdvancedADC adc2(A1);
AdvancedADCDual adc_dual(adc1, adc2);

AdvancedDAC dac0(A12);
AdvancedDAC dac1(A13);

bool lastButtonState = LOW;

float SIGNAL_EMIT0[N_SAMPLES];
float SIGNAL_EMIT1[N_SAMPLES];

unsigned int SIGNAL_REC1[N_SAMPLES];
unsigned int SIGNAL_REC2[N_SAMPLES];

void setup() {
  
  Serial.begin(115200); delay(3000);

  pinMode(5, INPUT_PULLUP);

  dac0.begin(AN_RESOLUTION_12, SAMPLINGFREQ, paq_len, 32);
  dac1.begin(AN_RESOLUTION_12, SAMPLINGFREQ, paq_len, 32);
  adc_dual.begin(AN_RESOLUTION_16, SAMPLINGFREQ, paq_len, 32);

}

void loop() {
  int buttonState = digitalRead(5);
  int paquetes = N_SAMPLES / paq_len;

  // Wave Gen
  if (ctrl < 1) {
    // Constant voltage
    for (int i = 0; i < N_SAMPLES; i++) {
      SIGNAL_EMIT0[i] = 127 * 16;
    }

    // Sine wave
    for (int i = 0; i < N_SAMPLES; i++) {
      SIGNAL_EMIT1[i] = 127 * 16;
    }

    Serial.println("arrays inicializados");

    for (int j = 0; j < sizeof(freq) / sizeof(float); j++) { // loop configurations
      if (freq[j] > 0) {
        for (int i = 0; i < (int)round(ncyc[j] * (SAMPLINGFREQ / freq[j])); i++) {
          SIGNAL_EMIT1[j * pwin + i] = ampl[j] * sin(2 * M_PI * (i * (float)freq[j] / (float)SAMPLINGFREQ)) * 127 * 16 + 127 * 16;
        }
      }
    }

    Serial.println("Wave generated");
    ctrl++;
  }

  if (buttonState == LOW && lastButtonState == HIGH) {
    boton = true;

    Serial.println("Button pressed");

      for (int r = 0; r < 2; r++) {
        
        for (size_t i = 0; i < paquetes; i++) {
          SampleBuffer buf1 = dac1.dequeue();

          for (size_t k = 0; k < paq_len; k++) {
            buf1[k] = SIGNAL_EMIT1[k + (i * paq_len)];
          }

          dac1.write(buf1);

          SampleBuffer buf3 = adc1.read();

          for (int n = 0; n < buf3.size(); n++) {
            SIGNAL_REC1[n + (i * paq_len)] = buf3[n];
          }

          buf3.release();

        }
      }

      for (int p = 0; p < N_SAMPLES; p++) {
        Serial.println(SIGNAL_REC1[p]);
      }

    boton = false;
  } 
  else if (!boton) {
    voltaje_const();
  }

  lastButtonState = buttonState;
}

void voltaje_const() {
  SampleBuffer buf1 = dac1.dequeue();

  for (size_t j = 0; j < 64; j++) {
    buf1[j] = SIGNAL_EMIT0[j];
  }

  dac1.write(buf1);
}


Thanks in advance!

j can take values from 0 to 19 and pwin is 2582. Their product rapidly exceeds 4095 and it even becomes negative when j>=13, which will be beyond the limits of the array. This will break the program.

Although this happens before checking for the button press…