1 minute read

How to Use Arduino Shields

Next Article
Index

Index

CHAPTER 6: Hacking Arduino 127

{ noTone(soundPin); } }

First, we define the variables for the pins. The switches will be connected to “sw1pin” and “sw2pin”. These will be digital inputs, while the “soundPin” will be a digital output.

Note that in the setup function for the switch pins, we use the command “pinMode” with the parameter INPUT_PULLUP. This sets the pin to be an input, but also enables a “pull-up” resistor built into the Arduino, which keeps the input pin HIGH, unless we pull it LOW by pressing the button.

It is because the input pins are normally high that in the “loop” function, when we are checking to see if a button is pressed, we have to use the “!” (logical not). In other words, the following will only sound the tone if the digital input pin “sw1pin” is LOW.

if (! digitalRead(sw1pin)) { tone(soundPin, 220); }

The “tone” function is a useful built-in Arduino function that plays a tone on a particular pin. The second parameter is the frequency of the tone in Hertz (cycles per second).

If no key is pressed, then the function “noTone” is called and stops any tone that is playing.

How to Use Arduino Shields

The success of Arduino had been in no small part due to the wide range of plug-in shields that add useful features to a basic Arduino board. A shield is designed to fit into the header sockets of the main Arduino board. Most shields will then pass through these connections in another row of header sockets, making it possible to construct stacks of shields with an Arduino at the bottom. Shields that have, say, a display on them, will not normally pass through in this way. You also need to be aware that if you stack shields like this, you need to make sure there are no incompatibilities, such as two of the shields using the same pin. Some shields get around this problem by providing jumpers to add some flexibility to pin assignments.

This article is from: