-1

I'm pretty new to programming for Arduino, and am slightly confused about how I can use structures with functions.

Below is an example - I have 2 sets of data stored in a structure, struct1 - firstSet and secondSet. I want to take each of these sets, and apply the function to them, changing all values. I then want to print the new values to the console, and the numbers should grow larger with each iteration, firstSet twice as fast as secondSet.

This doesn't happen, and the initial values are printed over and over again. I'm not sure what I need to do to feed those values back out to their original structures.

I've been looking at adding a return line, and also changing void to something else (int and void*), but I really can't seem to work this out. Is it even possible? Alternatively is there a better way to do this? Thanks in advance!

#include "Arduino.h"
//The setup function is called once at startup of the sketch
struct struct1
{
int val1 = 1;
int val2 = 2;
int val3 = 3;

};

struct1 firstSet;
struct1 secondSet;

void func1(struct struct1)
{
struct1 dataSet;
dataSet.val1 = dataSet.val2;
dataSet.val2 = dataSet.val3;
dataSet.val3++;
}
void setup()
{
  Serial.begin(9600);
  while (! Serial);
  Serial.println("Console Online");
}
void printAll()
{
Serial.println(firstSet.val1);
Serial.println(firstSet.val2);
Serial.println(firstSet.val3);
Serial.println("---");
Serial.println(secondSet.val1);
Serial.println(secondSet.val2);
Serial.println(secondSet.val3);
Serial.println("---END---");
}

// The loop function is called in an endless loop
void loop()
{
func1(firstSet);
func1(firstSet);
func1(secondSet);
printAll();
delay (1000);
}
jc89
  • 1
  • 1
  • 2
  • 1
    if i understood correctly, passing by reference is what you are looking for. get a good c++ book to learn the basics (https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – skeller Jul 25 '18 at 20:56
  • You need to give a name to the parameter of `func1`. – Barmar Jul 25 '18 at 20:56

3 Answers3

2

func1 is taking a struct struct1, i.e. a copy of a struct as a parameter. As a result, changes made in the function are not visible in the calling code. Even then, you're not actually changing the parameter but a local variable.

You need to declare the parameter as a reference to link the parameter to the variable in the calling function. Then you'll see the changes.

void func1(struct1 &dataSet)
{
    dataSet.val1 = dataSet.val2;
    dataSet.val2 = dataSet.val3;
    dataSet.val3++;
}
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thanks for this, that's working nicely! Now to see if I can get it working in my main project. I did find that I had to keep the struct definition in place, eg: 'void func1(struct struct1 &dataSet)' – jc89 Jul 25 '18 at 21:05
  • in Arduino 1.8.15 (nano IoT) this does not work. See my answer below on how to do it. – Miguel Tomás Aug 23 '21 at 11:12
0

I'm guessing a bit on the detail of what you want, but the essense of the answer is that your function must take a reference to the struct.

void func1(struct1& dataSet)
{
    dataSet.val1 = dataSet.val2;
    dataSet.val2 = dataSet.val3;
    dataSet.val3++;
}

The & makes dataSet a reference. Your code was passing a copy of the structure (among other errors) which is why it could never alter the original structure.

john
  • 85,011
  • 4
  • 57
  • 81
0

You need to declare the function parameter as a reference to link it to the variable in the calling function. In Arduino IDE one can declare a struct in a function like so:

(the example below is to read Nano's IMU acceleration sensor data)

#include <Arduino_LSM6DS3.h> // Use Arduino library for the IMU on the Nano 33 IOT

struct AccelerationData{
  // accelerometer values
  float ax;
  float ay;
  float az;
};

void getAccelerationValues(struct AccelerationData & data){
    data.ax=-1;
    data.ay=-1;
    data.az=-1;
    // read accelerometer values if available
    if (IMU.accelerationAvailable()) {
      IMU.readAcceleration(data.ax,data.ay,data.az);
    }
  }
Miguel Tomás
  • 1,714
  • 1
  • 13
  • 23