# 4. LED flashing¶

## Goals¶

• Generate led blinking using the ledBlink function.

## LED D1 flashing¶

In this example, you want to make led D1 blink with a half second on time and half a second off time. The flashing period will therefore be one second. In this case the function will be given from the `setup()` block only once.

Equivalent program in the Ardublock environment:

In this link you can download the `program file for Ardublock environment 'ledBlink'`

## Exercises¶

Program the code needed to solve the following problems.

1. Make led D1 and led D4 blink with a half second on time and half a second off time. The two LEDs must turn on and off at the same time. Use the `ledBlink()` function.

2. Modify the previous exercise so that the lighting of the two leds D1 and D4 alternates, so that only one led is on at any time. The lighting time of each led will be half a second.

3. Make two LEDs blink at the same time with a frequency of one second. LED D1 will be programmed with the function `ledBlink(1, 500, 500)`, on the contrary, led D3 will be programmed with the following code.

 ```1 2 3 4 5 6``` ```void loop() { pc.ledWrite(3, LED_ON); // Encender el led D3 delay(500); // Esperar medio segundo (500 ms) pc.ledWrite(3, LED_OFF); // Apagar el led D3 delay(500); // Esperar medio segundo (500 ms) } ```

You should try to synchronize the two leds so that they blink at the same time by adjusting the lighting times by modifying the time of the `delay(500)` function.

4. Correct the syntax errors in the following program to make it work correctly.

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28``` ```// Programa con errores sintácticos. // Luces de Navidad. #include #include void setup() { int time_on; // Declara la variable time_on como un número entero int time_off; // Declara la variable time_off como un número entero pc.begin(); // Inicializar el módulo PC42 // Repite y asigna valores a variable 'num' desde 1 hasta 5 for(int num=1; num<=5; num++) { // Tiempo encendido = aleatorio entre 0,5 y 3,0 segundos time_on = random(500, 3000) // Tiempo apagado = aleatorio entre 0,5 y 3,0 segundos time_off = Random(500, 3000) // Parpadea el led 'num' un tiempo aleatorio pc.ledblink(num, time_on, time_off) } void loop() { } ```
5. Make a led blink so that it lights up for two tenths of a second every ten seconds, to indicate that the module is working using very little energy.

6. Make a led blink so that it lights up for two tenths of a second, every second.

7. The first six leds must light up in sequence so that led D1 begins to light up and ends up with led D6. Between the lighting of one LED and the next, between 250 and 500 thousandths of a second must pass. Once all the leds are on, they will start to turn off by led D1 until they are all off. Note: To solve this problem, only one `ledBlink()` function should be used for each led, along with a delay() function to wait a time between one power on and the next.