// 1. generate tones on a speaker on pin 5 // 2. demonstrate functional abstraction // 3. demonstrate MIDI notes and equal temperament scales // 4. demonstrate for loop with multiple expressions // 5. demonstate global constants with 'const float' and 'const int' const int SPEAKER_PIN = 5; void setup() { Serial.begin(115200); } void loop() { arpeggio(60, 3, 4); // 60 is the MIDI number for note C4 silence(); arpeggio(60, 3, 8); arpeggio(85, -6, 8); silence(); silence(); } void arpeggio(int start, int interval, int length) { for (int note = start, count = 0; count < length; note = note + interval, count = count + 1) { float freq = midi_to_freq(note); Serial.println(freq); tone(SPEAKER_PIN, freq); delay(200); } } const int MIDI_A0 = 21; const float freq_A0 = 27.5; float midi_to_freq(int midi_note) { return freq_A0 * pow(2.0, ((float)(midi_note - MIDI_A0)) / 12.0); } void silence(void) { noTone(SPEAKER_PIN); delay(500); }