Help with capitalization and not in scope errors

I am confused about how capitalization affects Arduino code and what “void” does and what is in scope. I have read a bit on the subjects but am still confused and can’t find an answer on the capitalization issues.

I am working through the examples in the book Exploring Arduino written by Jeremy Blum. In working with Chapter 2, program Listings 2-5 and 2-6, I created the following errors.

In the beginning of the Arduino Sketch I have the following:

boolean ledOn=false; //Variable re the present state of the LED (on/off)

The last line of the sketch in the void loop section of the sketch I had the following:

digitalWrite(LED, LedOn); //for some reason the error I got here seemed to be the capital letter L used in LedOn

I had mistakenly capitalized the l in ledOn in the final line of code. The error I got was “LedOn” was not declared in this scope.

I made a similar error in my next sketch I worked on:

In the beginning of the sketch I had the following:

boolean ledMode=0; //Variable cycle between LED states.

In the setup section I had the following:

void setMODE(int mode)

At the end of the sketch I had the following:

setMode(ledMode);

The error I got was “setMode” was not declared in this scope.

The fix was to match the capitalization of “mode” in setmode. (I note that the first setMode function sets “mode” and following that there are a number of references to mode. If I change the capitalization of any of those “mode”s I get the same type of error. I ask, why does it matter? Perhaps more importantly where can I find more materials to read and learn more about this so I understand better how and why this all works.

Thank you for any helpful suggestions.

I am confused about how capitalization affects Arduino code and what "void" does and what is in scope.

Capitalization does NOT affect Arduino code, except that function calls must match function definitions and variable names must be consistently capitalized.

void is a return type. It means that the function does not return a value.

There are plenty of resources that describe scope, with examples of what is in, and not in, scope, for each level of scope - global, file, function, block, etc.

digitalWrite(LED, LedOn); //for some reason the error I got here seemed to be the capital letter L used in LedOn

Not for some random reason. There is a very good reason.

I ask, why does it matter?

Because a collection of letters that defines a function or variable name must be the same collection of letters everywhere the name is used. A different collection of letters means a different name. An A is not the same letter as an a, hugo.

Its not a question of mattering, that's how the language happens to be defined. There are literally millions of C and C++ programs out there so the language definition is not going to change in a backwards-incompatible way - C and C++ are case-sensitive, end of story.

The best way to avoid issues with capitalisation is to stick to a standard way of doing things.

"camelCase" is the norm for function and variable names. Where the name is made up of several words, start with lower case, then use a capital for the first letter of each new "word". For example:-

ledPin motorPin prevPotVal

If they're single "word" names, all lower case:- pot servo motor

Class names usually start with a capital, then follow the "camelCase" form:- Servo Serial LiquidCrystal NewPing

Defines are usually in upper case, often with individual "words" separated by underscores:- LCD_I2C_ADDRESS MAX_DISTANCE TRIG_PIN

Constants should really follow the same rule as defines, but will often be seen in the same "camelCase" form as variables.

If you use the same form all the time, you won't make any many mistakes, and won't need to keep referring back to the original declarations to see how you wrote things originally.

As stated, capitalization matters: ledOn, ledON, LEDon, LedOn, and LedON are all different names. They are not interchangable.

The "scope" in the error messages refers to the fact that where a name is valid depends on how it is defined. For example, a name defined near the top of a file is accessible anywhere in the file from that point on. A name defined inside of a function is only available inside that function. A name defined inside of a set of curly braces is only valid inside that set of curly braces.

When the error message says that LedON is not defined in the scope, it just means that the variable is not accessible in that area of the program. It may be available elsewhere, but not there.

There is a lot more to the scoping rules, and it would be good for you to read up on it. For example, you can define a variable at the top file level, and it's accessible in all functions in that file. But, if you are inside a function, and you define another variable with the exact same name inside that function, then that is a different variable and the one at the file level is hidden and not accessible in that function. Confusing? You bet! There are a lot more situations like that, there is a lot to learn.

Here's a link that might help explain variable scope:- Scope