# Array question

OK, for something that is probably simple to most programmers (which I'm not): can I create an array of values and at the same time define the initial value at 0?

For example to have 6 different Sensor values, and have them all with an initial value of 0, could I do something like:

int Sensor [6]={1,2,3,4,5,6} in Sensor=0

Or does that cancel each other out?

Create an experimental sketch, containing your code in question, and try compiling it to see what happens!

Code snippet, (not compiled or tested but to get the idea

``````// declare an array of six integer values all set to 0
int Sensors[6]={0,0,0,0,0,0};
int CurrentSensor=1;    // current sensor = 0;
...
int val = Sensors[CurrentSensor];
...
// Set
int newvalue = 15;
CurrentSensor = 3;
Sensors[CurrentSensor] = newvalue;  // Array becomes {0,0,0,15,0,0}
``````

Note that arrays are starting with index 0 so an array of six sensors counts from 0 to 5. See http://www.arduino.cc/en/Reference/Array. It is allways good to take time to read the tutorial and reference sections as these contain a lot of info and examples.

I see, I did read the tutorial, I clearly got some stuff mixed up afterwards.

Thanks for the example code!

Alright I got it now, I’m just having some difficulty implementing a delay in a “for” sequence.

Basically I have 2 Arrays that need to match each other, ie:

Sensor = {0,1,2,3,4,5}
SensorValue = {0,1,2,3,4,5}
count = 6;

If I want to have SensorValue be an AnalogRead of Sensor, but I want to put a small (10ms) delay in between them to avoid jitter, can I do this?
I mean I can write them all out, but I figure there has to be a way to do it with a counter. Something like:

for (int SensorValue = 0; SensorValue < count; SensorValue++)
analogRead(for (int Sensor = 0; Sensor < count; Sensor++))

Of course I dont know if I can mix 2 “for” commands within each other to make the arrays sync up, and I still have no idea how to insert a delay between them. The problem with writing it out is that if in the future I change the array length I have to add a bunch of code…

Cheers!

I mean I can write them all out, but I figure there has to be a way to do it with a counter. Something like…

No. But something like this will work:

``````for(int i=0; i<count; i++)
{
}
``````

Oh, smart....so should I add a statement for int i in the setup?

Any ideas on the delay? Maybe add it in the for function?

Thanks for this!

should I add a statement for int i in the setup?

Why? The scope of a variable should be as small as possible.

Any ideas on the delay? Maybe add it in the for function?

Not sure why you need a delay, but, if you do, yes, add it to the for loop.

You are right...I just re-read the description and the jitter and noise is just using them as digital inputs, not analog. My bad.

As for the int i statement, I thought I had to "declare" all my variables before I could use them...can I just "spring up" this int i in the for function?

I thought I had to "declare" all my variables before I could use them

You do. The variable i IS defined before it is used.

``````#define ARRAY_SIZE(a)      (sizeof(a) / sizeof(a[0]))

enum
{
pinSENSOR_0      = ?            // assign sensor pin value here
, pinSENSOR_1      = ?            // assign sensor pin value here
, pinSENSOR_2      = ?            // assign sensor pin value here
, pinSENSOR_3      = ?            // assign sensor pin value here
, pinSENSOR_4      = ?            // assign sensor pin value here
, pinSENSOR_5      = ?            // assign sensor pin value here
};

struct sensor_t
{
uint8_t   _pin;
int         _value;
};

sensor_t      sensors[] =
{
{ pinSENSOR_0, 0 }
, { pinSENSOR_1, 1 }
, { pinSENSOR_2, 2 }
, { pinSENSOR_3, 3 }
, { pinSENSOR_4, 4 }
, { pinSENSOR_5, 5 }
};

for ( int i = 0; i < ARRAY_SIZE(sensors); i++)
{