Skip to content

Instantly share code, notes, and snippets.

@JordanRickman
Created April 2, 2019 01:48
Show Gist options
  • Save JordanRickman/f03aa7ac1aa76867667eee7b8ce08d29 to your computer and use it in GitHub Desktop.
Save JordanRickman/f03aa7ac1aa76867667eee7b8ce08d29 to your computer and use it in GitHub Desktop.
ATTiny Sketch for Homemade Hardware
const float FACTOR = 1.05946; // One semitone
float freq = 440;
const int BUTTON_UP = 3;
const int BUTTON_DOWN = 4;
const int SPEAKER = 0;
bool up_pressed = false;
bool down_pressed = false;
void setup() {
pinMode(BUTTON_UP, INPUT);
pinMode(BUTTON_DOWN, INPUT);
pinMode(SPEAKER, OUTPUT);
tone(SPEAKER, freq);
}
void loop() {
bool freq_changed = false;
if (!up_pressed && digitalRead(BUTTON_UP) == HIGH) {
up_pressed = true;
}
if (up_pressed && digitalRead(BUTTON_UP) == LOW) {
up_pressed = false;
freq *= FACTOR;
freq_changed = true;
}
if (!down_pressed && digitalRead(BUTTON_DOWN) == HIGH) {
down_pressed = true;
}
if (down_pressed && digitalRead(BUTTON_DOWN) == LOW) {
down_pressed = false;
freq /= FACTOR;
freq_changed = true;
}
if (freq_changed) {
noTone(SPEAKER);
tone(SPEAKER, freq);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment