if (analogRead(A0) > 500)
{
digitalWrite(led, HIGH);
}
else
{
digitalWrite(led, LOW);
}
This if statement turns the led pin on if an analog reading is greater than 500 or off if the reading is less than or equal to 500.
MAKING LOGICAL COMPARISONS
So far we have used two types of comparison: == (equal to) and > (greater than). Here are some more comparisons you can make:
<= less than or equal to
>= greater than or equal to
!= not equal to
You can also make more complicated comparisons using logical operators like && (and) and || (or). For example, to turn an LED on if a reading is between 300 and 400, you could write the following:
int reading = analogRead(A0);
if ((reading >= 300) && (reading <= 400))
{
digitalWrite(led, HIGH);
}
{
digitalWrite(led, LOW);
}
In English, this code might read, “If the reading is greater than or equal to 300 and the reading is less than or equal to 400, then turn the LED on.” Since we’re using the && operator to specify that both conditions must be true, if either condition is not met, the LED remains dark.
GROUPING CODE INTO FUNCTIONS
Functions can be confusing if you’re new to programming. Functions are best thought of as ways to group together lines of code and give them a name so that the block of code is easy to use over and over again.
Built-in functions such as digitalWrite are more complicated than they first seem. Here is the code for the digitalWrite function:
void digitalWrite(uint8_t pin, uint8_t val)
{
uint8_t timer = digitalPinToTimer(pin);
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *out;
if (port == NOT_A_PIN) return;
// If the pin that support PWM output, we need to turn it off
// before doing a digital write.
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
out = portOutputRegister(port);
uint8_t oldSREG = SREG;
cli();
if (val == LOW) {
*out &= ~bit;
} else {
*out |= bit;
}
SREG = oldSREG;
}
Since someone already wrote the digitalWrite function, we don’t have to worry about what all this code does; we can just be glad that we don’t have to type it all out every time we want to change pin from high to low. By giving that big chunk of code a name, we can just call the name to use this code.
You can create your own functions to use as shortcuts for more complicated chunks of code. For example, to create a function that makes an LED blink the number of times you specify as a parameter, with the LED pin also specified as a parameter, you could use the sketch below. This function is named blink, and you can call it during startup so that the Arduino L LED blinks five times after a reset.
➊ const int ledPin = 13;
➋ void setup()
{
pinMode(ledPin, OUTPUT);
➌ blink(ledPin, 5);
}
void loop() {}
➍ void blink(int pin, int n)
{
➎ for (int i = 0; i < n; i++)
{
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
}
At ➊, we define the pin being used. The setup function at ➋ sets ledPin as an output and then calls the function blink ➌, passing it the relevant pin and the number of times to blink (5). The loop function is empty and does nothing, but the Arduino IDE insists that we include it even if it serves no purpose. If you don’t include it, you will get an error message when you install the program.
The blink function itself begins at ➍ with void. void indicates that the function does not return any value, so you cannot assign the result of calling that function to a variable, as you might want to do if the function performed some kind of calculation. Then follows the name of the function (blink) and the parameters the function takes, enclosed within parentheses and separated by commas. When you define a function, you must specify the type of each of the parameters (for example, whether they are int or float). In this case, both the pin (pin) and the number of times to blink (n) are int values. Lastly, at ➎, we have a for loop that repeats the digitalWrite and delay commands inside it n times.
That’s it for the software crash course. If you want to learn more about programming for Arduinos, visit http://www.arduino.cc/ before everyone at your Internet service provider becomes a zombie.
ASSEMBLING A SCREWSHIELD
Many of the projects in this book use a screwshield that fits over the Arduino sockets and allows you to connect wires to Arduino pins using screw terminals. Not all wires will fit into the normal Arduino sockets, but almost any thickness of wire will fit securely in a screw terminal and won’t come loose. There are various screwshields on the market, all with slightly different layouts. In this book, I use the popular model from Adafruit (the proto-screwshield, part number 196), which is provided as a kit that you have to solder together. There are lots of connections to make, but none of them are difficult. The component parts of the proto-screwshield are shown in Figure C-7.
Figure C-7: The parts of Adafruit’s Proto-Screwshield
The screw terminals line the edge of the board and Arduino pass-through headers. The screwshield pass-through headers slot through the shield into the PCB. You can plug wires into these as you would in the Arduino Uno, and they have sockets on the top side so you can plug still another shield on top.
Of the two LEDs, one is a power LED that indicates when the board is powered up, and the other is for you to use in your build. You don’t have to solder either LED in place if you don’t need them. The push button is a reset switch, which can be useful as it’s hard to get at the Arduino’s reset button when the screwshield is in place. Again, it is by no means essential.