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() { }

albert
  • 205
  • 2
  • 13

2 Answers2

6

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.

You may want to take a look at:

Edgar Bonet
  • 45,094
  • 4
  • 42
  • 81
-1

First create structure and after create function and after function declaration line c++ is bottom approach.