Process
- Find out frequency of notes using Frequency Chart
- Find out half of the wave period using the equation
- Half of period(μs) = 500μs/ frequency(Hz)
- Create a function for each note where the delay between speaker on and off is equivalent to half of the period (μs).
- Find out how many times the wave needs to be repeated for the desired note length
- Times repeated = Length of note(μs) / Wave period(μs)
Although this was fun to learn about, a function already exists to do this sort of stuff. tone(pin, frequency, length); creates an oscillating signal on a declared pin, at a given frequency for a set amount of milliseconds. There’s even a whole Arduino library called pitches which has predetermined the frequency of every note.
Links Used
Note Frequency Chart (Pitch to Note) (muted.io)
No Surprises – Radiohead Sheet music for Piano (Solo) | Musescore.com