Digital Functions

Now that we’ve seen the digital functions in action, let’s look at each of them individually in greater detail to better understand how they work.

pinMode()

Before we can put the other digital functions to work, we first need to let the Arduino know how we plan to use its digital I/O pins. To do this, we use a function called pinMode() to set up which pin we are going to use and how we intend to use it. Its syntax is fairly simple, as follows:

pinMode(pin, state)

Inside the parenthesis of the function are two values separated by a comma. The first value is the number of the pin that we are setting up. This could be a number or variable with the value ranging from 0 to 13 or A0 to A5 (if using the Analog In pins for digital I/O) corresponding to the pin number printed on the interface board.

The second value is the state that we need for the pin to work with our circuit and can include either of two predefined constants: INPUT or OUTPUT. Using INPUT in a pinMode() function will place the pin in a state of high impedance, where the pin is configured to receive an incoming signal yet places very little load on the overall circuit. This is good for reading sensitive inputs without affecting the sensor in any way. A digital input pin is only sensitive to two values, HIGH and LOW.

By using OUTPUT, the digital pin is placed in a state of low impedance and can, therefore, source or sink a respectable current, considering the size of such a little chip, of 40 milliamps to other circuits. This is enough current to brightly light up an LED while providing little resistance to the rest of the circuit.

In this project, we use pinMode() two different ways:

pinMode(speakerPin, OUTPUT);
pinMode(sensorPin, INPUT);

The first line sets the speaker pin to an OUPUT state so that we can connect and power a small external speaker. The second line establishes the sensor pin as an INPUT, making it ready to receive signals from a low power sensor or other input.

For every digital I/O pin that we need to use, we must first establish its mode once in the program, usually done in the setup() function. This can get a bit tedious if we plan to use all 14 digital pins as outputs. Instead, we can use a for loop as shown in previous examples to set up each pin as an output, like in the following example:

for (int i=0; i<14; i++) pinMode(i, OUTPUT);

This line is a simple for statement used to repeat a line of code a total of 14 times, placing each digital pin into an OUTPUT mode. This could come in handy and save you some typing if you’re trying to hook up 14 LEDs to one Arduino.

digitalWrite()

Once we have a pin established as an OUTPUT, it is then possible to turn that pin on or off using the digitalWrite() function. Its syntax follows:

digitalWrite(pin, state)

There are two statements that are used with this function that include pin number and state. Just like pinMode(), this could be a number or variable with the value ranging from 0 to 13 or A0 to A5. The second statement is the state of the output pin that corresponds to the two predefined constants: HIGH and LOW.

HIGH is the state of sourcing current and provides a connection to +5 VDC. LOW, the default of any output pin, is the state of sinking current, providing a connection to ground. If the circuit is configured similar to Figure 5-2, where the output pin is configured to source current for a circuit, setting the pin HIGH is basically turning the circuit on, while setting the pin LOW turns it off. This would be reversed if the circuit is more like Figure 5-3 and the output pin needs to sink current in order to turn on the circuit. This is why in this book we will generally stick to sourcing current in our circuits so that HIGH will most likely mean “turn something on.”

Just like we did with the pinMode() function, it is possible to turn on or off all of the pins using a for loop. Take the following code sample:

for (int i=0; i<14; i++) digitalWrite(i, HIGH);
delay(1000);
for (int i=0; i<14; i++) digitalWrite(i, LOW);
delay(1000);

By substituting i in digitalWrite(i, HIGH) with the current iteration of the loop, this source code will turn on all 14 pins in order so quickly that it will appear that they all come on simultaneously. The rest of the code pauses for 1 second, turns each of the pins off, then pauses for another second. If you connected a series resistor and LED to each pin, then this code would blink all 14 LEDs on and off every second. To create a more interesting pattern, we could rewrite the sample like the following:

for (int i=0; i<14; i++) {
  digitalWrite(i, HIGH);
  delay(1000);
}
for (int i=13; i>=0; i--) {
  digitalWrite(i, LOW);
  delay(1000);
}

The first for loop will turn on each of the 14 pins individually, pausing for 1 second between each one, starting with pin 0 and ending with pin 13, until all of the pins are on. The next loop begins with pin 13 and ends with pin 0, turning off each pin individually with a 1 second pause in between each one until none of the pins are on.

digitalRead()

With a digital pin configured as an INPUT, we can read the state of that pin using the digitalRead() function. Its syntax is fairly basic, as follows:

digitalRead(pin)

Here, we specify the pin number of the INPUT pin using either a variable or a numerical constant. What we do with this reading depends on maybe two different ways to use this function. The first is to assign the state of the pin to a variable. The second is to use the function in the place of a variable. For example, if we take the following statement:

sensorState = digitalRead(sensorPin);
if (sensorState == HIGH) digitalWrite(ledPin, HIGH);

In these two lines of code, digitalRead() is used to read the input pin and this reading is assigned to a variable. The next line performs a conditional test on the variable and if this variable is equal to HIGH, then it will make an output pin HIGH. We could rewrite these lines of code in one functionally identical line instead, as follows:

if (digitalRead(sensorPin) == HIGH) digitalWrite(ledPin, HIGH);

This somewhat compact line of code uses the digitalRead() function in the place often occupied by a variable, so that if the reading of the sensor pin is equal to HIGH, then the rest of the line is executed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.243