This is the next part of our Arduino tutorial series and this time we will learn and use arrays to create a small Christmas ornament with various flashing sequences. This would be the perfect project to keep the kids busy if you want to teach them the basics of soldering — just mount the LEDs on a piece of card and you can be powered by a standard 9V battery.

It’s also a key lesson in Arduino programming for beginners, although unless you’re planning on using it as a decoration, I’d highly recommend using it anyway.

Note. This is a tutorial for beginners, and we certainly will not open new paths — is it? just a device to teach how to use arrays and For loops to work with lots of LEDs (or other output devices).

If you haven’t already, now is the time to follow the other articles in the series:

  • What is an Arduino and what can you do with it ?
  • What is an Arduino starter kit and what does it contain?
  • More cool components to buy with your starter kit
  • Getting started with your Arduino starter kit? Installing Drivers and Configuring the Board and Port
  • Fritzing, a free circuit diagram drawing tool
  • A Closer Look at the Arduino Application Structure and Blink Sample Program

For this project, you will need at least 8 or 9 LEDs red or green, resistor for each of them, bread board and some connecting wires. The starter kit from Ooomlout, which I recently purchased myself and is pictured in this tutorial, offers great value for money and has more LEDs and resistors than you’ll ever need, and comes with a neat breadboard and Arduino case for storage. careful.

Here’s the last thing:

arduino christmas lights

And a video of it in action.

Here is a view of the wiring from Fritzing. It’s very simple — just connect the positive lead of the LEDs to the pins 2-> whatever (up to pin 13) and connect the negative legs to ground built into the resistor. The value I have used here is 560 ohms. That’s it for wiring.

arduino christmas lights

In terms of software, think about how you can write all those LEDs in code. You can do it like this:

  int led1 = 2;  // первый светодиод на контакте 2
 int led2 = 3;  // второй на контакте 3
 // и т. д.

 void loop () {
	 digitalWrite (LED1, HIGH);
	 задержки (100);
	 digitalWrite (LED1, LOW);
	 задержки (100);
	 digitalWrite (LED2, HIGH);
	 // так далее

You should see that with 9 LEDs this will tire you out quickly. The answer lies in arrays which if you can’t remember our Programming 101 about basic data types — mostly just lists.

The syntax is as follows (put this as the first line in your code):

  int leds [] = {2,3,4,5,6,7,8,9,10}; 

The square brackets indicate what? LEDs? variable will array . The curly braces enclose the list of pin numbers that our array will contain.

Now, to use an array, we need to refer to it by index number. Index starts from 0 and therefore will always be 1 less than the total number of things inside it ( so with 9 elements, the last one will have index 8 ).

You write it like this:

  светодиоды [0] 

Which in our case would pick the number 2 because that’s what’s at index 0 in our array.

Following so far? Excellent. This alone is not enough for us — we also need some way to iterate through each element of our array of LEDs. For this we will use for loop . The syntax for this is as follows:

  for (начальная переменная; условие, при котором мы повторяем снова; меняем переменную на каждой итерации) 

For example:

  для (int i = 0; i <9; i ++) 

who says

  • start this loop with a variable i whose value is equal to zero
  • keep looping only when i less than 9 ? (so: 0,1,2,3,4,5,6,7,8)
  • every time add 1 to i (i++ is a short way of saying i = i + 1)

In general, the loop will repeat as many times as we have LEDs, and each time it repeats we will have a variable i which we can use however we like.

We will use this structure twice to begin with. Once inside the setup function, all of our pins are switched to output mode, like so:

  void setup () {
    для (int i = 0; i <9; i ++) {
        pinMode (светодиоды [I], OUTPUT);

Do you see what we did there? Instead of writing 9 lines of code to declare each individual output as output, we create a for loop that iterates 9 times, each time setting a different output.

Now you should see how we can do the same in the main program loop to turn on each LED in sequence:

  void loop () {
  для (int i = 0; i <9; i ++) {
       digitalWrite (светодиоды [I], HIGH);
       задержки (100);
       digitalWrite (светодиоды [I], LOW);

Try it. You can download the complete code for today’s project here if you don’t want to type it in again (although I encourage you as it helps the learning process).

Okay, now we have a pretty boring lighting sequence. Let’s program another one. Just for fun, let’s do this completely randomly. Replace the main loop code with the following:

  void loop () {
 int randomLed = random (0,8);
   digitalWrite (светодиоды [randomLed], HIGH);
   задержка (50);
   randomLed = случайный (0,8);
   digitalWrite (светодиоды [randomLed], LOW);

Instead of using a for loop to iterate through each LED, we choose a random number between 0 and 9 and turn it on.

I’m going to leave it there for today as you should now have enough knowledge to program all new sequences and experiment with loops. To prove how easy it all is, I asked my wife to think of a sequence she would like to see and then got? she will program it herself given only the code and lessons you’ve had so far. She came up with this, so see if you can pick it up for homework!

Questions, suggestions, problems — write in the comments.

Похожие записи