Happy new year LMR,
I am trying to modify the buzzer code from arduino sample. I wanna try to keep it simple and easy to call a function to manage different tones. For example:
________________________________________________
char notes1[] = "cdefgab "; // a space represents a rest
int beats1[] = { 1, 1, 1, 1, 1, 1, 3}
int length1 = 7; // the number of notes
char notes2[] = "bagfedc "; // a space represents a res
int beats2[] = { 1, 1, 1, 1, 1, 1, 3}
int length2 = 7; // the number of notes
char notes3[] = "ccccc "; // a space represents a rest
int beats3[] = { 1, 1, 1, 1, 1};
int length3 = 5; // the number of notes
______________________________________________
Is it possible to have all these 3 element(note, beat,length) in an array so i can call playTrack(2) and it will play something like:
void playTrack(int sid){ for (int i = 0; i < length5; i++) { if (note[sid] == ' ') { delay(beat[sid] * tempo); // rest } else { playNote(note[sid], beat[sid] * tempo); } delay(tempo / 2); } }void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(BuzzerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(BuzzerPin, LOW);
delayMicroseconds(tone);
}
}void playNote(char note, int duration) {
char names[] = { ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘a’, ‘b’, ‘C’ };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
I know there’s a tone() library but somehow it conflict with the code I am using now, beside I don’t need fancy music so. Anyone any suggestions?
BTW, is there a tone MAP out there? like 1915=c, 1700=d…956=C and so on?
Thanks for any helps~