Arduino Giga crashing when running just once

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;
}

Why do you think your board crashes?
If you put Serial.println("I'm alive"); in the first line of your loop, what happens?

Yeah, so I've tried using multiple serial.print to check, and it dies right when i try to use the adc in some way, so i suspect that what dies is the adc, but i don't know why

Does the Giga have enough memory to store an array with 4096 elements?

I think so, when uploading, it sasy that is using only 12% of the available memory

You wrote it works once.

I know, but that doesn't help me solve the issue

I don't agree. Working once and not after gives a lot of information.
Why don't you write a code to just read that analog signal? Is voltage of this signal withing a range of your board?
ps. I don't know Giga specs

1 Like

the voltage is in range, basically bc I'm generating that signal with another giga, but I'll make a simpler code and try to make it work more than once

My guess is if you remove this line it won't crash...

Again only a guess, but I think it is returning to you an internal buffer where it reads the data into and you release it, and it thinks it still has that buffer to use on your second call.

I'm thinking that equal sign is killing you..
paquetes is 128, so i will hit 128..
and then..

as i can hit 128, that's 128 * 32 = 4096 and then you add k..
seems like your going out of bounds on your signal array..

good luck.. ~q

2 Likes

Yes

this actually was the solution, thank you so much!!

1 Like