Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.