Skip to content

Instantly share code, notes, and snippets.

@nic-hartley
Last active September 29, 2019 00:25
Show Gist options
  • Save nic-hartley/f97e2fb0162286b38d23ee6ef8359984 to your computer and use it in GitHub Desktop.
Save nic-hartley/f97e2fb0162286b38d23ee6ef8359984 to your computer and use it in GitHub Desktop.
const auto PLAY_BTN_PIN = 8;
const auto SAVE_BTN_PIN = 12;
const auto TONE_SELECT_PIN = A5;
const auto SPEAKER_PIN = 5;
const auto INDICATOR_LED = 13;
void setup() {
pinMode(PLAY_BTN_PIN, INPUT);
pinMode(SAVE_BTN_PIN, INPUT);
pinMode(TONE_SELECT_PIN, INPUT);
pinMode(SPEAKER_PIN, OUTPUT);
pinMode(INDICATOR_LED, OUTPUT);
}
int get_cur_freq() {
const int freqs[] = { 261, 277, 293, 311, 329, 349, 370, 392, 415, 440, 466, 494, 523 };
int freq_idx = 13 * analogRead(TONE_SELECT_PIN) / 1024;
int freq = freqs[freq_idx];
return freq;
}
#define check_play() do { if (digitalRead(PLAY_BTN_PIN) == HIGH) goto play; } while (0)
void loop() {
const int MAX_FREQS = 64;
int stored_freqs[MAX_FREQS];
int cur_len = 0;
for (int i = 0; i < MAX_FREQS; ++i) {
while (digitalRead(SAVE_BTN_PIN) == LOW) {
tone(SPEAKER_PIN, get_cur_freq(), 50);
check_play();
}
stored_freqs[cur_len] = get_cur_freq();
++cur_len;
while (digitalRead(SAVE_BTN_PIN) == HIGH) check_play();
}
play:
if (cur_len == 0) {
for (int i = 0; i < 5; ++i) {
digitalWrite(INDICATOR_LED, HIGH);
delay(50);
digitalWrite(INDICATOR_LED, LOW);
delay(50);
}
} else {
digitalWrite(INDICATOR_LED, HIGH);
delay(500);
for (int i = 0; i < cur_len; ++i) {
tone(SPEAKER_PIN, stored_freqs[i], 500);
delay(500);
}
delay(500);
digitalWrite(INDICATOR_LED, LOW);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment