1. Turning the LEDs on and off¶
Goals¶
- Turn on and off the led diodes of the Picuino UNO shield.
- Generate led blinking by waiting time.
Management library for Arduino¶
In order to start programming, it is necessary to previously download and install the library needed to program the Picuino UNO shield
In the following web page you can see how the necessary steps for the installation of a library for Arduino.
The ledWrite
function¶
-
ledWrite
(int ledNum, int bright)¶ This function allows you to turn a specific led on or off. Its parameters are the following:
ledNum
: led to turn on or off. Valid values range from 1 for led D1 to 8 for the blue color of led D6.The D6 led is an RGB led, which integrates 3 leds inside. The numbers 6, 7, 8 respectively represent the red, green and blue colors of led D6.
bright
: brightness with which the led will light up.LED_OFF
: turns off the led. It can be replaced by the number zero.LED_ON
: turns on the led with maximum intensity.- Values between 2 and 255: turn on the led with an intensity proportional to the number.
Turn on led D1¶
The following program will permanently light led D1
1 2 3 4 5 6 7 8 9 10 11 | // Enciende el led D1
#include <Picuino.h>
void setup() {
pio.begin(); // Inicializar el shield Picuino UNO
pio.ledWrite(1, LED_ON); // Encender el led D1
}
void loop() {
}
|
LED D1 flashing¶
If you want to turn led D1 on and off with a period of one second, one way to do it is to turn on the led, wait half a second, turn off the led and wait half a second. When this sequence is repeated, the led flashes. To achieve a half-second wait, use the delay(500)
function, which causes a 500-millisecond (0.5-second) wait. To make the sequence loop indefinitely, the code is inserted inside the loop()
block, which loops over and over once the Arduino program is started.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Parpadea el led D1
#include <Picuino.h>
void setup() {
pio.begin(); // Inicializar el shield Picuino UNO
}
void loop() {
pio.ledWrite(1, LED_ON); // Encender el led D1
delay(500); // Esperar medio segundo (500 ms)
pio.ledWrite(1, LED_OFF); // Apagar el led D1
delay(500); // Esperar medio segundo (500 ms)
}
|
Exercises¶
Program the code needed to solve the following problems.
Turn on leds D1, D3 and D5 permanently.
Turn on led D1, wait one second, turn on led D2, wait one second and continue like this until the first 5 leds are on. Once finished, the program will keep the 5 leds on.
Turn on the red, amber and green leds as in a traffic light.
First the green led will light up and stay on for 4 seconds. Then the green led will turn off and the amber led will light up for 2 seconds. Finally, the amber led will turn off and the red one will turn on, which will remain on for 4 seconds.
This sequence will repeat indefinitely.
Modify exercise 3 so that the amber led flashes three times, changing from on to off every half second, before turning on the red led.
Program a beacon that turns on alternately the red led and the blue led. Each led should stay on for one second. One of the two LEDs must always be on and both LEDs will never be on at the same time.
Turn on led D1, wait a second, turn off led D1 and turn on led D2. In this way, the first 5 leds will light up consecutively. At each moment only one led will be on. Once the sequence is finished, it will start over from the beginning.
Make a modification to the previous exercise so that at all times there are 2 LEDs on. When the program starts, leds D1 and D2 should light up. After a second, leds D2 and D3 will light up. The sequence will continue until reaching the initial situation.
The following program has several errors, correct the syntax errors so that it works 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
// Enciende 6 ledes en secuencia a derecha y a izquierda // Programa con errores sintácticos #include <Picuino.h> void setup() { pio.begin(); // Inicializar el shield Picuino UNO pio.ledWrite(1, LED_on); } void loop() { for(char i=1; i<=5; i++) { pio.ledwrite(i+1, LED_ON); // Encender el led siguiente delay(250); // Esperar 250 milisegundos pio.ledWrite(i, LED_OFF); // Apagar el led anterior delay(100); // Esperar 100 milisegundos } for(char i=5; i>=1; i--) { pio.ledWrite(i, LED_ON); // Encender el led anterior delay(250); // Esperar 250 milisegundos pio.ledWrite(i+1, LED_OFF); // Apagar el led siguiente delay(100); // Esperar 100 milisegundos }
Make a program that makes D1 blink once per second while D2 blinks twice per second.