Skip to main content
Added more explanations.
Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126
  int i=0; 
  int  dist [i] = {
    calculateDistance()      };

I'm not sure what you are thinking here, but you obviously cannot store data into a zero-length array. You also cannot put a function call to populate an array of variable length.

You need to decide how many elements you need in the array, in advance, and allocate an array of that size.


Maybe more like this:

 int  dist [180];

  for (i=0;i<180;i++){    
    dist [i] = calculateDistance ();
    if (dist[i]>max){
      max=dist[i];
      angle=i;
      Serial.print(angle);
      delay(30);
    }

  }

Also, this is wrong in your code:

for (i=0;i=180;i++){  

You need to test for < 180 here. Your for loop will assign 180 to i and then check to see if it is non-zero, which will always be true.

  int i=0; 
  int  dist [i] = {
    calculateDistance()      };

I'm not sure what you are thinking here, but you obviously cannot store data into a zero-length array. You also cannot put a function call to populate an array of variable length.

You need to decide how many elements you need in the array, in advance, and allocate an array of that size.

  int i=0; 
  int  dist [i] = {
    calculateDistance()      };

I'm not sure what you are thinking here, but you obviously cannot store data into a zero-length array. You also cannot put a function call to populate an array of variable length.

You need to decide how many elements you need in the array, in advance, and allocate an array of that size.


Maybe more like this:

 int  dist [180];

  for (i=0;i<180;i++){    
    dist [i] = calculateDistance ();
    if (dist[i]>max){
      max=dist[i];
      angle=i;
      Serial.print(angle);
      delay(30);
    }

  }

Also, this is wrong in your code:

for (i=0;i=180;i++){  

You need to test for < 180 here. Your for loop will assign 180 to i and then check to see if it is non-zero, which will always be true.

Source Link
Nick Gammon
  • 38.9k
  • 13
  • 70
  • 126

  int i=0; 
  int  dist [i] = {
    calculateDistance()      };

I'm not sure what you are thinking here, but you obviously cannot store data into a zero-length array. You also cannot put a function call to populate an array of variable length.

You need to decide how many elements you need in the array, in advance, and allocate an array of that size.