• No se han encontrado resultados

1.3.6. PROBLEMA DE LA PAPR ELEVADA

1.3.6.3. Métodos de reducción de la PAPR

In this chapter, we will cover the following recipes:

f Blinking LED without delay() f Connecting an external LED f Fading the external LED f RGB LED

f LED bar graph

f The 7-segment display

Introduction

In this chapter, we will explore LEDs with the Arduino. The fastest way to get some feedback from a system or from the Arduino is via an LED. They are simple devices which are either on or off. However, they form the basis for advanced technologies such as LED TVs, projectors, or

lasers. In this chapter, we will also see how to use them efficiently and explore some interesting

applications for them.

LED stands for Light Emitting Diode and, in its core, it's just a diode that emits light. LEDs are

incredibly common these days and can be found at any common electronics shop. Radioshack, Digikey, Farnell, Sparkfun, Adafruit, or Pololu are just a few places we can buy LEDs from, online.

Blinking LED without delay()

It is easy to make the LED blink on an Arduino. We turn it on, wait, turn it off, wait again, and then we repeat the cycle. However, this wait state will completely halt the Arduino execution. We want to make the LED blink while the Arduino is performing other actions.

Getting ready

For this recipe all you need is an Arduino board connected to the computer via USB.

How to do it…

The following code will make the internal LED blink on the Arduino without ever using the delay() function:

// Variable for keeping the previous LED state

int previousLEDstate = LOW;

unsigned long lastTime = 0; // Last time the LED changed state int interval = 200; // interval between the blinks in milliseconds void setup() {

// Declare the pin for the LED as Output

pinMode(LED_BUILTIN, OUTPUT); }

void loop(){

// Here we can write any code we want to execute continuously // Read the current time

unsigned long currentTime = millis();

// Compare the current time with the last time

if (currentTime - lastTime >= interval){

// First we set the previous time to the current time

lastTime = currentTime;

// Then we inverse the state of the LED

if (previousLEDstate == HIGH) { digitalWrite(LED_BUILTIN, LOW); previousLEDstate = LOW; } else { digitalWrite(LED_BUILTIN, HIGH); previousLEDstate = HIGH; } } }

While most Arduinos have the LED on pin 13, some don't. To make sure

we are addressing the correct LED pin, we can use the LED_BUILTIN

constant. This is already defined in the Arduino language and will always

equal the LED pin number of the Arduino board that has been used.

How it works…

The big difference between a normal LED blinking program and this one is that we don't

use the delay() function. The delay() function simply stops the code execution until

the specified amount of time passes. Here, we track the internal time of the Arduino; when

enough time passes, we change state. The internal time since the start of the Arduino is accessible using the millis() function, which will return the time—in milliseconds—since the program started working.

This approach is called non-blocking, since it doesn't block the execution of our code. The

delay() function is considered to be a blocking function, as it blocks code execution. Breaking down the code

The code tracks the amount of time passed and changes the state of the LED if enough time has passed.

We need a few variables. The previousLEDstate variable will store the last state of the LED. The lastTime variable remembers when the LED state changed from HIGH to LOW or from LOW to HIGH. When we set a pin as HIGH, it will output 5 V. When we set it as LOW, it will just go to 0 V.

The interval variable is the interval in milliseconds at which we want the LED to change state.

// Variable for keeping the previous LED state

int previousLEDstate = LOW;

unsigned long lastTime = 0; // Last time the LED changed state int interval = 200; // interval between the blinks in milliseconds

In the setup() function, we set the LED pin as an output:

void setup() {

// Declare the pin for the LED as Output

pinMode(LED_BUILTIN, OUTPUT); }

The important part comes in the loop() function. The first step is to record the time since

the Arduino began running the program. The millis() function returns very big numbers; variables getting data from this function should always be declared as long or unsigned long. Unsigned variables can only take positive values, from 0 to the maximum allocated space. For example, a normal long variable can take values from -2,147,483,648 up to 2,147,483,648, while an unsigned long can go from 0 up to 4,294,967,295.

unsigned long currentTime = millis();

Now, we need to see if enough time has passed since the last time we changed the state of the LED. For this, we compare with the previous time. If the difference between the current time and the last is bigger than the interval we declared, we can proceed to change the state of the LED:

if (currentTime - lastTime >= interval)

When the interval has passed, we first record the new time as being the previous time. By doing

this, we reset the time to which we will compare the next time. Then, we check what the previous LED state was and we set the LED to the opposite state. If it was LOW we set it to HIGH and if it was HIGH, we set it to LOW. The previous LED state variable is also set to the new LED state:

lastTime = currentTime;

// Then we inverse the state of the LED

if (previousLEDstate == HIGH) { digitalWrite(LED, LOW); previousLEDstate = LOW; } else { digitalWrite(LED, HIGH); previousLEDstate = HIGH; }

See also

The Button debouncing recipe in Chapter 3, Working with Buttons, for other topics which avoid the delay() function

Documento similar