From a7d02d9a9bf95efd5e3d76bc2e2bd67549fbd977 Mon Sep 17 00:00:00 2001 From: James McKenzie Date: Mon, 29 Apr 2019 21:46:33 +1200 Subject: [PATCH] Autotune working --- Theremin/Theremin.ino | 64 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/Theremin/Theremin.ino b/Theremin/Theremin.ino index a025c1a..ce8058a 100644 --- a/Theremin/Theremin.ino +++ b/Theremin/Theremin.ino @@ -15,19 +15,28 @@ void setup() { pinMode(11, OUTPUT); //the trigger pin pinMode(12, INPUT); //echo pin - if (digitalRead(6) ==HIGH){ - mode = 0; - mode = 1; - } + } void loop() { - + if (digitalRead(6) ==HIGH){ + mode = 0; + }else{ + mode = 1; + } + float note = 0; if (digitalRead(7) == LOW){ if (mode == 0){ - digitalWrite(13, HIGH); - tone(8, getNote()); + note = tune(getNote()); } + else{ + note = getNote(); + } + digitalWrite(13, HIGH); + Serial.print(note); //print units after the distance + Serial.println(" hz"); //print units after the distance + tone(8, note); + } else{ noTone(8); @@ -50,7 +59,44 @@ float getNote() calculatedDistance = 1760; } - Serial.print(calculatedDistance); //print units after the distance - Serial.println(" hz"); //print units after the distance + return calculatedDistance; } +float tune(float toTune){ + if (toTune> 523.75){ //C5 + return 523.75; + } + if (toTune> 493.88){ //B4 + return 493.88; + } + if (toTune> 440){ //A4 + return 440; + } + if (toTune> 392){ //G4 + return 392; + } + if (toTune> 349.23){ //F4 + return 349.23; + } + if (toTune> 329.63){ //E5 + return 329.63; + } + if (toTune> 293.66){ //D4 + return 293.66; + } + if (toTune> 261.63){ //C4 + return 261.63; + } + if (toTune> 246.94){ //B3 + return 246.94; + } + if (toTune> 220){ //A3 + return 220; + } + if (toTune> 196){ //G3 + return 196; + } + if (toTune> 174.61){ //F3 + return 174.61; + } +}