TB2 002: LED

Part 1

For our next trick, let’s look at the LED. NOTE: we’re ignoring the LCD for now, so you’re going to get unexpected things  written on the display. Just roll with it.

First off, let’s blink the LED. The LED is connected to i/o pin 13. In our setup() function, we define pin 13 as an output.

As the name suggests, the  loop() function loops indefinitely as long as the TB2 has power. First, we turn on the LED by setting pin 13 to HIGH with the digitalWrite() function.

Now we wait for half a second (500 milliseconds) with the delay() function.

Then we turn the LED off by switching the pin LOW, and then wait for another half second. We close the loop() function with }.

Here’s the code in full.

Play around with the delay times and see how that changes the blinking pattern.

Part 2

As it happens, i/o pin 13 is a special PWM pin. In addition to using it in a digital way (ie. on and off), we can use an analogWrite() function to send it a value between 0 and 255, allowing us to vary the brightness of the LED. To tidy our code slightly, we’re going to give pin 13 a name (LED) by assigning it to a constant variable (constant variables don’t change in the course of the execution of the code) – it’s not strictly necessary to do so here, but we may as well. There are two ways to declare a constant variable. The first is:

The other is:

Take your pick.

When we use a pin as an analog output, we don’t have to set a pin mode in the setup() function, so our setup is empty.

In our loop() function we’re using analogWrite() instead of digitalWrite(). You’ll notice we’re calling the pin by its name LED now. Setting the a value of 255 is the equivalent of turning the LED on to full brightness. Then we wait for half a second like before.

Let’s set the LED to half brightness now by sending it a value of 128 before waiting for another half second.

Finally we turn the LED off with a value of 0 before waiting again and closing the loop.

Here’s the code in full:

Part 3

How about doing a fade-in on the LED? It just requires sending increasing analogWrite() values in quick succession. We’ll use the variable brightness to keep track of the value we want to send to the LED – we assign it an initial value of 0.

Again, our setup() function is empty.

Now we’re going to use an if/else-statement to increase the value of brightness each time through the loop. (The Arduino website has good info on the basics – here’s the bit on if/else statements.)As long as the value is less than 255, we increment it by 1 with brightness++ (it’s the same as saying brightness = brightness + 1). If brightness is not less than 255 (ie. it is 255 now), assign brightness a value of 0 again.

Actually we can leave out the curly braces if there’s only one line after the if and else statements – it looks a little nicer.

Now that we know what the value of brightness should be this time through the loop, we can send it to the LED.

We’ll only wait for 10 milliseconds this time before jumping back to the top of the loop again.

The code in full looks like this:

By the way, there’s a cool shorthand way of writing the four lines of the if/else statement above. It looks like this:

It’s called the ternary operator and I use it quite a bit in my code. Try the full code for the same result.

Leave a Reply

Your email address will not be published. Required fields are marked *