LESSON 11 – VOID
FUNCTIONS FUNCTIONS A function is simply a
section of code that is organized together to perform a task. We can execute the code inside the function
by calling the function with its name. TWO KEY FUNCTIONS Arduino programs require
that you have a setup() function and a loop() function. These two functions are special because
they are autimatically called by the Arduino. However, we can create
other functions that we can call on our own. MODULARITY Functions allow us to
organize code and give it a name. By
doing so, we are able to think on a higher level about the task at hand
because the problem is broken down in many parts. VOID Some functions are used to
calculate something and then return the calculated value to where they were
called. Other functions do not return
anything and are used to make something happen (such as output something to
screen or turn on LEDs). Such
functions are void functions. EXAMPLE 1 The following program/circuit
will create a void function called blinkLED(). This function will take care of turning an
LED on for 1 second and then turning it off.
We then call this function
from inside the loop() function. To
provide some variation, inside the loop() function, we have a delay that
ranges from 0 to 5 seconds.
PARAMETERS It is sometimes important
to provide information to a function.
For example, in the program above, perhaps we want to state how long
the blinkLED() function should light up the LED. We can do this using parameters (or
arguments). The following example is
the same as the previous one except the blinkLED() function now has a
parameter that states how long the LED should light up. Notice that the call to that function provides
the matching information.
The
parameter called duration is an integer.
It is just like any other variable except it gets its value from the
function call. So above, duration gets
the value 2000 for the first call and the value 500 for the next one. |
TRY THIS… PRACTICE 11-1 RESEARCH Before starting, go
research a little bit about Morse code.
You should understand what a dot and a dash are. CIRCUIT Series circuit: From pin
#2, go to an LED. From the LED go to a
resistor. From the resistor, go to
ground. CODE You will write a program
that will display SOS in morse code using the LED in your circuit. STEP 1 – GLOBAL VARIABLES First, you need a global
variable that will hold your base period of time. I’d set it to 1000 to start. This will be slow enough for Morse code
newbies like us to read the message. You also need a variable
called ledPin that will hold the number of the pin connected to the LED. STEP 2 – SETUP Inside setup, give values
to your two global variables from above.
Also set the pinMode for the ledPin to OUTPUT. STEP 3 – VOID FUNCTIONS You need to create all of
these void functions: dot() – Puts LED on for
one period of time dash() – Puts LED on for
three periods of time gap() – Puts LED off for
one period of time gapLetter() – Puts LED off
for three periods of time gapWord() – Puts LED off
for seven periods of time STEP 4 – LOOP Inside loop, put the functions
calls needed to display S, then O and then S.
For example, for S, we need: dot(); gap(); dot(); gap(); dot(); gapLetter(); That’s it! |