Arduino IDE: composing songs with an array #8.1

Arduino IDE

Welcome back to our programming tutorial using the Arduino IDE. Today we will show some code examples with arrays.

You can take a look at the previous chapters of the course here:

In the previous article the use of an array was theoretically illustrated. On this article you will get a tutorial on how to create and compose the soundtrack of Pirates of the Caribbean with the help of a passive buzzer.

In order to approach the world of Arduino, there is a need to know certain programming topics.

The components required to carry out this project are:

  • 1x Arduino Uno or Elegoo UNO R3
  • 1x Passive buzzer
  • 2x F-M connectors (1x red, 1x black)

Now the next step is to connect the buzzer to the UNO board, the red (positive) cable to pin 8 and the black (negative) cable to GND ground.

The electrical diagram is shown in the following image:

Arduino array tutorial

Before reaching the code concerning the composition of the soundtrack it is good practice to explain the two sketches that will show the difference between the use of various simple variables and the use of an array.

In the following sketches, 6 LEDs were used, connected to pins 2-7 by means of 220 Ohm resistors. The first sketch uses 6 simple variables and causes the LEDs to flash sequentially, one by one. To achieve this, we used the digitalWrite (pinNum, HIGH / LOW) function.

The second sketch shows the use of an array and a for loop to accomplish the same purpose. But far fewer lines have been used for it.

Here is the first sketch:

And here is the second sketch:

It is evident that an array allows to use far fewer variables and to optimize and streamline the code when using data types of the same type.

The following listing shows how to compose the soundtrack of the Pirates of the Caribbean using Arduino and arrays.

 

Previous articles:

Follow us to keep yourself updated with the news!

Simone Candido è un ragazzo appassionato del mondo tech nella sua totalità. Simone ama immedesimarsi in nuove esperienze, la sua filosofia si basa sulla irrefrenabile voglia di ampliare a 360° le sue conoscenze abbracciando tutti i campi del sapere, in quanto ritiene che il sapere umano sia il connubio perfetto tra cultura umanistica e scientifica.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.