I'm using the following code to read an analog signal when I press a button, but i don't understand why, whenever it tries to read a second time, the board crashes and stops working until reset. Then it works just once and on the second try it crashes again.
Here's the code:
#include <Arduino_AdvancedAnalog.h>
#include <Arduino.h>
AdvancedADC adc1(A2);
uint64_t last_millis = 0;
#define N_SAMPLES (4096)
#define SAMPLINGFREQ (96000)
int paq_len = 32;
bool boton = 0;
int ctrl = 0;
unsigned int SIGNAL_REC1[N_SAMPLES];
bool lastButtonState = LOW;
void setup() {
Serial.begin(115200);
delay(3000);
pinMode(5, INPUT_PULLUP);
pinMode(86, OUTPUT);
pinMode(87, OUTPUT);
pinMode(88, OUTPUT);
//Resolution, sample rate, number of samples per channel, queue depth.
if (!adc1.begin(AN_RESOLUTION_16, SAMPLINGFREQ, paq_len, 32)) {
Serial.println("Failed to start analog acquisition!");
while (1)
;
}
}
void loop() {
bool buttonState = digitalRead(5);
int paquetes = N_SAMPLES / paq_len;
if (buttonState == LOW && lastButtonState == HIGH) {
Serial.println("boton soltado");
delay(22);
for (int i = 0; i <= paquetes; i++) {
Serial.println("OK");
while (!adc1.available()) {
Serial.println("wait");
delay(10);
}
Serial.println("available");
SampleBuffer buf = adc1.read();
for (int k = 0; k < buf.size(); k++) {
SIGNAL_REC1[k + (i * paq_len)] = buf[k];
}
buf.release();
ctrl++;
}
for (int k = 0; k < N_SAMPLES; k++) {
Serial.println(SIGNAL_REC1[k]);
}
Serial.println("Onda recibida");
lastButtonState = HIGH;
delay(1000);
}
lastButtonState = buttonState;
}