For this circuit need:
- Arduino Uno or similar. This session accepts any other model Arduino.Un PC with the Arduino environment correctly installed and configured.
- A breadboard.
- 8 LEDs.
- A 330 ohms resistor.
- Some cables Protoboard.
If we wanted to ride a circuit that have 8 LEDs and in which the light is shifted from one another, one possibility would be repeated several times the same sequences of instructions already know.
For example if we connect various LEDs for different digital pins Arduino, we should declare our function setup () could be:
void setup ()
{
// Initialize the digital pins as an output
pinMode (13, OUTPUT);
pinMode (12, OUTPUT);
pinMode (11, OUTPUT);
..............................
pinMode (6, OUTPUT);
}
And turn our loop () should be repeated as often as LEDs have the game on and off each of the LEDs in sequence from the pin 13 to 6.
This solution is what could be described as brute force, but not very elegant, is laborious and probably would commit more than one error in writing it, because people tend to make mistakes doing boring repetitive tasks.
Instead computers are not bored and C ++ also offers a convenient way to indicate that you repeat something a set number of times. For this medium is the instruction that can be used in combination with a variable.
A variable is a container that can take multiple values, in our case will accept all values between 6 and 13.
C ++ requires us to declare the type of the variables before use. In our case we use the integer type int is written to indicate that this variable is numeric and integer, without decimals.
We will see that there are other types of variables. We will return to this issue in future sessions.
For example, to initialize in our setup () pins from 13 to 6 as outputs (required by our Arduino) could use the for statement as follows:
void setup ()
{
int i = 0; // Initialize the variable i as an integer
for (i = 6; i <14; i ++)
pinMode (i, OUTPUT);
}
Although the syntax seems complicated at first, you get used quickly. The important thing here is that for needs 3 parameters separated by a semicolon character.
These parameters are, in this order:
A variable that anger taking values according to a certain rule, and to which we assign an initial value. In this case: i = 6.
The cycle continues as long as this condition is met. In our case while i is less than 14, that is until 13: i <14
As you change the variable at each iteration. In our case i ++ it is C ++ ask to increase the variable i by one, at the end of each iteration.
With the same criteria we could write the loop (function) and Download:
void loop ()
{
int i = 0; // Initialize the variable i as an integer
for (i = 6; i <14; i ++)
{
digitalWrite (i, HIGH);
delay (500);
digitalWrite (i, LOW);
delay (500);
}
}
In session 3 code was very similar except that wrote the value 13 for the only pin that had a connected LED. Here we assign the pin with a variable i, which is taking the values of 6 to 13 for the pin.
The circuit diagram is very similar to the Session 3, except for the fact that we place on the breadboard 8 LEDs.
The only novelty is that since the function of the resistance is to limit the intensity of the current flowing through the circuit, and since all diodes have common ground, just a single resistor between this point and Ground.
When our program to lift the corresponding pin HIGH value, the associated LED lighting up circuit is closed.
Below are a connection scheme circuit in a breadboard.
This setup allows us to play with lights and lends itself to several different programs to achieve different effects.
For example, with the previous program 4.1, the effect is not exactly fantastic car because when we just iterate for, the program starts again from the beginning, which causes light to jump from pin 6 to pin 13.
So Could we make light bounce? Think about a bit.
Of course yes, it would suffice to use two for loops, similar to the following Download:
void loop () // Prog_4_2
{
for (int i = 6; i <14; i ++) // we define the variable i on the fly
{
digitalWrite (i, HIGH);
delay (500);
digitalWrite (i, LOW);
delay (500);
}
for (int i = 12; i> 6; i--) // define the variable i on the fly
{
digitalWrite (i, HIGH);
delay (500);
digitalWrite (i, LOW);
delay (500);
}
}
Another serious variant, make a ripple effect in which the lights go up leaving pre lit LEDs until the maximum and now shutting down the upper LEDs. We recommend trying to solve the problem as a challenge, before seeking a solution.
No hay comentarios:
Publicar un comentario