Arduino 101: Tutorial #6 |Important Concepts|
Hello people!
In this tutorial we will see some important concepts like comments in a code and discuss a few mistakes that beginners make while learning arduino. So let's get started.!
Important Concepts
- Comments in a code: If you recall the very first code that we made. It was a fairly simple code to blink an LED. We defined only one variable (our pin number) and used a few commands that made our code to work. Now imagine a situation where you're working with tens of different led's performing different functions used at several different places in a code. You can see how difficult it will be to remember each one of the LED's and their functions in code. In this kind of situation, a code can easily become very messy. It will become very hard to read and understand, not for the computer(of course!) but for the human. Hence to make code more readable to human eyes, we use comments at various places to describe the working of block of codes. These comments are ignored by the computer and are for the convenience for the human reader only.! The programing language of arduino supports two types of comments:
- Block comments: These comments start with a /* and continue until a */ is encountered. Below is an example of the same.
- Single Line Comments: A single line comment starts with // and tells the computer to ignore the rest of the line.
2. Global variables v/s Local variables: Global variables are declared outside any function, and they can be accessed (used) on any function in the program. Whereas, local variables are declared inside a function, and can be used only inside that function. My suggestion is, wherever possible, use local variables, as it makes the code more readable and occupies less memory in the arduino.
3. Compiler: By now I believe you must have seen the "compiling sketch" bar below after pressing the upload button.
Now let us define what "compiling sketch" means. To compile a sketch a special program called compiler is used ,that processes statements written in a particular programming language and turns them into machine language or "code" that a computer's processor uses. In our case, the arduino IDE has an inbuild compiler that converts the sketch to a machine readable "code".
4. Serial Monitor:You may have noticed that Pins 0 and 1 on the Arduino board say “RX” and “TX” next to them. These pins are monitored by another chip on the Arduino. This chip is more commonly called a USB-to-Serial converter. In future, most probably I'll do an entire tutorial on Serial, but until then just remember that serial monitor is used to communicate between your computer and the arduino. That is, you can receive data on the serial monitor from the arduino, as well as send data/commands to the arduino via serial monitor. The most common use of serial monitor is to debug the code by checking what the program is doing at various points in a code.
Common Mistakes made by Beginners
If you've just started learning arduino and programing, there is a good chance your code will not compile due to some mistake in the format. Below I point out some common mistakes that beginners make while learning arduino:
- The most important thing to remember is that the programing language is case sensitive. That is, there is a strict difference between words in lowercase and words in uppercase. For eg. "LEDpin" is not equivalent to "ledpin".
- Block of codes are to be contained in curly braces '{' and '}'.
- There are no commas in numbers. Hence, to write the number 2,340 you must write 2340 only and not use any commas.
- All the local variables of a function can only be used inside that particular function.
- Every open parenthesis '(' must have a matching closed parenthesis ')'.
- Similarly, every open block comment'/*' must have a matching closing block '*/'.
- Each program statement needs to end with a semicolon ';'
And with this we end our tutorial here. I hope this gives you an insight on some of the basic concepts that are important while starting with arduino.
Hope you enjoyed the tutorial. If you have any suggestions/complaints or questions feel free to comment below. :)
Comments
Post a Comment