Usando-LEDs-con-sentencias-condicionales-if-else

It´s time to start connecting something to our board! In this lesson we  will be switching various LEDs on and off.  You will need everything that we have explained so far, as we are going to be using it.

  • Communicating via serial port
  • Functions and variables

You will need 1 LED and a ZUM BT 328 controller board (or one compatible with Arduino).

Switching an LED on and off

Switching an LED on and off is one of the first programs with peripherals done using Arduino. Being the first one, we will look at it in more detail but we will do things faster in subsequent lessons.

First things first, is an LED  analogue or digital? An LED is a digital component, which can only have two values; on or off.
Secondly, is an LED an input or output component? An LED is an output component, as we have to command it, which means telling it whether to switch on or off, whereas a  sensor gives us the reading of a value.

So let´s start by connecting an LED to pin digital 8 of the board, as shown in the diagram:

zum-8led

As we have explained before, there are two key functions when programming in Arduino, setup() and loop(). The setup() function usually carries out all configuration and initialisation tasks, whereas the loop() function contains the commands to be executed by the program.

void setup()

The only configuration required is to indicate that digital pin 8 is output.

The pinMode() function takes the digital pin number as the first parameter and the work mode as the second, which can be INPUT or OUTPUT.

void loop()

Now we´re going to make the LED flash on and off. To do this we need to switch it on, wait a second, switch it off and then wait another second. As the loop() function is executed continuously, it will make the LED flash on and off. Let´s have a look at the code.

The digitalWrite() function  takes two parameters, the first is the number of the digital pin and the second is the state it is given: HIGH or LOW. When the state is HIGH, it sets the pin to 5 volts, whereas it is earthed when LOW (0 volts). This causes the LED to switch on or off. To be able to carry out a write action on a digital pin, you will first need to set the pinMode to OUTPUT. The delay() function pauses execution for the duration given in milliseconds.

If-else conditional sentences

A conditional sentence modifies the program flow of execution, according to a condition. For example, it could make a program that lights up the LED is character H is received from the serial port, or switches it off if character L is received. Let´s see how it´s done.

If-else structure

The if-else structure has the following structure

ifelse

If the condition is met, instructions 1 and 2 will be executed, and if not, instructions 3 and 4. NB – it is not essential for there to be an else. For more information, check the official documentation on the instructions if and else.

Acting according to a value

Now let´s see how to make the LED switch on if it receives the character H, and off if it receives the character L. Bear in mind the function of the previous lesson, where execution of the program was paused until something arrives from the serial port.

Let´s modify the function so that it saves the character received from the serial port. This value must be sent to another function (which we will create later) that manages what is done with the read value. We will call it manageChar(). Let´s see how the program turns out and then we´ll explain things.

int ledPin = 8;

We will save the number of the pin connected to the LED as a global variable. This will avoid any confusion when it comes to writing it in different parts of the program.

void manageChar(char c)

This function takes one character as a parameter. If the value of the said parameter is H (the equality comparison is written in Arduino as ==), the LED connected to the ledPin pin will switch on (8 in this case), but if the parameter is L, the LED will switch off. If the parameter was neither H nor L, this function wouldn´t do anything.

void waitForInput()

As we saw in the previous lesson, this function waits to receive data via the serial port. We have modified it so that it stores the read value in a char variable (remember that the Serial.read() function returns a char -byte-)

The read value will then be sent to the manageChar function so that it switches the LED on or off accordingly.

setup()

Set up pin 8 as an output pin and start the serial communication at 9600 bauds.

loop()

This is the function executed continuously, which in this case, will wait to receive data from the serial port and then, once the data has been received, it will manage it using manageChar() function and so on and so forth.

Exercise

Using everything that you have learnt, have a go at making the following program. I will publish the solution next week in the next lesson.

Make a program that reads the serial port and manages and the values read as follows:

  • First character: Indicates digital pin number, between 0 and 9
  • Second character: Indicates whether to switch the LED on or off, which is connected to the corresponding pin,  it could be worth H or L

This means that is the sequence 1H4L5H2L arrives via serial port, it should execute the following

  • LEDs connected to pins 1 and 5 on
  • LEDs connected to pins 4 and 2 off
  • The rest of the LEDs remain in the same state as before.

Go on, have a go at resolving it, share the code, and if you dare send us a video of how it works. We´ll look at the solution next time.