2

I need to initialize several buttons and LEDs connected to my Arduino. To store data related to an individual button or LED, I decided to define my own data type using two different structs. One for a button and another one for a LED.

As I need to configure some GPIOs and other peripherals after instantiating each button/LED, I implemented a small factory function to get a clean interface and combine the required steps for instantiating and initialization.

Defining my own button_t type works as expected. The compiler does not throw any errors. However, I tried to implement the same approach for led_t type upon the compiler states the following error:

example:25:1: error: 'led_t' does not name a type

led_t ledFactory(uint8_t pinNumber)

^~~~~

exit status 1 'led_t' does not name a type

My example code looks like this:

#include <Arduino.h>
#include <stdint.h>

struct button_t
{
    const uint8_t pin;
    uint8_t currentState;
    uint8_t lastState;
    uint32_t lastDebounceTime;
};

button_t buttonFactory(uint8_t pinNumber)
{
    button_t btn = {pinNumber, 0, 0, 0};
    // setting some MCU registers here
    return btn;
};

struct led_t
{
    const uint8_t pin;
    const uint8_t state;
};

led_t ledFactory(uint8_t pinNumber)
{
    led_t led = {pinNumber, 0};
    // setting some MCU registers here
    return led;
};

void setup()
{
}

void loop()
{
}
4

This is a bug in the Arduino IDE: when converting your .ino file to a .cpp file, it added a declaration for the function ledFactory() before the declaration of the type led_t.

As it appears to be adding those declarations right before the first function of your sketch, a simple workaround is to put all your type definitions before your functions.

1
  • 1
    @albert, if your curious to see this mangling for yourself, check "compilation" for "Show verbose ... " in File/Preferences. You should see the full compiler command-line immediately before the error message. It will reference some <build-process-generated-directory>/sketch/your_sketch_name.ino.cpp file. There you see the result of the build process's manipulation of your .ino file into the .cpp file it ultimately tries to compile. And for your code, under IDE version 1.8.13 at least, exactly what Edgar described.
    – timemage
    Nov 16 '20 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.