I am creating an ESP8266 project to log the current status of my garage door (open or closed) to data.sparkfun.com. When I try to compile the code I get an error "'postDoorStatus' was not declared in this scope" (postDoorStatus refers to a function which actually posts the door status to the data.sparkfun.com site). If I re-order the functions so that the postDoorStatus function is BEFORE the loop function then no problems. If I comment out the #include <ESP8266WiFi.h> then no problem.
I have removed almost all the code to demonstrate - even this minimal code reproduces the problem.
#include <ESP8266WiFi.h>
//declare constants, global variables, and pin numbers
#define doorSensor 5 // GPIO 5 pin of ESP8266-12 will be the pin that sensor connects to
int lastDoorStatus = 0; //global variable to track door status - initialize to 0 (open)
void setup() {
pinMode (doorSensor, INPUT_PULLUP);
}
void loop() {
int doorSensorValue = digitalRead(doorSensor);
postDoorStatus(doorSensorValue);
}
void postDoorStatus(int doorSensorValue) {
//do nothing here for troubleshooting purposes
}
full error message:
Arduino: 1.6.7 (Mac OS X), Board: "Generic ESP8266 Module, Serial, 80 MHz, 40MHz, DIO, 115200, 512K (64K SPIFFS), ck"
/Users/XXXX/Dropbox/Arduino/Sketchbooks/not_in_scope_error_troubleshoot/not_in_scope_error_troubleshoot.ino: In function 'void loop()':
not_in_scope_error_troubleshoot:21: error: 'postDoorStatus' was not declared in this scope
postDoorStatus(doorSensorValue);
^
exit status 1
'postDoorStatus' was not declared in this scope
This report would have more information with
"Show verbose output during compilation"
enabled in File > Preferences.
In general, functions should be known to the compiler before you use them. For (to me) unknown reasons this however does not always seem to be the case (most Arduino code that I have seen / used does not seem to require it though).
You can add a prototype in the beginning of your code file
#include <ESP8266WiFi.h>
//declare constants, global variables, and pin numbers
#define doorSensor 5 // GPIO 5 pin of ESP8266-12 will be the pin that sensor connects to
int lastDoorStatus = 0; //global variable to track door status - initialize to 0 (open)
// prototypes here
void postDoorStatus(int doorSensorValue);
void setup() {
pinMode (doorSensor, INPUT_PULLUP);
}
...
...
Alternatively you can create an include file in your project directory with all the prototypes of functions that you created and include that file in your source(s).
sterretje:
In general, functions should be known to the compiler before you use them. For (to me) unknown reasons this however does not always seem to be the case (most Arduino code that I have seen / used does not seem to require it though).
The IDE adds the prototypes automatically.
In this case an external tool (ctags) fails when a certain header is included.
sterretje:
In general, functions should be known to the compiler before you use them. For (to me) unknown reasons this however does not always seem to be the case (most Arduino code that I have seen / used does not seem to require it though).
The IDE automatically generates prototypes for you, sometimes doing it wrongly.
Sorry it took me a while to get back to this project. Thanks for all the helpful comments although I'm not sure I can say I actually understand the problem better now. Seems to be an idiosyncracy.