Hardware Reference
In-Depth Information
Lines and Spacing
Because the Arduino compiler can separate each statement by looking at
the semicolons and the curly brackets, it doesn't concern itself at all with how
you format your lines. In fact, you could put the entire Blink sketch on one
line if you'd like.
To make it easier for mere mortals to read and understand your code, how-
ever, it's a good idea to put each statement on its own line and indent each
new block of code.
Case Sensitivity
With the Arduino language, everything is case-sensitive. This means that
trying to call a function called PINMODE() will generate an error (try pin
Mode() instead). This goes for variables as well. If you define a variable called
led , you can't access it with LED , which would be considered a totally separate
variable.
Hardcoding
When you called pinMode() and digitalWrite() in the Blink sketch, you used
the variable led to pass the pin number 13 into the function. You could have
also used the functions in the following way, by putting the number 13 in each
of the functions' pin parameters:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
This is called hardcoding because you're writing the value directly into each
function as opposed to referring to it as data from elsewhere. There are a few
reasons you'll want to avoid hardcoding these values.
For one, creating a variable and giving it a good name makes your code easier
to understand. If you have a motor on pin 12 and an LED on pin 13 and you
want to turn the motor on and the LED off, this code is a little easier to un-
derstand:
digitalWrite(motorPin, HIGH);
digitalWrite(ledPin, LOW);
than this:
digitalWrite(12, HIGH);
digitalWrite(13, LOW);
In that last example, you'd have to remember which pin is which, or else you'd
have to look it up.
Search WWH ::




Custom Search