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:
- Arduino IDE: what is an array or a vector #8
- Arduino IDE: RGB LED, for, while, do while loops #7
- Arduino IDE: for loops against while / do while #6
- Arduino IDE: while and do while loops #5
- Arduino IDE: turn on LEDs using a button (if) #4.1
- Arduino IDE: the conditional construction IF #4
- Arduino IDE: arithmetic and logical operators #3
- Arduino IDE: variables, constants and macros #2
- How can I use Arduino IDE to write a program? #1
- Arduino IDE: what it is and how it works #0
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:
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:
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:
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
// We declare the variables int pin2 = 2; int pin3 = 3; int pin4 = 4; int pin5 = 5; int pin6 = 6; int pin7 = 7; int timer = 100; void setup(){ // Variables that write a signal are OUTPUT variables pinMode(pin2, OUTPUT); pinMode(pin3, OUTPUT); pinMode(pin4, OUTPUT); pinMode(pin5, OUTPUT); pinMode(pin6, OUTPUT); pinMode(pin7, OUTPUT); } void loop() { // function digitalWrite with parameter HIGH turns the LED ON // function digitalWrite with parameter LOW turns the LED OFF digitalWrite(pin2, HIGH); delay(timer); digitalWrite(pin2, LOW); delay(timer); digitalWrite(pin3, HIGH); delay(timer); digitalWrite(pin3, LOW); delay(timer); digitalWrite(pin4, HIGH); delay(timer); digitalWrite(pin4, LOW); delay(timer); digitalWrite(pin5, HIGH); delay(timer); digitalWrite(pin5, LOW); delay(timer); digitalWrite(pin6, HIGH); delay(timer); digitalWrite(pin6, LOW); delay(timer); digitalWrite(pin7, HIGH); delay(timer); digitalWrite(pin7, LOW); delay(timer); digitalWrite(pin6, HIGH); delay(timer); digitalWrite(pin6, LOW); delay(timer); digitalWrite(pin5, HIGH); delay(timer); digitalWrite(pin5, LOW); delay(timer); digitalWrite(pin4, HIGH); delay(timer); digitalWrite(pin4, LOW); delay(timer); digitalWrite(pin3, HIGH); delay(timer); digitalWrite(pin3, LOW); delay(timer); } |
And here is the second sketch:
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 |
int pinArray[] = {2, 3, 4, 5, 6, 7}; // We declare an array with its content, so we don't need to declare its size // The size of the array is statically calculated by the compiler. int count = 0; // A variable in the FOR loop. int timer = 100; void setup(){ // We define the action of the values (OUTPUT) inside this loop for (count=0;count<6;count++) { pinMode(pinArray[count], OUTPUT); } } void loop() { for (count=0;count<6;count++) { digitalWrite(pinArray[count], HIGH); delay(timer); digitalWrite(pinArray[count], LOW); delay(timer); } for (count=5;count>=0;count--) { digitalWrite(pinArray[count], HIGH); delay(timer); digitalWrite(pinArray[count], LOW); delay(timer); } } |
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.
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
const int buzzer = 8; const int velocitasuono = 1.5; #define NOTE_DO4 262 #define NOTE_RE4 294 #define NOTE_MI4 330 #define NOTE_FA4 349 #define NOTE_SOL4 392 #define NOTE_LA4 440 #define NOTE_SI4 494 #define NOTE_DO5 523 #define NOTE_RE5 587 #define NOTE_MI5 659 #define NOTE_FA5 698 #define NOTE_SOL5 784 #define NOTE_LA5 880 #define NOTE_SI5 988 // here is the score, coded inside the arrayo int note[] = { NOTE_MI4, NOTE_SOL4, NOTE_LA4, NOTE_LA4, 0, NOTE_LA4, NOTE_SI4, NOTE_DO5, NOTE_DO5, 0, NOTE_DO5, NOTE_RE5, NOTE_SI4, NOTE_SI4, 0, NOTE_LA4, NOTE_SOL4, NOTE_LA4, 0, NOTE_MI4, NOTE_SOL4, NOTE_LA4, NOTE_LA4, 0, NOTE_LA4, NOTE_SI4, NOTE_DO5, NOTE_DO5, 0, NOTE_DO5, NOTE_RE5, NOTE_SI4, NOTE_SI4, 0, NOTE_LA4, NOTE_SOL4, NOTE_LA4, 0, NOTE_MI4, NOTE_SOL4, NOTE_LA4, NOTE_LA4, 0, NOTE_LA4, NOTE_DO5, NOTE_RE5, NOTE_RE5, 0, NOTE_RE5, NOTE_MI5, NOTE_FA5, NOTE_FA5, 0, NOTE_MI5, NOTE_RE5, NOTE_MI5, NOTE_LA4, 0, NOTE_LA4, NOTE_SI4, NOTE_DO5, NOTE_DO5, 0, NOTE_RE5, NOTE_MI5, NOTE_LA4, 0, NOTE_LA4, NOTE_DO5, NOTE_SI4, NOTE_SI4, 0, NOTE_DO5, NOTE_LA4, NOTE_SI4, 0, NOTE_LA4, NOTE_LA4, NOTE_LA4, NOTE_SI4, NOTE_DO5, NOTE_DO5, 0, NOTE_DO5, NOTE_RE5, NOTE_SI4, NOTE_SI4, 0, NOTE_LA4, NOTE_SOL4, NOTE_LA4, 0, NOTE_MI4, NOTE_SOL4, NOTE_LA4, NOTE_LA4, 0, NOTE_LA4, NOTE_SI4, NOTE_DO5, NOTE_DO5, 0, NOTE_DO5, NOTE_RE5, NOTE_SI4, NOTE_SI4, 0, NOTE_LA4, NOTE_SOL4, NOTE_LA4, 0, NOTE_MI4, NOTE_SOL4, NOTE_LA4, NOTE_LA4, 0, NOTE_LA4, NOTE_DO5, NOTE_RE5, NOTE_RE5, 0, NOTE_RE5, NOTE_MI5, NOTE_FA5, NOTE_FA5, 0, NOTE_MI5, NOTE_RE5, NOTE_MI5, NOTE_LA4, 0, NOTE_LA4, NOTE_SI4, NOTE_DO5, NOTE_DO5, 0, NOTE_RE5, NOTE_MI5, NOTE_LA4, 0, NOTE_LA4, NOTE_DO5, NOTE_SI4, NOTE_SI4, 0, NOTE_DO5, NOTE_LA4, NOTE_SI4, 0, NOTE_MI5, 0, 0, NOTE_FA5, 0, 0, NOTE_MI5, NOTE_MI5, 0, NOTE_SOL5, 0, NOTE_MI5, NOTE_RE5, 0, 0, NOTE_RE5, 0, 0, NOTE_DO5, 0, 0, NOTE_SI4, NOTE_DO5, 0, NOTE_SI4, 0, NOTE_LA4, NOTE_MI5, 0, 0, NOTE_FA5, 0, 0, NOTE_MI5, NOTE_MI5, 0, NOTE_SOL5, 0, NOTE_MI5, NOTE_RE5, 0, 0, NOTE_RE5, 0, 0, NOTE_DO5, 0, 0, NOTE_SI4, NOTE_DO5, 0, NOTE_SI4, 0, NOTE_LA4 }; // Here is the array with the note duration. int durata[] = { 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 375, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 375, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 125, 250, 125, 125, 125, 250, 125, 125, 250, 125, 250, 125, 125, 125, 250, 125, 125, 125, 125, 375, 375, 250, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 375, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 375, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 250, 125, 125, 125, 125, 125, 250, 125, 125, 125, 250, 125, 125, 250, 125, 250, 125, 125, 125, 250, 125, 125, 125, 125, 375, 375, 250, 125, 375, 250, 125, 375, 125, 125, 125, 125, 125, 125, 125, 125, 375, 250, 125, 375, 250, 125, 375, 125, 125, 125, 125, 125, 500, 250, 125, 375, 250, 125, 375, 125, 125, 125, 125, 125, 125, 125, 125, 375, 250, 125, 375, 250, 125, 375, 125, 125, 125, 125, 125, 500 }; void setup() { // Nothing to set up } // tone is the fiunction that generates the sound with three formal parameters: output PIN, note array, frequency (or duration) array. void loop() { for (int i=0;i<203;i++){ int attesa = durata[i] * velocitasuono; tone(buzzer,note[i],attesa); delay(attesa);} delay (2000); // delay(2000) 2 seconds of silence before the music starts again. } |
Previous articles:
- Arduino IDE: RGB LED, for, while, do while loops #7
- Arduino IDE: for loops against while / do while #6
- Arduino IDE: while and do while loops #5
- Arduino IDE: turn on LEDs using a button (if) #4.1
- Arduino IDE: the conditional construction IF #4
- Arduino IDE: arithmetic and logical operators #3
- Arduino IDE: variables, constants and macros #2
- Arduino IDE: how can I use Arduino IDE to write a program? #1
- Arduino IDE: what it is and how it works #0
Follow us to keep yourself updated with the news!