Skip to main content
The OP claims that he has an Arduino Mega, thus the Arduino-Uno tag is misleading
Link
Added avr-size output
Source Link
UserK
  • 559
  • 1
  • 11
  • 24

This is the output of avr-size:

   text    data     bss     dec     hex
  55828    3906    2978   62712    f4f8

This is the output of avr-size:

   text    data     bss     dec     hex
  55828    3906    2978   62712    f4f8
Clarified the meaning
Source Link
UserK
  • 559
  • 1
  • 11
  • 24

I'm trying to send 60 float values via serial. Numbers are provided by an accelerometer and saved in a 240 bytes-long array. The array has been declared globallyis sent to a function and these values are then copied in a structure.

The problem The content of the array is still correct when the function is called. Then, when I copy it in the structure, something wrong happens and data are modified after the 26th float.

Where does the modification of an array of floats take place?

void sendBuffAcc(float buffXAccToSend[20];
 [...]
 collect(a[])
 {
  ifint (storeAccData)
stepCont = {1;
   
  // Filling BuffPrints 
 the content of the buffXAccToSend[contBuffAcc-1]globally =defined accX;array buffXAccToSend
  
 Serial.println();    contBuffAcc++;
  }

  //for After(int 60i=0;i<sizeBuffAcc;i++) values call sendBuffAcc() 
  if{ (contBuffAcc%(sizeBuffAcc+1) == 
 0) 
   {Serial.print("val: ");
    sendBuffAccSerial.print(i);
    accDataReady=true;Serial.print(" 
   }
 }");
 [...]

This array is then copied in a structure called and the sent via serial byte by byte.. Because of buffer's dimension constraints, the message is sent 20 values at the time.

Here is the function that creates messages to send via serial

void sendBuffAcc  Serial.println(buffXAccToSend[i])
{;
  int stepCont}
 = 1;

Serial.println();  // Data are store in a structure and then sent 

  MyShortCommand * pMyCmdShort = (MyShortCommand *)(&bufferAcc[sizeof(MyControlHdr)]);

  for (int i=0;i<sizeBuffAcc;i++)    
  {
    Serial.print("val: ");
    Serial.print(i);
    Serial.print("| Global Array:");
    Serial.println(buffXAccToSend[i]);

    numFilterValuesToSend++;    
    
    pMyCmdShort->cmd = accValuesID;  
    pMyCmdShort->param1 = buffXAccToSend[i];a[i];  
 
    //pMyCmdShort->param2 You= can0;
 find the output ofpMyCmdShort->param3 this= loop0;
 at the end ofpMyCmdShort->param4 the= question0;
  
   if Serial.print(printAckCommands)
"|   Param: {");
    Serial.print(pMyCmdShort->param1);
    Serial.print(""| CMD number: Array:");
      Serial.println(stepConta[i]); 

    // Move Serial.println(pMyCmdShort->param1);
the pointer to save data in Serial.println();next positions
    pMyCmdShort++;
  } 
  stepCont++;
 }
 storeAccData = false;
 contBuffAcc=1;   pMyCmdShort++;   
}

First, the content of the global array buffXAccToSend is printed via serial, then the i-th elements of a[i] and the value assigned to the pMyCmdShort->param1. This is the output:

val: 0    -0.00
val: 1   // checks-0.00
val: whether2 all values have been0.01
val: added3 to the message or-0.01
val: the4 
    0.04
val: //5 content has reached the0.00
val: maximum6 dimension 'maxLengthMess'
  0.01
val: 7 if (stepCont%(maxLengthMess) == 0.03
val: ||8 stepCont == sizeBuffAcc) 0.01
val: 9   {
 -0.00
val: 10    sendCommandToMatlabFilter();0.00
val: 11    -0.01
val: 12    -0.00
val: 13   } 0.01
val: 14  
   }-0.02
val: 15 stepCont++;
 }  0.03
val: storeAccData16 = false;  0.00
val: contBuffAcc=1;17    0.02
val: 18    -0.01
}val: 19    0.00

The problem The content of buffXAccToSend is correct in the collect() function but when I use it in the sendBuff() function it data after the 26th float are all 0.

The content of a the 60 float long array, buffXAccToSend:

val: 20    -0.00
val: 21    -0.0200
val: 22    -0.0201
val: 23    -0.01
val: 24    -0.0201
val: 25    -0.02  01
val: 26    -0.00
val: 27    0.03
val: 28    -0.01
val: 29    -0.00
val: 30    0.0104
val: 31    -0.0001
val: 32    -0.01 

val: 330| Global Array: 0.01|  Param: 0.0001|  Array: 0.01
val: 341| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: 352| Global Array: -0.01|  Param: -0.01|  Array: -0.01
val: 3| Global Array: -0.02|  Param: -0.02|  Array: -0.02
val: 364| Global Array: 0.02|  Param: 0.02|  Array: 0.02
val: 375| Global Array: 0.01|  Param: -0.0001|  Array: 0.01
val: 386| Global Array: 0.01| - Param: 0.0001|  Array: 0.01
val: 397| Global Array: 0.01|  Param: 0.01|  Array: 0.0001
val: 408| Global Array: 0.04|  Param: 0.0004|  Array: 0.04

