Hardware Reference
In-Depth Information
Functions
A function is a portion of code that can be called, with parameters if required,
and returns data if required. If you write a long list of repeating statements in
code, or if you have created code that needs to be called several times, it may
be useful to create a function.
The main program is running and then calls a function, called addTwo() ,
with two parameters: 12 and 30 . The function is run and data is returned. The
program then returns to where it was.
A function requires a data type, even if it does not return any data. If no data
is to be returned, then the data type void must be used. The contents of the
function are contained within curly brackets. In the addTwo() function shown
above, it returned an int datatype, indicated when it was i rst declared.
Libraries
The Arduino programming environment comes with a standard library, a
library of functions that are included in every sketch. However, the Arduino is
also an embedded system, so the standard library contains the strict minimum.
By default, it can handle basic mathematical operations, and set pins to digital
or analog input and output, but it cannot write data to an SD card, connect
to WiFi, or use a TFT screen. These devices that are not standard on Arduino
boards. Of course, an Arduino can use these devices when they are available,
but to use these devices, a library for the specii c device must be imported into a
sketch. Otherwise, there is no point in having the extra functionality that could
potentially take up space on a device where space is critical.
Adding a library to your sketch adds more functionality and allows you, the
programmer, to use new functions. For example, by importing the EEPROM
library, you can access the internal EEPROM by using two new functions: read()
and write() . The standard library will be presented in Chapter 4, and different
libraries are presented throughout the topic.
CROSS REFERENCE
Chapter 6 explains EEPROM technology and the EEPROM
library.
Summary
This chapter showed you how to create your i rst Arduino sketch and walked you
through it step by step. Arduino has developed all the tools required for you to get
started programming, and they are delivered in a simple package that contains
everything you need, from writing your programs to l ashing the i nal binary i le.
 
Search WWH ::




Custom Search