Ask Your Question
3

How to provide Arduino input parameters of any size without creating a struct?

asked 2021-12-11 11:00:00 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-01-20 01:00:00 +0000

pufferfish gravatar image

One way to provide Arduino input parameters of any size without creating a struct is to use an array. For example, if you need to input a series of integers, you could create an integer array and pass it to a function.

Here's an example:

void myFunction(int *myArray, int arraySize) {
  for(int i=0; i<arraySize; i++) {
    Serial.println(myArray[i]);
  }
}

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

  int myArray[] = {1, 2, 3, 4, 5};
  int arraySize = sizeof(myArray)/sizeof(myArray[0]);

  myFunction(myArray, arraySize);
}

void loop() {
  //do nothing
}

In this example, we create an integer array called myArray with 5 integer values. We also calculate the array size by dividing the size of the array by the size of the first element.

We then pass myArray and arraySize to the myFunction function. Inside the function, we use a for loop to print out each element of the array using the Serial.println function.

By using an array, we can input any number of parameters without having to define a struct or hardcoding each parameter individually.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-12-11 11:00:00 +0000

Seen: 10 times

Last updated: Jan 20 '22