Lesson 2

Lesson 2: Blinking LEDs

In this lesson you will learn how to control the state of (4) user-controlled LEDs.

Prerequisites

Before starting this lesson be sure to follow the Initial Setup & Configuration. Also, we strongly recommend that you take some time to read "Getting Started with Arduino and Genuino products".

Introduction

Apeiros provides a total of (4) LEDs: (3) Green and (1) Red. LED2, LED3 and LED4 are green in color and LED5 is red in color. You can easily control the state (On/Off) of each LED by using several functions contained within the ApeirosIoExpander class library. In case you didn't already know, LED is an acronym for light emitting diode. LEDs are great because they don't consume a significant amount of current and can be driven directly by most microcontrollers. If you want to learn more about LEDs, then please check out this great tutorial.

 Apeiros Class Library Functions

The ApeirosIoExpander class library contains a total of (14) different functions for setting and getting the state of the (4) user-controlled LEDs. See code below for a list of all related LED functions.

void setLED2State(bool bState);
void setLED3State(bool bState);
void setLED4State(bool bState);
void setLED5State(bool bState);
void setAllLEDsState(bool bState);
bool getLED2State();
bool getLED3State();
bool getLED4State();
bool getLED5State();
void toggleLED2State();
void toggleLED3State();
void toggleLED4State();
void toggleLED5State();
void toggleAllLEDsState();

For example, let's examine how to set the state of LED2. Suppose we want to turn LED2 on. Recall that we need to first declare an instance of the ApeirosIoExpander class library and then call the begin() function from within the setup() function.

// Declare Apeiros class instances.//
/***********************************/
ApeirosIoExpander io;
/***********************************/

void setup() {
  // put your setup code here, to run once:
  
  // Initialize ApeirosIoExpander.
  io.begin();

}

Now we can call setLED2State() function from within the main() loop.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Set state of LED2 to be on.
  io.setLED2State(1);
  
  delay(150);  // Delay for a specified amount of time in milliseconds
}

Suppose we want to turn LED2 off. Simply pass a zero instead of a 1 when calling the setLED2State() function.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Set state of LED2 to be off.
  io.setLED2State(0);
  
  delay(150);  // Delay for a specified amount of time in milliseconds
}

What if we forgot the state of LED2? We can use the getLED2State() function and it will return the current state of LED2. Notice that we need to declare a boolean variable, ledState, so that we can store the returned state of LED2.

void loop() {
  // put your main code here, to run repeatedly:

  bool led2State;
 
  // Get current state of LED2.
  led2State = io.getLED2State();
  
  delay(150);  // Delay for a specified amount of time in milliseconds
}

Toggling the state of an LED is a relatively simple coding task. Simply turn the LED on, delay for a specified time and then turn the LED off. However, wouldn't it be nice to be able to call a single function repeatedly.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Toggle state of LED2.
  io.toggleLED2State();
  
  delay(150);  // Delay for a specified amount of time in milliseconds
}

Finally, we can toggle the state of all (4) LEDs by calling the toggleAllLEDsState() function.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Toggle state of all LEDs.
  io.toggleAllLEDsState();
  
  delay(150);  // Delay for a specified amount of time in milliseconds
}

TIP:

When using the toggleAllLEDsState() function we must consider the initial state of each of the (4) LEDs. If all four LEDs are on, then calling the toggleAllLEDsState() function will turn all (4) LEDs off. However, suppose LED2 & LED4 are initially on and LED3 & LED5 are off. In this case, calling the toggleAllLEDsState() function will turn off LED2 & LED4 and turn on LED3 & LED5. Imagine the cool possibilities!

EXPERIMENT


 

Play around with the LED functions and test out blinking LEDs. Can you create a custom sketch that turns each LED on in ascending order and then off in descending order? Use a delay of 500 milliseconds in-between each LED state change. Decrease the delay time and observe how the LEDs blink. What happens when the delay value gets to be less than 15 milliseconds?