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.

void setup() {
pinMode(13, 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.

void loop() {
  digitalWrite(13, HIGH);

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

delay(500);

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

  digitalWrite(13, LOW);
  delay(500);
}

Here’s the code in full.

void setup() {
pinMode(13, OUTPUT);
}

void loop() {
  digitalWrite(13, HIGH);
  delay(500);
  digitalWrite(13, LOW);
  delay(500);
}

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:

const int LED = 13;

The other is:

#define LED 13

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.

void setup() {
}

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.

void loop() {
  analogWrite(LED, 255);
  delay(500);

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

analogWrite(LED, 128);
delay(500);

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

  analogWrite(LED, 0);
  delay(500);
}

Here’s the code in full:

const int LED = 13;

void setup() {
}

void loop() {
  analogWrite(LED, 255);
  delay(500);
  analogWrite(LED, 128);
  delay(500);
  analogWrite(LED, 0);
  delay(500);
}

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.

const int LED = 13;
int brightness = 0;

Again, our setup() function is empty.

void setup() {
}

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.

void loop() {  
  if (brightness < 255)
  {
    brightness++;
  }
  else
  {
    brightness = 0;
  }

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.

  if (brightness < 255)
    brightness++;
  else
    brightness = 0;

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

analogWrite(LED, brightness);

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

  delay(10);
}

The code in full looks like this:

const int LED = 13;
int brightness = 0;

void setup() {
}

void loop() {
  if (brightness < 255)
    brightness++;
  else
    brightness = 0;
  analogWrite(LED, brightness);
  delay(10);
}

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

brightness = (brightness < 255) ? brightness + 1 : 0;

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

const int LED = 13;
int brightness = 0;

void setup() {
}

void loop() {
  brightness = (brightness < 255) ? brightness + 1 : 0;
  analogWrite(LED, brightness);
  delay(10);
}

Leave a Reply

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