I'm working on a program for an ESP8266 and it's getting a little cluttered to have everything in one source file, so I decided to break it up, but I'm unsure of how to go about properly structuring the different files.
My current structure is a main.cpp
file that contains my void loop()
and void setup()
, with no includes. Then, I have a separate file named effects.cpp
with functions that will be different LED effects, and a corresponding effects.h
file. Again no includes. Finally, I have a globals.h
file that contains the following:
#ifndef GLOBAL_H
#define GLOBAL_H
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <FastLED.h>
#include <BlynkSimpleEsp8266.h> //library for blynk functions
#define NUM_LEDS 150 //Number of LEDs on the strip
#define DATA_PIN D5
#define CLOCK_PIN D6
#define LED_TYPE APA102 //Change this to the chipset of the LED strip you're using
#define BRIGHTNESS 84
extern char auth[]; //stores API token for Blynk
extern int pinValue; //stores state of button from Blynk app
CRGB leds[NUM_LEDS];
#endif
So then I added an #include <globals.h>
to both main.cpp
and effects.cpp
. My understanding was that with this structure, both of those files would be able to refer to the same libraries and the same variables as declared in globals.h
. This seems to be working for the most part, but I keep getting errors saying that "Blynk" has multiple definitions (first occurrence in effects.cpp
, second in main.cpp
). I get the same error for "leds".
I haven't defined "Blynk" anywhere in my code, it's an external library, so I'm not sure what the issue is. The code exactly as it is works fine if it's all in one file. I also can't use extern
before CRGB leds[NUM_LEDS];
in my globals.h
file because CRGB is not a recognized variable type.
Can anyone point me in the right direction, what am I doing wrong?