The melody encoding format I'll use is compact and pretty straightforward, but I admit it isn't the easiest one to read. The melody is represented as a null-terminated string of chars, in which each note is described by 3 consecutive characters:
- The note duration in sixteenth notes as an hexadecimal digit between 0 and F (0 has a special meaning and is interpreted as a whole note)
- The note name in English notation as an uppercase letter, or lowercase if sharp (R has a special meaning and indicates a rest)
- The octave number as a decimal digit, between 0 and 8 (for a rest, the value is ignored)
So for instance, a quarter-note C from the 5th octave is 4C5, and a eighth-note D sharp from the 4th octave is 2d4.
With this notation, the Tetris theme is:
In this example, a piezo passive sounder is connected on pin 3 of the Arduino board. The code to play a melody …