Code simplification

I have a loop in my code that reads an x number of channels, therefore I need array with the same number of positions.

in order to make this fool proof, I wish to simply enter the number of ADC Channels as p[er the example below

int ADC_CHANNELS=6;
//Create a N position array to store the result of the ADC conversions
int myvar[6];

How can i create an array with the same number of positions as the number of channels?

I tried this and it did not work.

int ADC_CHANNELS=6;
//Create a N position array to store the result of the ADC conversions
int myvar[ADC_CHANNELS];

Oh yes it did! what gave you the idea that it did not?

Mark

holmes4:
Oh yes it did! what gave you the idea that it did not?

Mark

Oh no it didnt… :frowning:

 const int ADC_CHANNELS = 6;

#define ADC_CHANNELS 6;
//Create a N position array to store the result of the ADC conversions
int myvar[ADC_CHANNELS];

Would this be acceptable?

-jim lee

BulldogLowell:

 const int ADC_CHANNELS = 6;

100% Working, Thanks!

To define an array like that, the value for the size of the array has to either be a const int or a #define because the compiler has to have a definite number to work with at compile time.

If you want to use an actual variable, you could use a dynamically allocated array, although this approach is not recommended on arduino:

int ADC=6 ;

int* array_name = new int[ADC]  ;   //  this will work even if ADC is a variable