January 23, 2013 Leave a comment
It may be just me but I am interested in reading code written by others. It’s like literature. Everyone has a different style and you can always pick up something useful if you read. But some beginners read code written by others simply because they wanted to use that code on their own projects. If you have already done that as a beginner, you may have seen what I am about to introduce: the #define.
These “#define” appear in the beginning of some project code and you may wonder what they do and why they are there. Here is some light reading about the “#define”:
First of all, the #define is not declaring or defining a variable. It is simply a substitution rule.
#define foo bar
If you have the above at the beginning of your code, then every time foo shows up in subsequent code, it gets replaced by bar. Normally only complete match is replaced so foobar won’t be replaced by barbar. Also foo inside a text string is not replaced either.
This “#define” command is called a preprocessor directive. The substitution occurs before the code is compiled, thus the name.
There are a lot of good use for this directive. If you want to blink an LED, you can do:
digitalWrite(13,HIGH); delay(1000); digitalWrite(13,LOW); delay(1000);
But that would suck if you want to blink a different pin. You have to change pin number in multiple lines and pray not to make a mistake. But this will work much better.
#define LED 13 digitalWrite(LED,HIGH); delay(1000); digitalWrite(LED,LOW); delay(1000);
This way if you move the LED to pin 10, all you have to do is to modify the “#define LED 13” into “#define LED 10”. Obviously in this example the first way only requires two changes to blink LED on pin 10. On the other hand, if you have programmed the game Angry Bird with earth gravity g=9.81m/s/s and suddenly want to expand your game to moon, where gravity g=1.62m/s/s, good luck to you if you typed in 9.81. There would be thousands of places where 9.81 was typed in and need to be changed. And from reading the program, you will have no idea what gravity value was used (just seeing a 9.81 in a calculation doesn’t count as knowing gravity is 9.81). Now if you did “#define g 9.81” in the beginning of your code, remaking your Angry Bird on the moon will only take two seconds on your coding and it will also be obvious to the reader of your code, which is probably 90% chance yourself at a later time. You will be surprised how forgetful you are.
There are a lot of advanced uses of “#define” to control how your code gets compiled, which in my opinion is not useful for beginners. I will cover that in a separate post. Just as a teaser, let’s think about this question: you have two prototypes, prototype1 with a 16X2 LCD and 4X4 matrix keypad, prototype2 with a 20X4 LCD and rotary encoder. Your project code works on both hardware but the initialization part is dependent on which prototype you are using. You will be able to use something like “#define prototype1” to make the compiler only compile initialization code for prototype1. Just exactly how, I will cover it in a separate post.