Join and print array

Hi.

Its been very long time since i programmed in C, so i built this very rude code:

Receive data from A/D → store in array → when reached 40 samples → Serial.print samples → bluetooth

#include <StackArray.h>
StackArray <int> stack;

void setup() {
  Serial.begin(9600);
   stack.setPrinter (Serial);
}

int Arr[40];

void loop() {

  int array_size = stack.count();

  if(array_size > 39)
  {
    while (!stack.isEmpty ())
    {
      stack.pop(); //Empty Array
    }


  char dataCode[80];

sprintf(dataCode,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", Arr[1], Arr[2],Arr[3], Arr[4],Arr[5],Arr[6], Arr[7],Arr[8], Arr[9],Arr[10], Arr[11],Arr[12], Arr[13],Arr[14], Arr[15],Arr[16], Arr[17],Arr[18], Arr[19],Arr[20], Arr[21],Arr[22], Arr[23],Arr[24], Arr[25],Arr[26], Arr[27],Arr[28], Arr[29],Arr[30], Arr[31],Arr[32], Arr[33],Arr[34], Arr[35],Arr[36], Arr[37],Arr[38], Arr[39],Arr[40]);

   //2,23,-50,-14,...

   Serial.println(dataCode);

  }

  
  int data = 1; // Data from A/D
  stack.push (1); //Push "1" up to 40
  Arr[array_size] = data; //Add data to Array


}

90% it prints ok, but sometimes i receive new line with blank data, understanding this can be accomplished in a much more efficient way, i´m asking your help.

Thanks.

  if(array_size > 39)
  {
    while (!stack.isEmpty ())
    {
      stack.pop(); //Empty Array
    }

Throwing the data array.

sprintf(dataCode,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", Arr[1], Arr[2],Arr[3], Arr[4],Arr[5],Arr[6], Arr[7],Arr[8], Arr[9],Arr[10], Arr[11],Arr[12], Arr[13],Arr[14], Arr[15],Arr[16], Arr[17],Arr[18], Arr[19],Arr[20], Arr[21],Arr[22], Arr[23],Arr[24], Arr[25],Arr[26], Arr[27],Arr[28], Arr[29],Arr[30], Arr[31],Arr[32], Arr[33],Arr[34], Arr[35],Arr[36], Arr[37],Arr[38], Arr[39],Arr[40]);

Then, access beyond the end of the empty array...

Do you really thins that 40 integer values, as strings, along with 40 commas AND a terminating NULL are going to fit in an array that can hold only 80 characters? I don't, for a minute. That array needs to be much larger.

  int data = 1; // Data from A/D
  stack.push (1); //Push "1" up to 40
  Arr[array_size] = data; //Add data to Array

Push the data onto a stack AND put it in an array. The stack is doing nothing but waste space.

Thanks for the answer.
Well following your tips, i simplified the code. It also includes the ADC part…

#include <ADSx.h>
ADSx ADC_ext(8,false,13,11,2,0);

void setup() {
  Serial.begin(9600);
  ADC_ext.CMRwrite(3,B001,4,4,1561);
}

int Arr[40];

int up;
void loop() {
  
  int data = ADC_ext.readSigned(2);   
  up++;
  Arr[up] = data;

        if (up >= 40)
        {
             up=0;
             char dataCode[5000];
             sprintf(dataCode,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", Arr[1], Arr[2],Arr[3], Arr[4],Arr[5],Arr[6], Arr[7],Arr[8], Arr[9],Arr[10], Arr[11],Arr[12], Arr[13],Arr[14], Arr[15],Arr[16], Arr[17],Arr[18], Arr[19],Arr[20], Arr[21],Arr[22], Arr[23],Arr[24], Arr[25],Arr[26], Arr[27],Arr[28], Arr[29],Arr[30], Arr[31],Arr[32], Arr[33],Arr[34], Arr[35],Arr[36], Arr[37],Arr[38], Arr[39],Arr[40]);
             Serial.println(dataCode);
        }

}

But the issue still remains, once a while i get a empty new line. Forgot to mention that this issue only happens when receiving data from Bluetooth, using USB always sends correct data.

I´m using a cheap HC06…

Issue in code or in bluetooth (hardware)?

  up++;
  Arr[up] = data;

Array indices start at 0.

The increment should occur AFTER up is used as an array index. To assure that it is incremented properly:

  Arr[up++] = data;
             char dataCode[5000];

Take your shoes off, if you need to! A value read from the analog pin will be in the range 0 to 1023. With the comma, each value will require no more than 5 places in the array. 40 values * 5 is nowhere near 5000. Only the Mega actually has 5000 bytes available.

But the issue still remains

Yes, it does. Array indices STILL start at 0, not 1. An array that can hold 40 elements does NOT have a value at Arr[40]. Pretending it does is still stupid,