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++)
{
   SensorValue[i] = analogRead(Sensor[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++)
{
   sensors[i]._value = analogRead(sensors[i]._pin);
}

Using structures allow you to keep variables that are related conviently grouped together.

Using symbols, instead of ‘hard’ numbers make the code easier to read, modify and understand. In the case of “virtual” pin numbers I prefer defining, assigning and grouping them with ‘enum’ keyword; others may not. Whatever method you use symbols are assigned a value before use and allow you to change ‘pin’ assinments in a single place versus searching, understanding and replacing hard numbers all throughout your code one at a time should you find yourself needing to rewire things.

When initializing array during declaration the ‘size’ specifier is not required. If done this way the size of the array can be calculated at compile time using the ‘sizeof’ operator. The size of an array is calculated as the size of the entire array divided by the sizeof of an entry in the array. Since it is not possible to declare an array of zero entries there will be at least one entry at index 0 thus allowing ‘sizeof(ARRAY_NAME)/sizeof(ARRAY_NAME[0])’ to calculate the number of entries at compile time. I used the macro ‘ARRAY_SIZE’ above to shorten the code a bit making it easier to code, and for me at least, read.

And finally an apology if you took my first post above as flippant. Experimenting is a good way to learn the language which in the case the Aduino IDE as ‘C++’ a not quite strict super-set of ‘C’.