Use passed array value's as argument in function

I have a few leds and i want to control them with a function, but the array doesn’t seems to arrive at the function.

Here’s my code

void setup() {
  for (int i = 2; i < 8; i++) {
    pinMode(i, OUTPUT);//setting pins to OUTPUT
  }
  int leds[] = {1,4,5,7};//make array
  arrayLeds(leds);//turn all leds on in the array
}

void loop() {}//no loop today

int arrayLeds(int array[]) {
  int size = sizeof(array) / sizeof(int);//get lenght of the array
  for (int i = 0; i < size; i++) {
    digitalWrite(array[i],HIGH); //loop through array to turn led on
  }
}

Maybe if you set the pins you're using to be outputs?
It's a bad idea to hijack the serial pins - you may want to use them for debug purposes.

int size = sizeof(array) / sizeof(int);

size = 1

int arrayLeds(int array[]) {
  int size = sizeof(array) / sizeof(int);//get lenght of the array
  for (int i = 0; i < size; i++) {
    digitalWrite(array[i],HIGH); //loop through array to turn led on
  }
}

The function doesn’t get the whole array by value. The function gets a pointer to the array. So inside this function, sizeof returns 2 which is the size of a pointer.

This is a common pitfall. There’s no way around it. If the array argument isn’t going to be a fixed size every time, then you’ll have to pass the size along as another argument.

int arrayLeds(int array[], int size) {
  for (int i = 0; i < size; i++) {
    digitalWrite(array[i],HIGH); //loop through array to turn led on
  }
}

Called as:

arrayLeds(leds , sizeof(leds)/sizeof(int));

An alternative is to terminate the array with a special number.

const int END = -1;

void turnOn(int* leds)
{
    int pin;
    while ((pin = *leds++) != END)
      digitalWrite(pin, HIGH);
  }
}

int leds[] = {1,4,5,7, END};
turnOn(leds);

Even better is to put the vector in program memory but that is another story.

Cheers!

Better still is not to use an "int", when a "uint8_t" will suffice

How about a bit vector?

void turnOn(uint16_t leds)
{
  uint16_t mask = 1;
  for (uint8_t pin = 0; pin < 16; pin++) {
    if (mask & leds)
      digitalWrite(pin, HIGH);
    mask <<=1;
  }
}

uint16_t LED(uint8_t pin)
{
  return (1 << pin);
}

uint16_t leds = LED(2) + LED(4) + LED(7);
turnOn(leds);

Cheers!

Delta_G:
The function doesn't get the whole array by value. The function gets a pointer to the array. So inside this function, sizeof returns 2 which is the size of a pointer.

I missed that. I would have thought that sizeof(x) where x is an array type of unknown size should have generated a compilation error. sizeof() should be using the lvalue, not the rvalue.

This form:

arrayLeds(leds , sizeof(leds)/sizeof(int));

is fine when the array is an int. However, a generalized form can be used with any array type, not just int. One way would be:

arrayLeds(leds , sizeof(leds)/sizeof(leds[0]));

so the function could use something other that an int if the signature of arrayLeds() is changed. I also like:

#define ELEMENTCOUNT(x) (sizeof(x) / sizeof(x[0])

// some code...

   for (i = 0; i < ELEMENTCOUNT(leds); i++) {
      // more code...

That advantage is that the macro is “typeless” and can be used with any array type.

PaulMurrayCbr:
I missed that. I would have thought that sizeof(x) where x is an array type of unknown size should have generated a compilation error. sizeof() should be using the lvalue, not the rvalue.

In this case, x is a pointer to int, not an array of unknown size. sizeof (int *) == sizeof x, and on Arduino that is 2.

PaulMurrayCbr:
I missed that. I would have thought that sizeof(x) where x is an array type of unknown size should have generated a compilation error. sizeof() should be using the lvalue, not the rvalue.

If you look at the generated code, you will see that what it does is perfectly logical. The argument the function actually receives is a pointer, NOT an array. So, sizeof returns the size of the argument, which is the size of a pointer. This is by design, and how c has always been defined c since it was first introduced in the '70s. Were it not so, a huge amount of time, and RAM, would be wasted copying whole arrays to the stack on function calls.

Regards,
Ray L.