Calling a void but passing over a Nth number of variables

Hello

I'm struggling to understand how to implement this. I have a void called 'frame', when I call it I want to pass it some variables but the number of variables will vary each time it's called. Here is my code so far

void loop(){

  frame(0-1,0-3,0-5,1-5,1-6,1-12,2-11);

}


void frame( /**what do I put here**/ ){
  
  frameTimer = millis();
  
  while(millis() - frameTimer < frameTime){
    
    //Work with each variable here
   
  }
  
}

Each variable will be processed the exact same way. I just cant work out how to pass them

I have a void called 'frame' ...

You have a function called frame. It returns void (ie., nothing).

I want to pass it some variables but the number of variables will vary each time it's called

Pass a pointer to an array. Also pass the number of items in this array.

I want to pass it some variables but the number of variables will vary each time it's called

Why will the number of arguments not be constant? There are ways, using varags, to do that but writing such a function is NOT a beginner project.

Ok, I've dropped frame (I didn't realise that was a 'thing')

I've also looked into the suggestions and got in a bit deep.

It's all to do with creating animations on charlie-plexed LEDs. A lot of the animations are simple to do with loops and counters, some of the more precise ans detailed animations need to be written frame by frame.

The array is a ring of 16 LEDs stacked 8 rings high.

My idea is to write these frames as simple as possible, sending an array of three digit numbers to the function, the first of each 3 digit number is the ring level (vcc out) and the second number is the column number (gnd in)

What about this idea?

void loop(){

  thisFunction(000101102203215); //5 LED's to alternate

}

void thisFunction(int thisString){

  thisFunctionTimer = millis();
  while(millis() - thisFunctionTimer < thisFunctionTime){

    for(int i = 0;i < thisString.length() / 3;i ++) {

      digitalWrite(thisString.substring(i * 3, 1) + 17, HIGH);
      digitalWrite(thisString.substring(i * 3 + 1 , 2), LOW);
      digitalWrite(thisString.substring(i * 3, 1) + 17, LOW);
      digitalWrite(thisString.substring(i * 3 + 1 , 2), HIGH);

    }

  }

}

Unless I've got the math wrong, this should do the following for the length of time the frame needs to run.

digitalWrite(17, HIGH);
digitalWrite(0, LOW);
digitalWrite(17, LOW);
digitalWrite(0, HIGH);

digitalWrite(18, HIGH);
digitalWrite(1, LOW);
digitalWrite(18, LOW);
digitalWrite(1, HIGH);

digitalWrite(18, HIGH);
digitalWrite(2, LOW);
digitalWrite(18, LOW);
digitalWrite(2, HIGH);

digitalWrite(18, HIGH);
digitalWrite(3, LOW);
digitalWrite(18, LOW);
digitalWrite(3, HIGH);

digitalWrite(18, HIGH);
digitalWrite(15, LOW);
digitalWrite(18, LOW);
digitalWrite(15, HIGH);

This way I can write a each frame in the animation with only one line.
Or is there a better idea?

void thisFunction(int thisString){

Your function and the parameter you call it with are a mess. Is it an int or is it a String ? It can't be both.

Also, the function

   for(int i = 0;i < thisString.length() / 3;i ++) 
    {
      digitalWrite(thisString.substring(i * 3, 1) + 17, HIGH);
      digitalWrite(thisString.substring(i * 3 + 1 , 2), LOW);
      digitalWrite(thisString.substring(i * 3, 1) + 17, LOW);
      digitalWrite(thisString.substring(i * 3 + 1 , 2), HIGH);
    }

is going to execute very quickly and assuming that you have got the maths right (I have not checked) all you will see is dim LEDs as they turn on/off so fast. You also need to turn off the LEDs when the sequence is finished.

Sorry, it's a string, wrote it without testing it.

Also running this

      digitalWrite(17, HIGH);
      digitalWrite(0, LOW);
      digitalWrite(17, LOW);
      digitalWrite(0, HIGH);

In a loop for 50ms produced very bright results. This was how I started writing and decided it needed to be easier to write. They also do turn off after each time round.

Yuo can keep the function name "frame" (I believe). Nick was just pointing out that you were calling the function a "void", when that is just what the function returns, void ie Nothing.

You wouldn't call:

int Addab (int a, int b)
  {
    return a+b;
  }

an Int would you?

both Frame and Addab are functions, where as Frame returns void/nothing, Addab returns an int.

Nick, could you link one of your forum pages for this? I'm still learning myself, and haven't coded C in almost 20 years.

Simply though, if we had an array called FrameCoord[], with the list he provided already in the array, could we just pass the index of where we wanted to start? Or would passing the pointer to the array be more effecient? (Trying to learn best practices now, and avoid future bad habits).

Ah I see thank you. In this particular case, nothing needs returning just processing as such.

Marmotjr:
Nick, could you link one of your forum pages for this? I'm still learning myself, and haven't coded C in almost 20 years.

Simply though, if we had an array called FrameCoord[], with the list he provided already in the array, could we just pass the index of where we wanted to start? Or would passing the pointer to the array be more effecient? (Trying to learn best practices now, and avoid future bad habits).

char someArray[10]; //an array to store what needs to be send
someFunction(someArray,10); //send a pointer to the array and also how many elements in it.


void someFunction(char* ptr, int elements) {
  for (int i = 0; i < elements; i++){
    //process each element here
  }
}

or...

if you don't want to explicitly create a named array and then copy your values into it and keep count of how many etc...

you can use an implicit null terminated array of pointers to your vars.

int a = 1000;
int b = 2000;
int c = 3000;
int d = 4000;

void setup()
{
  Serial.begin(9600);

  someFunction((int*[]){&a,&b,&c,&d,0});
}

void loop()
{
}

void someFunction(int *ptr[])
{
  while (*ptr)
    Serial.println(**ptr++);
}

Ah, gotcha. Flashbacks have occurred! Somewhat :D.

In tom's example, how would the syntax of accessing the array elements be written? ptr[i]?

Yaafm, I see that one, but it's kind of scary lol, I easily see myself overwriting the entire memory by accident. What exactly does the ** do (I tried googling/searching for that, but obviously....)?

Thanks for the help guys, I remember now (I think), that passing a function an array creates a new copy of it, correct? Passing just the pointer allows for manipulation/access of the existing array without any increased memory usage, correct?

Thanks folks.

**ptr++ dereferences ptr twice THEN ptr is incremented.

The contents of ptr are int pointers, so the contents of the contents of ptr are ints i.e. the values of our vars.

Marmotjr:
I remember now (I think), that passing a function an array creates a new copy of it, correct?

No - C always passes arrays by reference not by value.