2. 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Parpadea el led D1 una vez por segundo

#include <Picuino.h>

void setup() {
   pio.begin();               // Inicializar el shield Picuino UNO
   pio.ledBlink(1, 50, 50);   // Parpadea el led D1
                              //   50 centésimas de segundo encendido
                              //   50 centésimas de segundo apagado
}

void loop() {
}

Exercises

Program the code needed to solve the following problems.

  1. Make led D1 and led D4 flash every one second. 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 ledBlink(1, 50, 50) function, on the contrary, led D3 will be programmed with the ledWrite() function. First led D1 will start to flash and, after a second, led D3 will. The two leds must be synchronized so that they blink at the same time by adjusting the on and off times with the delay() 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
    // Luces de Navidad.
    // Programa con errores sintácticos.
    
    #include <Picuino.h>
    
    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
    
       pio.Begin()    // Inicia el shield Picuino UNO
    
       // 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(50, 300)
    
          // Tiempo apagado = aleatorio entre 0,5 y 3,0 segundos
          time_off = Random(50, 300)
    
          // Parpadea el led 'num' un tiempo aleatorio
          pio.ledblink(num, time_on, time_off)
    
    }
    
    void loop() {
    }
    
  5. Make a led blink so that it lights up for one tenth of a second every ten seconds, to indicate that the module is on, using very little electrical current.

  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, together with a delay() function to wait for a time between one flash and the next.