Created
March 1, 2019 09:54
-
-
Save Alex4386/f7e82578bd229ceb5a46bc3ad766e9cf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define speakerOut 12 | |
#define BASEFREQ 440 | |
int sharp = 1; | |
int nope = 0; | |
int flat = -1; | |
void setup() { | |
Serial.begin(9600); | |
// put your setup code here, to run once: | |
//pinMode(speakerOut, OUTPUT); | |
} | |
double getHertz(char note = 'c', int sharp = 0, int octave = 4) { | |
Serial.print(note); | |
Serial.print(sharp); | |
Serial.println(octave); | |
int toneVal; | |
switch(note) { | |
case 'a': | |
toneVal = 0; | |
break; | |
case 'b': | |
toneVal = 2; | |
break; | |
case 'c': | |
toneVal = 3; | |
break; | |
case 'd': | |
toneVal = 5; | |
break; | |
case 'e': | |
toneVal = 7; | |
break; | |
case 'f': | |
toneVal = 8; | |
break; | |
case 'g': | |
toneVal = 10; | |
break; | |
default: | |
toneVal = 0; | |
break; | |
} | |
toneVal = toneVal + sharp; | |
double twelveth = 1.0/12.0; | |
double amp = pow(pow(2, twelveth), toneVal); | |
Serial.println(BASEFREQ*amp*pow(2,(octave-4))); | |
return BASEFREQ*amp*pow(2,(octave-4)); | |
} | |
void tune(char* note, int howLong, int delayTime=0) { | |
double hertz; | |
Serial.println(note); | |
if (note[1] == '#' || note[1] == 'b') { | |
int modifier = 0; | |
if (note[1] == '#') { | |
modifier = 1; | |
} else { | |
modifier = -1; | |
} | |
hertz = getHertz(note[0], modifier, note[2] - '0'); | |
} else { | |
hertz = getHertz(note[0], 0, note[1] - '0'); | |
} | |
tone(speakerOut, hertz, howLong); | |
delay(howLong); | |
delay(delayTime); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
tune("c4",100, 100); | |
tune("d4",100, 100); | |
tune("e4",100, 100); | |
tune("f4",100, 100); | |
tune("g4",100, 100); | |
tune("a5",100, 100); | |
tune("b5",100, 100); | |
tune("c5",100, 100); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment