Now you’ll let your Arduino work on its own for a change, without having to operate potentiometers. You’ll use your piezo buzzer to play the beginning of Beethoven’s famous piece “Für Elise” through a series of tone functions.
That alone would be a bit too simple. Additionally, you’ll use a for loop to set how many times this melody should play consecutively.
First, let’s look at the melody. This is based on a sketch available on Github that saves you a lot of work. The user spara took the trouble to enter each individual note, its length, and the pauses into an Arduino sketch. Here are the first two notes, for example:
tone(10, 329.63, 300);
delay(350);
tone(10, 311.13, 300);
delay(350);
All these notes are located in the void setup()
function of your sketch. This means they are actually only played once when you start your Arduino. That’s nice, but in this lesson, loops come into play, allowing you to control how many times the piece is played.
The For Loop
Let’s first take a look at the for loop. In our case, it is created according to the following schema:
for (int i = 0; i < 3; i++) {
//Code to be repeated
}
First, of course, the command for
, which initiates the loop. In the parentheses ( )
there are some additional parameters.
First, the so-called initialization. This basically means that you declare a variable and assign it a value (zero in this case). This variable is typically simply referred to by the letter i
. It’s the counter that increases after each iteration of the loop – until a certain condition is no longer true.
___STEADY_PAYWALL___
Now the condition: with i < 3
you check if the variable i
is less than 3. Only as long as this is the case will the code in the loop be executed again. As soon as i
has the value 3, the loop stops.
Finally, the counter, which increases after each iteration: For this you use i++
As you already know, this increases a value by 1. As soon as the code within the curly braces { }
has run through once, this command increases the value in the variable i
from 0 to 1.
Let’s look at the individual iterations:
- Before the first iteration,
i
equals the value 0 -> The loop begins. - After the first iteration,
i
equals the value 1 -> The conditioni < 3
is true -> The loop runs a second time. - After the second iteration,
i
equals the value 2 -> The condition is still true -> The loop runs a third time. - After the third iteration,
i
equals the value 3 -> The condition is false -> The loop stops and is not executed further.
With a for loop, you can control quite well how many times something should be executed in succession. This could also be, for example, the blinking of your LED.
The While Loop
There is another method to create a loop in your sketch: while
Here too, it checks whether a condition is true, and the code between the curly braces is executed as long as the condition remains true.
int i = 0;
while (i < 3){
//Code to be repeated
i++;
}
However, there are two important differences from the for loop: First, the variable for the counter – i.e., i
– must be created before the loop. Additionally, the counter is incremented in the code that’s being repeated.
Otherwise, this loop works the same way. Apart from the fact that you need less space in the sketch for the for loop, there is another consideration that sometimes speaks for for
, sometimes for while
:
In a for loop, you must know in advance how often it should run. In a while loop, on the other hand, the “looped” code itself can influence whether it will be repeated once more. For example, a special event could increase the counter so much that the loop stops.
The more experience you gain in programming, the easier the decision between loops will become. And then you’ll eventually come across the third loop method: do...while
, which we won’t cover in this course.
Back to Beethoven: Download the sketch from the downloads onto your Arduino. Do you hear some classical music? 🙂