Hello! I've never posted on the forum before, so please let me know if I need to include additional information or posting to the incorrect category. I've been trying to learn how to use the millis() function properly and wanted to play two tones on two different piezoelectric passive buzzers. Was trying to make it sound like both buzzers were playing at the same time by alternating between the two pins (5 & 6) every 100 milliseconds every 500 milliseconds. Though the timing isn't that important currently, because I can't seem to get my tones to play correctly. Either the timing is slightly off or some tones aren't playing at all. Currently trying to run this program on an arduino uno. Any help/advice would be helpful! I also haven't used the millis() function before, so I'm not sure I'm using it correctly.
const double MAIN[] = {1046.50, 1567.98, 1975.53, 1479.98};
const char* CHORUS[] = {"N", "N", "N", "N", "N", "N", "N",
"N", "N", "N", "N", "N", "N",
"N", "N", "N", "E4", "N", "G4", "N",
"D4", "N", "F4#", "N", "E4", "F4#",
"E4", "B4", "E4", "C4", "C4#", "E4","E4",
"N", "G4", "N", "D4", "N", "F4#",
"N", "D4", "G4", "D4", "B4", "E4", "C4",
"N", "E4", "E4", "N", "G4", "N",
"D4", "N", "F4#", "N", "E4", "F4#", "E4",
"B4", "E4", "C4", "C4#", "E4", "E4",
"N", "G4", "N", "D4", "N", "F4#", "N",
"D4", "G4", "D4", "B4", "C4", "N",
"C4", "N", "E4", "N", "N", "N", "D4",
"N", "N", "N", "C4", "N", "N",
"N", "E4", "C4", "N", "E4", "E4", "N",
"G4", "N", "D4", "N", "F4#", "N",
"E4", "F4#", "E4", "B4", "E4", "C4", "C4#",
"E4", "E4", "N", "G4", "N", "D4",
"N", "F4", "N", "D4", "G4", "D4", "B4",
"E4", "C4", "N", "E4", "E4", "N",
"G4", "N", "D4", "N", "F4#", "N", "E4",
"F4#", "E4", "B4", "E4", "C4", "C4#",
"E4", "E4", "N", "G4", "N", "D4", "N",
"F4#", "N", "D4", "G4", "D4", "B4",
"C4", "N", "C4", "N", "E4", "N", "N",
"N", "D4", "N", "N", "N", "C4",
"N", "N", "N", "E4", "C4", "N", "E4"};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
}
double FrequencyCalc(const char*);
void loop() {
double mainFrequency,
chorusFrequency;
int noteInterval = 1000,
switchInterval = 20,
mainPin = 6,
chorusPin = 5,
mainFrequencyCount = 0;
bool pinSwitch = false;
unsigned long startMillis,
currentMillis;
for (int i = 0 ; i <= 176 ; i++){
startMillis = millis();
currentMillis = startMillis;
if (mainFrequencyCount == 3){
mainFrequency = MAIN[mainFrequencyCount];
mainFrequencyCount = 0;
}
else{
mainFrequency = MAIN[mainFrequencyCount];
mainFrequencyCount++;
}
chorusFrequency = FrequencyCalc(CHORUS[i]);
Serial.println(mainFrequency);
Serial.println(chorusFrequency);
while (currentMillis - startMillis <= noteInterval){
while (currentMillis - startMillis <= switchInterval){
if (chorusFrequency == 0.00){
noTone(chorusPin);
tone(mainPin, mainFrequency);
currentMillis = millis();
}
if (chorusFrequency > 0.00){
if (pinSwitch == true){
noTone(chorusPin);
tone(mainPin, mainFrequency);
currentMillis = millis();
}
if (pinSwitch == false){
noTone(mainPin);
tone(chorusPin, chorusFrequency);
currentMillis = millis();
}
}
}
pinSwitch = !pinSwitch;
currentMillis = millis();
}
}
}
double FrequencyCalc(const char* note) {
double octave = note[1] - 48,
key,
frequency;
char notes[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B'};
int keyPattern[] = {4, 6, 8, 9, 11, 13, 15};
if (note[0] == 'N'){
frequency = 0.00;
}
else {
for (int a = 0 ; a <= 6 ; a++){
if (note[0] == notes[a]){
key = (octave - 1) * 12 + keyPattern[a];
if (note[2] == '#'){
key++;
}
}}
key = (key - 49.00)/12.00;
frequency = pow(2, key) * 440;
}
return frequency;}