Counting values in array / 7 Segment efficiency

Is there a way to run an array through, without having to count the values inside?

Here is what I have to do (assuming that I have set an array with 9 values inside)

for (int i = 0; i < 9; i++) {
          digitalWrite (pins[i], LOW);
     }

This is hard to explain, as I use this within PHP. but here is what I would like to do, so as not to have to know how many values are in my array.

for (int i = 0; i < pins[] + 1; i++) {
          digitalWrite (pins[i], LOW);
     }

I'm having a hard time explaining my question, but if you can understand what I'm after, any help is much appreciated.

I'm still trying to master the C/C++ language, but I suspect that there is a way to do what you wish using the Sizeof() function:

http://arduino.cc/en/Reference/Sizeof

Lefty

cmj,

The first thing you should know is that your first example has an error. It will look at the first 10 elements of the array, and if the array is only dimensioned to 9 elements, that could cause problems. It's a common error, because if you're not used to programming in C, you may not be used to arrays where the first element has a subscript of 0.

In your for loop, the variable 'i' will be set to:

0 1 2 3 4 5 6 7 8 9

That's 10 elements. If your array only has 9 elements, you want to loop from 0 to 8, so the for loop should read:for (int i = 0; i < 9; ++i)

Back to your original question. If you know in advance how big the array will be, you should probably define a constant:

#define NUM_PINS 9

byte pins[NUM_PINS] = {5, 6, 7, 8, 9, 10, 11, 12, 14};

for (int i = 0; i < NUM_PINS; ++i)
   // etc.

What I think you're asking, though, is how to get the compiler to do the counting for you. Lefty is right (is that an oxymoron?), 'sizeof' is your friend. Try this:

byte pins[] = (5, 6, 7, 8, 9, 10, 11, 12, 14};
const int num_pins = (sizeof(pins) / sizeof(byte));

for (int i = 0; i < num_pins; ++i)
  // etc.

Regards,

-Mike

One of my favourite macros: #define SIZEOF_ARRAY(x)  (sizeof (x) / sizeof (x[0]) )

Obviously this won't work inside a function where the array is passed as a reference, but I can't think of many simple mechanisms that would.

Murdock and Groove, I appreciate both of your replies. I tend to jump around a lot when it comes to different projects, so my quick example did have an error in it. i've since changed it. I'm currently trying to figure out an efficient way to use a single 7 segment display, with no driver. I'm putting the configuration for numbers 0-9 in their own arrays, and then i'm going call them from a lookup chart on the fly, hence not wanting to need the number of values in an array at every given moment.

This is for a small mono-numbered clock that will display 4 numbers seperately (3 numbers for 9:00 and before)

Any input on creating the most efficient method of displaying numbers on a single display is most appreciated.. I'm going to go ahead and change the thread title and hopefully bring more people in.

I'm putting the configuration for numbers 0-9 in their own arrays

They'd each fit in a single byte - why bother with an array?

What does "mono-numbered" mean?

Mono-numbered being one display, confusing wording..

As far as using a byte, I'm very very new to the world of programming but will definitely research this. How would I utilize a byte, how would I trigger all the pins simultaneously?

A seven segment display often has eight LEDs - seven for the numeral segments plus the decimal point.
A byte holds eight bits, so you can have one bit to decide whether or not to light each LED.

Here's a driver posted recently:
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1270490474/0#0

There must be plenty of other examples.