What is “not declared in this scope” error
As from the name we can understand that when the compiler of Arduino IDE is unable to recognize any variable or is unable to process any loop or any instruction having any undeclared variable so it gives the error “not declared in this scope”, which means that code is unable to understand the instruction given in the Arduino code.
The Arduino IDE has a feature that it automatically indicates the line of the program which contains errors simply by underlining them with red color even before the code is compiled.
Situations when the compiler gives the error “not declared in this scope”
To illustrate when the error of not declaring in this scope arises in the Arduino program we have given an example code in which this type of error was encountered.
For simplicity we have compiled an Arduino code which performs an addition operation and the image of the code for the simple addition having the error is posted below:
When we compiled this code we got the error “b is not declared in this scope“ as it can be seen in the image posted below.
As you can see that the compiler also identified the line which has the error and also it has indicated that the variable “b” is out of the scope in the program.
We have posted an image below in which you can clearly see that the Arduino IDE has highlighted the variable that it is unable to understand.
How to fix “not declared in the scope” error
To fix the error we simply just declare that variable so that the compiler can understand the variable used in the code. So always remember to declare the variable you want to use in the Arduino program. We have posted the correct code after removing this error:
We have successfully compiled the code and it can be seen from the image of the output menu of the Arduino IDE:
And the output of the code shown in the serial monitor is:
Conclusion
In Arduino programming while compiling the code one can encounter a whole bunch of error codes but these errors can be fixed by avoiding the mistakes in the first place. However, there are some errors which are due to the incorrect syntax used for programming and some are the logical errors. Although the chances of logical errors are minimal. The most common errors that are encountered by the users are related to syntax or declaration errors. So, we have described what are the declaration errors and how they arise and how we can remove them with the help of simple Arduino example code.