Hi, I was wondering if you can use sharp/flat notes with the Internet Button.
I’ve looked everywhere on the forums and googled here and there, and still can’t find my answer. Does anyone know if this is possible?
Thanks!
Hi, I was wondering if you can use sharp/flat notes with the Internet Button.
I’ve looked everywhere on the forums and googled here and there, and still can’t find my answer. Does anyone know if this is possible?
Thanks!
This is the code that does the note playing
void InternetButton::playNote(String note, int duration){
int noteNum = 0;
int octave = 5;
int freq = 256;
//if(9 - int(command.charAt(1)) != null){
char octavo[5];
String tempString = note.substring(1,2);
tempString.toCharArray(octavo,5);
octave = atoi(octavo);
//}
if(duration != 0){
duration = 1000/duration;
}
switch(note.charAt(0)){
case 'C':
noteNum = 0;
break;
case 'D':
noteNum = 2;
break;
case 'E':
noteNum = 4;
break;
case 'F':
noteNum = 5;
break;
case 'G':
noteNum = 7;
break;
case 'A':
noteNum = 9;
break;
case 'B':
noteNum = 11;
break;
case 'R': // Rest note
octave = -1;
break;
default:
break;
//return -1;
}
// based on equation at http://www.phy.mtu.edu/~suits/NoteFreqCalcs.html and the Verdi tuning
// fn = f0*(2^1/12)^n where n = number of half-steps from the reference frequency f0
freq = float(256*pow(1.05946,( 12.0*(octave-4) +noteNum)));
// C4^ (2^1/12)^ 12 half-steps in an octave ^how many extra half-steps within that octave, 0 for a C
tone(D0,int(freq),duration);
delay(duration);
noTone(D0);
//return freq;
}
As you can see there is no special code for #
or b
, so no you can’t with the library as is, but you could use that code to write your own function that supports sharp/flat notes too.
In order to play a song, you might also want to come up with your own playSong()
function.
void InternetButton::playSong(String song){
char inputStr[200];
song.toCharArray(inputStr,200);
Serial.println(inputStr);
char *note = strtok(inputStr,",");
char *duration = strtok(NULL,",");
playNote(note,atoi(duration));
while(duration != NULL){
note = strtok(NULL,",");
Serial.println(note);
duration = strtok(NULL,", \n");
Serial.println(duration);
//if(atoi(duration) <= 0){
// break;
//}
playNote(note,atoi(duration));
}
}
Awesome!
I’ll be using this a lot. Thank you!
As of July 2016 this functionality is build into playSong. “b” for flat and “#” for sharp (a few other possibilities, check the Github for InternetButton).