And here is the output of the for loop inside the sendBuff()

Sendingval: #9| commandsGlobal Array:20 0.00|  Param: 0.00|  Array: 0.00
val: CMD10| numberGlobal Array: 210.01|  Param: 0.01|  Array: 0.01
val: 11| Global Array: -0.00|  Param: -0.00|  Array: -0.00
val: 12| Global Array: -0.01|  Param: -0.01|  Array: -0.01
val: CMD13| numberGlobal Array: 220.00|  Param: 0.00|  Array: 0.00
val: 14| Global Array: -0.02|  Param: -0.02|  Array: -0.02
val: 15| Global Array: -0.02|  Param: -0.02|  Array: -0.02
val: CMD16| numberGlobal Array: 230.00|  Param: 0.00|  Array: 0.00
val: 17| Global Array: -0.02|  Param: -0.02|  Array: -0.02
val: 18| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: CMD19| numberGlobal Array: 240.03|  Param: 0.03|  Array: 0.03
val: 20| Global Array: -0.01|  Param: -0.01|  Array: -0.01
 
val: CMD21| numberGlobal Array: 250.00|  Param: 0.00|  Array: 0.00
-val: 22| Global Array: 0.0201|  Param: 0.01|  Array: 0.01
val: 23| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: CMD24| numberGlobal Array: 26-0.01|  Param: -0.01|  Array: -0.01
val: 25| Global Array: -0.02|  Param: -0.02|  Array: -0.02
 
val: CMD26| numberGlobal Array: 270.00|  //Param: From0.00| now onArray: values0.00
val: are27| allGlobal zero!Array: Why?
0.00|  Param: 0.00|  Array: 0.00
 
val: CMD28| numberGlobal Array: 28
0.00|  Param: 0.00|  Array: 0.00
 
val: CMD29| numberGlobal Array: 29
0.00|  Param: 0.00|  Array: 0.00
 
[val: 30| Global Array: 0.00|  Param: 0.00|  Array: 0.]
00
val: CMD31| numberGlobal Array: 590.00|  Param: 0.00|  Array: 0.00
val: 32| Global Array: 0.00|  Param: 0.00|  Array: 0.00

I've also tried passing the array as an argument like sendBuff(buffXAccToSend) but it didn't work. Is it a memory related problem?

I'm trying to send 60 float values via serial. Numbers are provided by an accelerometer and saved in a 240 bytes-long array. The array has been declared globally

 float buffXAccToSend[20];
 [...]
 collect()
 {
  if (storeAccData)
  {
    // Filling Buff 
     buffXAccToSend[contBuffAcc-1] = accX;   
     contBuffAcc++;
  }

  // After 60 values call sendBuffAcc()
  if (contBuffAcc%(sizeBuffAcc+1) == 0) 
   {
    sendBuffAcc();
    accDataReady=true; 
   }
 }
 [...]

This array is then copied in a structure called and the sent via serial byte by byte.. Because of buffer's dimension constraints, the message is sent 20 values at the time.

Here is the function that creates messages to send via serial

void sendBuffAcc()
{
  int stepCont = 1;

  // Data are store in a structure and then sent
  MyShortCommand * pMyCmdShort = (MyShortCommand *)(&bufferAcc[sizeof(MyControlHdr)]);

  for (int i=0;i<sizeBuffAcc;i++)    
  {
    numFilterValuesToSend++;    
    
    pMyCmdShort->cmd = accValuesID;  
    pMyCmdShort->param1 = buffXAccToSend[i];  
 
    // You can find the output of this loop at the end of the question
    if (printAckCommands)
    {
      Serial.print(" CMD number: ");
      Serial.println(stepCont);
      Serial.println(pMyCmdShort->param1);
      Serial.println(); 
    }     
    pMyCmdShort++;
     
    // checks whether all values have been added to the message or the 
     // content has reached the maximum dimension 'maxLengthMess'
    if (stepCont%(maxLengthMess) == 0 || stepCont == sizeBuffAcc)
    {
      sendCommandToMatlabFilter();          
    }    
   }
  stepCont++;
 }
 storeAccData = false;
 contBuffAcc=1;      
}

The problem The content of buffXAccToSend is correct in the collect() function but when I use it in the sendBuff() function it data after the 26th float are all 0.

The content of a the 60 float long array, buffXAccToSend:

val: 20    -0.00
val: 21    0.02
val: 22    0.02
val: 23    0.01
val: 24    -0.02
val: 25    -0.02  
val: 26    0.00
val: 27    0.03
val: 28    -0.01
val: 29    -0.00
val: 30    0.01
val: 31    -0.00
val: 32    -0.01
val: 33    0.00
val: 34    0.01
val: 35    0.02
val: 36    0.02
val: 37    -0.00
val: 38    -0.00
val: 39    0.00
val: 40    0.00

