1
votes

I am trying to create variables of type struct, that are available in the main loop and all other functions in Arduino script.

I made a simple struct:

struct IDENTITY
{
    int identifier;
    bool is_alive;
}

The struct is in the main ino file; declared on the top before the functions loop and setup and all the other functions I may use.

I did try a simple initializer function, because when I tried to instantiate a struct IDENTITY at the top of the script (where usually you put global variables, but after the struct declaration), I would get an error of type not defined.

void initialize()
{
    struct IDENTITY testguy;

    testguy.identifier = 1;
    testguy.is_alive = true;
}

This function is below the struct definition, and when I compile, it does not give me errors. I call initialize() from setup() and it works fine.

Now I would like to use testguy; although since it is in a different function, it is created as a local variable the with scope limited to the function in which is created, so I can't access these variables from loop nor any other function.

Although I can't create a variable of type IDENTITY anywhere outside of a function; so I am not sure exactly how to handle this. In Visual Studio with C++ I do not have problems creating structs instances, so I assume it is a problem with C and Arduino IDE?

1

1 Answers

1
votes
struct IDENTITY {
  int identifier;
  bool is_alive;
};

IDENTITY testguy = { 256, true };

void setup() {
  testguy.identifier=6;
  testguy.is_alive=false;
}

void loop() {
}

Works for me...