passing array to a function

ok, i'm at a lost as how to make this work. I want to preform the following:

do_something(array[4], 500);

then

void do_something(array[], int timer){
  int x = 0

  do
  {
    x++;
    if(digitalRead(array[] == LOW){
      digitalWrite(LED[], HIGH)
    }
  }while(x<timer)
}

This isn't all the code for this section as i'm still working through it and trying to understand what's needed? any help would be greatly appreciate!

I think a plain english description might be a better way to communicate what you're wanting to do as your code fragment is lacking and confusing.

mopar99:

do_something(array[4], 500);

of what type is array[]?

You are passing the 4th element of the array, not the array.

if array[] is an array of ints, for example:

int array[20];

void setup() 
{

}

void loop() 
{
  myFunction(array[4]);
}

void myFunction(int elementFour)
{
  
}

My apologies... The the array[4] = {22,23,24,25};

Was not sure how to handle the part of the function to receive this and then be able to use it.

Basically, I have a array of 4 pins and value for a timer. The function is to check each pin of the array for the time allowed, then move on to the next pin.

Do you really mean 'timer' or is it a loop 'count'?

Would like for it to be a timer but it currently as a counter.

mopar99:
Would like for it to be a timer but it currently as a counter.

You can use the array name, "array" in your example to pass a pointer to the first element of the array.

void myFunction(byte* myArray)

Let's you pass array to a function, for example.

You can use the pointer to manipulate elements of the array or just use the values pointed to by the pointer in your function.

BulldogLowell:
You can use the array name, "array" in your example to pass a pointer to the first element of the array.

void myFunction(byte* myArray)

Let's you pass array to a function, for example.

You can use the pointer to manipulate elements of the array or just use the values pointed to by the pointer in your function.

it might be easier for one to follow, if you would use the code provide. i'm still at a lost.

mopar99:
Basically, I have a array of 4 pins and value for a timer. The function is to check each pin of the array for the time allowed, then move on to the next pin.

I'm not sure what your code is supposed to do.

check each pin for what? what time is allowed?

BulldogLowell:
I'm not sure what your code is supposed to do.

check each pin for what? what time is allowed?

i think if you read though the code again and what i've typed, it will make sense. The part i'm hung up on is making the array work. there's most like needs to a for loop before the do...while loop in order to cycle through the pins (has beed added to the code below).

int array[4] = {22,23,24,25};
int LED[4] = {30,31,32,33};

do_something(array[4], 500); 

void do_something(array[4], int timer){
  int x = 0

  for(i=0; i<3;i++){ //newly added piece of code as mention above.
    do
    {
      x++;
      if(digitalRead(array[i] == LOW){
        digitalWrite(LED[i], HIGH);
      }
    }while(x<timer);
  }
}

mopar99:
i think if you read though the code again and what i've typed, it will make sense. The part i'm hung up on is making the array work. there's most like needs to a for loop before the do...while loop in order to cycle through the pins (has beed added to the code below).

I'm not really understanding what it is you are trying to do, but this compiles. You pass the array name, and the size of the array and the duration of time to sample in milliseconds ( not tested):

int myArray[4] = {22, 23, 24, 25};
int myLed[4] = {30, 31, 32, 33};

void setup()
{
  Serial.begin(9600);
  do_something(myArray, sizeof(myArray) / sizeof(myArray[0]), 500);
}

void loop()
{
  
}

void do_something(int* array, size_t num_elements, unsigned long duration)
{
  for (size_t i = 0; i < num_elements; i++)
  {
    Serial.println(array[i]);
    unsigned long startTime = millis();
    do
    {
      if (digitalRead(array[i] == LOW))
      {
        digitalWrite(myLed[i], HIGH);
      }
    } while (millis() - startTime < duration);
  }
}

BulldogLowell:
I'm not really understanding what it is you are trying to do, but this compiles. You pass the array name, and the size of the array and the duration of time to sample in milliseconds ( not tested):

int myArray[4] = {22, 23, 24, 25};

int myLed[4] = {30, 31, 32, 33};

void setup()
{
 Serial.begin(9600);
 do_something(myArray, sizeof(myArray) / sizeof(myArray[0]), 500);
}

void loop()
{
 
}

void do_something(int* array, size_t num_elements, unsigned long duration)
{
 for (size_t i = 0; i < num_elements; i++)
 {
   Serial.println(array[i]);
   unsigned long startTime = millis();
   do
   {
     if (digitalRead(array[i] == LOW))
     {
       digitalWrite(myLed[i], HIGH);
     }
   } while (millis() - startTime < duration);
 }
}

Thank you BulldogLowell!

I play with it last night to get a better understanding of it. :smiley: Now I'm off to chase a bug.