And here is the output of the for loop inside the sendBuff()

Sending # commands:20
 CMD number: 21
-0.00

 CMD number: 22
0.02

 CMD number: 23
0.02

 CMD number: 24
0.01
 
 CMD number: 25
-0.02

 CMD number: 26
-0.02
 
 CMD number: 27  // From now on values are all zero! Why?
0.00
 
 CMD number: 28
0.00
 
 CMD number: 29
0.00
 
[...]

 CMD number: 59
0.00

I've also tried passing the array as an argument like sendBuff(buffXAccToSend) but it didn't work. Is it a memory related problem?

I'm trying to send 60 float values via serial. Numbers are provided by an accelerometer and saved in a 240 bytes-long array. The array is sent to a function and these values are then copied in a structure.

The problem The content of the array is still correct when the function is called. Then, when I copy it in the structure, something wrong happens and data are modified after the 26th float.

Where does the modification of an array of floats take place?

void sendBuffAcc(float a[])
{
  int stepCont = 1;
 
  // Prints the content of the globally defined array buffXAccToSend
  Serial.println();      
  for (int i=0;i<sizeBuffAcc;i++)    
  {   
    Serial.print("val: ");
    Serial.print(i);
    Serial.print("    ");
    Serial.println(buffXAccToSend[i]);
  }
  Serial.println();            

  MyShortCommand * pMyCmdShort = (MyShortCommand *)(&bufferAcc[sizeof(MyControlHdr)]);

  for (int i=0;i<sizeBuffAcc;i++)    
  {
    Serial.print("val: ");
    Serial.print(i);
    Serial.print("| Global Array:");
    Serial.println(buffXAccToSend[i]);

    numFilterValuesToSend++;    
    
    pMyCmdShort->cmd = accValuesID;  
    pMyCmdShort->param1 = a[i];  
    pMyCmdShort->param2 = 0;
    pMyCmdShort->param3 = 0;
    pMyCmdShort->param4 = 0;
 
    Serial.print("|   Param: ");
    Serial.print(pMyCmdShort->param1);
    Serial.print("|   Array:");
    Serial.println(a[i]); 

    // Move the pointer to save data in next positions
    pMyCmdShort++;
  } 
  stepCont++;
 }
 storeAccData = false;
 contBuffAcc=1;      
}

First, the content of the global array buffXAccToSend is printed via serial, then the i-th elements of a[i] and the value assigned to the pMyCmdShort->param1. This is the output:

val: 0    -0.00
val: 1    -0.00
val: 2    0.01
val: 3    -0.01
val: 4    0.04
val: 5    0.00
val: 6    0.01
val: 7    0.03
val: 8    0.01
val: 9    -0.00
val: 10    0.00
val: 11    -0.01
val: 12    -0.00
val: 13    0.01
val: 14    -0.02
val: 15    0.03
val: 16    0.00
val: 17    0.02
val: 18    -0.01
val: 19    0.00
val: 20    0.00
val: 21    -0.00
val: 22    -0.01
val: 23    -0.01
val: 24    0.01
val: 25    0.01
val: 26    -0.00
val: 27    0.03
val: 28    -0.01
val: 29    -0.00
val: 30    0.04
val: 31    -0.01
val: 32    0.01 

val: 0| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: 1| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: 2| Global Array: -0.01|  Param: -0.01|  Array: -0.01
val: 3| Global Array: -0.02|  Param: -0.02|  Array: -0.02
val: 4| Global Array: 0.02|  Param: 0.02|  Array: 0.02
val: 5| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: 6| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: 7| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: 8| Global Array: 0.04|  Param: 0.04|  Array: 0.04
val: 9| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 10| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: 11| Global Array: -0.00|  Param: -0.00|  Array: -0.00
val: 12| Global Array: -0.01|  Param: -0.01|  Array: -0.01
val: 13| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 14| Global Array: -0.02|  Param: -0.02|  Array: -0.02
val: 15| Global Array: -0.02|  Param: -0.02|  Array: -0.02
val: 16| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 17| Global Array: -0.02|  Param: -0.02|  Array: -0.02
val: 18| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 19| Global Array: 0.03|  Param: 0.03|  Array: 0.03
val: 20| Global Array: -0.01|  Param: -0.01|  Array: -0.01
val: 21| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 22| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: 23| Global Array: 0.01|  Param: 0.01|  Array: 0.01
val: 24| Global Array: -0.01|  Param: -0.01|  Array: -0.01
val: 25| Global Array: -0.02|  Param: -0.02|  Array: -0.02
val: 26| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 27| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 28| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 29| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 30| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 31| Global Array: 0.00|  Param: 0.00|  Array: 0.00
val: 32| Global Array: 0.00|  Param: 0.00|  Array: 0.00

Is it a memory related problem?

added 251 characters in body
Source Link
UserK
  • 559
  • 1
  • 11
  • 24
Loading
Source Link
UserK
  • 559
  • 1
  • 11
  • 24
Loading