Skip to content

Instantly share code, notes, and snippets.

@janhajk
Created November 1, 2018 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janhajk/4284b5e59e4f3518c006e38b18622b48 to your computer and use it in GitHub Desktop.
Save janhajk/4284b5e59e4f3518c006e38b18622b48 to your computer and use it in GitHub Desktop.
int channel = 0;
int resolution = 8;
void setup() {
Serial.begin(115200);
ledcSetup(channel, 2000, resolution);
ledcAttachPin(12, channel);
}
void loop() {
ledcWrite(channel, 20);
for (int key = 44; key < 80; key = key + 1){
tone(key, 50);
}
}
void tone (int key, int leng) {
ledcWriteTone(channel, frequencyFromKey(key));
delay(leng);
}
// Gibt die Hz zurück anahnd der Klaviertasten-Nr.
// siehe : https://de.wikipedia.org/wiki/Frequenzen_der_gleichstufigen_Stimmung
int frequencyFromKey (float key) {
float hz = 0;
hz = pow(2, ((key-49)/12)) * 440;
Serial.println(hz);
return hz;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment