I am working on a project involving an ESP-01 device where I am using defines in a header file to switch on/off certain portions of the code to test different options and save space when something is not used.
Like this in defines.h: #define USE_STATE_MACHINE
Then in my main.cpp I have this at the top: #include "defines.h" //Global defines here
Further down this:
#ifdef USE_STATE_MACHINE
enum {initSystem, pulseOff, pulseOn};
unsigned char CurrState = initSystem; //Initial state after power-on
void RunStateMachine(); //This is the state machine responsible for handling the rain pulses
void DoOnPulseArrival();
void InitSwitchSensor();
#endif
and further down the use of stuff when the define is active:
void setup()
{
// put your setup code here, to run once
String topic;
int jumper = 1; //Value read from GPIO2 with no connection on attached device
#ifdef USE_STATE_MACHINE
InitSwitchSensor(); //Set up the I/O direction and type for GPIO2 used as pulse detector
jumper = digitalRead(2); //1 if switch open, 0 if switch closed
#endif
//and other code of course
Here the InitSwitchSensor() call is marked as not within scope…
I cannot understand it because it IS declared and defined outside of any function in main.cpp and also before being called in setup(), yet it is marked as not within scope, WHY?
Is it forbidden to use #define to switch parts of the code on/off?
There are several similar out-of-scope instances further down too for other optional operations.
I had hoped that it would be possible to manage the size of the code using defines such that the define controls the full existence of the specific parts in my code (to fit into the limited flash)…
What happens if I remove the #ifdef on the declarations and implementyation of the functions but keep them at the usage places? Will the code then contain the implementation of the functions, which are not used so they occupy flash space?
Or is the linker smart enough to not put a function body, which is never called, into flash?
As is often the case, the problem could well be in tge code that you don’t show us.
It could be just the forum formatting but I note that the first #ifdef line is a different colour - is this within a larger conditionally compiled file that could be removing the section of code you have shown us?
What you could do is to set an option on the compiler to keep the file created after all of the macro substitutions are made and make sure that the first block of code/text is there.
Another way could be to check that calls to ‘RunStateMachine()’ work.
Susan
The coloring is done entirely on this forum website.
I have copied raw text from the source file and then applied the </> formatting button to the lines containing code. At that time the coloring appears in the preview pane to the right…
The call to RunStateMachine() inside loop() does not show any such problems in the editor.
Here is the entire loop code:
unsigned long int justnow; //Global var used in loop to read temp every hour
void loop()
{
justnow = millis();
char currtemp[10];
//Check for hourly temperature reading
if (NextTempRead < justnow)
{
NextTempRead = justnow + 3600000; //Next reading in an hour
if (GetTemperature(currtemp) == 0)
{
//To be done: A valid reading was done so send it via MQTT
}
}
#ifdef USE_STATE_MACHINE //And here we are running the state machine...
RunStateMachine(); // Check for arrival of rain pulses and related actions
#endif
delay(1); //Used to set low power operation of WiFi via ModemSleep
}
Follow-up:
I have tried removing a lot of #ifdefs but the issue of variable scope remains… Another example:
At the top of main.cpp I have a declaration of a global string variable.
This is the second line of code in main.cpp: String MyHostname; //Will hold the calculated hostname for WiFi connect
So I will consider this a global declaration, right?
But then further down inside the function StartWiFi() where it is going to be used:
int Start_WiFi()
{
...
String MyChipID = String(GetChipIdentifier(), HEX);
MyHostname = "esp-" + MyChipID; //MyHostname is a global var
Well, here the MyHostname item has a red wigglie underline and when hovering the mouse over it I get the message “MyHostname was not declared in this scope”
But it is clearly defined way up on the second line of code in the file just following the #include declarations!
What is PlatformIO doing???
Are you not allowed to use String type variables as global?
The red wiggles are produced by IntelliSense - a product of a well known company from Redmond - not by platformio.
Nevertheless - did you already try Rebuild IntelliSense Index” ?
Sometimes this helps. And do you also have error messages during compile (using pio build)? If yes - please show us the complete code - not only fragments. With these it’s impossible to see the real reason for the problems - especially when your glass orb isn’t present at the moment
I did not know about this at all…
But now I have executed the rebuild for my active environment and the wiggle disappeared!
But I had disabled a lot of stuff in between so now I will look over it all next and see if I can backtrack to the way it is supposed to work…