Custom listing type declaration with Arduino

I'm having trouble using a custom enum type in Arduino.

I read elsewhere that the use of a header file is necessary for custom type declarations due to preprocessing of the Arduino IDE. So, I did this, but I still cannot use my own type. Here are the relevant parts of my code in my main arduino file (beacon.ino)

#include <beacon.h> State state; 

And in beacon.h:

 typedef enum { menu, output_on, val_edit } State; 

But, when I try to compile, I get the following error:

 beacon:20: error: 'State' does not name a type 

I guess something is wrong with the way I wrote or included my header file. But what?

0
c types avr arduino
source share
1 answer

beacon.h should be as follows:

 /* filename: .\Arduino\libraries\beacon\beacon.h */ typedef enum State{ // <-- the use of typedef is optional menu, output_on, val_edit }; 

from

 /* filename: .\Arduino\beacon\beacon.ino */ #include <beacon.h> State state; // <-- the actual instance void setup() { state = menu; } void loop() { state = val_edit; } 

Leave the typdef file and the final instance of "state" is turned off when you run it in the main INO file or vice versa. Where the above beacon.h file should be in the users. \ Arduino \ libraries \ beacon \ directory directory, and the IDE needs to be restarted to cache its location.

But you could just define it and specify everything at once in INO

 /* filename: .\Arduino\beacon\beacon.ino */ enum State{ menu, output_on, val_edit } state; // <-- the actual instance, so can't be a typedef void setup() { state = menu; } void loop() { state = val_edit; } 

Both compile fine.

You can also use the following:

 /* filename: .\Arduino\beacon\beacon2.ino */ typedef enum State{ // <-- the use of typedef is optional. menu, output_on, val_edit }; State state; // <-- the actual instance void setup() { state = menu; } void loop() { state = val_edit; } 

here the instance is separate from the enumeration, allowing the enumeration to be exclusively a type. Where above is an instance, not a typedef.

+2
source share

All Articles