Arduino Uno hangs and get inaccessible

I uploaded the following sketch to my Arduino Uno Rev V3:

#define READ_PIN 5
#define ON_BOARD_LED_DIO_PIN 13
#define BUFFER_SIZE 300

int buffer[BUFFER_SIZE];
unsigned long timeStamp[BUFFER_SIZE];
unsigned int index = 0;

void setup()
{
  Serial.begin(115200);
  Serial.println("START");
  
  pinMode(ON_BOARD_LED_DIO_PIN, OUTPUT);
  digitalWrite(ON_BOARD_LED_DIO_PIN, LOW);
}

void loop()
{
  if (index < BUFFER_SIZE)
  {
    timeStamp[index] = micros();
    buffer[index++] = analogRead(READ_PIN);
  }
  else if (index == BUFFER_SIZE)
  {
    for (int i=0; i < BUFFER_SIZE; i++)
    {
      Serial.print(i);
      Serial.print("\t");
      Serial.print(timeStamp[i]);
      Serial.print("\t");
      Serial.println(buffer[i]);
    }
    index++;
  }
}

It started to just continuoulsy send “START” messages, and it seems like it is restaring all the time. The big problem is that I can not exit this condition. I can not upload any new scetches. The TX LED is lit all the time due to the “START” messages, and I can talk to the Uno via the Serial Monitor (the RX LED lits), although the sketch does not monitor the serial line (the serial link seems to work OK). I now have nothing connected to any port of the Uno. Cycling the power just make the Uno start to send “START” again. The funny thing is that when I compile a sketch it says “nnn bytes (of a 28672 byte maximum)”. The maximum has become lower (it was 32 K before)! When I uploaded the sketch I had a photo transistor and resistor attached to A5. The simple circuit worked, since I previously had it attached to DIO pin 2 (Emitter to ground, Collector to A5 and 10k resistor from Collector to +5V).

Please give me some advice on how to exit the lock-up.

Moderator edit: code tags added, italics removed.

How much RAM memory does your board have? How much of it are you using? My first thoughts.

Thanks for the quick reply. I have 2K RAM and this should be sufficient to store the two buffers.

I now actually have managed to solve the lock-up!! I realised that the computer did not try to send any data when uplaoding since I didn't see the RX LED on the board. For some reason, the wrong board type had been selected. When I corrected this, I could upload a new skectch. I will now try to see why the sketch does not work properly. The first thing will be to reduce the BUFFER_SIZE.

I have 2K RAM and this should be sufficient to store the two buffers.

Barely. Those two buffers take up 600 and 1200 bytes of the 2048 available.

Your literal constants all take up space, as does the Serial instance's input and out buffers, as does the stack when calling functions. You need to leave some free space for the stack and other uses.

Thanks for all the quick support. It works fine if I reduce the size, so we can consider this issue [u]closed[/u]. It would, however, be nice to have some more concrete indication of how much RAM space a sketch takes up.

It would, however, be nice to have some more concrete indication of how much RAM space a sketch takes up.

When? The problem is that as you call functions the stack grows, decreasing the amount of memory available. As you return, the stack shrinks, increasing the amount of memory available.

There are functions, at least two different ones, that measure free memory (that's a search term for you), but they require some thought (see the issue above) to use intelligently.