Go Down

Topic: Buzzer Playing Unwanted High Frequency Sound (Read 85 times) previous topic - next topic

Jarett127

Jul 13, 2017, 04:31 pm Last Edit: Jul 13, 2017, 04:37 pm by Jarett127
I am programming in a song for my uno and for some reason, the buzzer connected to pin 8 is playing a high frequency sound similar to a dog whistle. When I programmed a different song however, it didn't play the high frequency sound. I don't know what is wrong but if anyone does please tell me. Thanks :)




#include <pitches.h>


#define FIRST_TONE_PIN 8
#define NUM_OF_VOICES 2
// a future version will support more than 2 simultaneous voices

// notes in the melody:
int melody1[] = {
0, NOTE_F6, NOTE_G6, NOTE_AS6, NOTE_AS6, NOTE_G6, 0, NOTE_F6, NOTE_F6, NOTE_G6, NOTE_AS6, NOTE_G6,
0, NOTE_F6, NOTE_G6, NOTE_G6, NOTE_F6, NOTE_D6, NOTE_C6, NOTE_C6, NOTE_AS5   
};
int mel1Durations[] = {
480, 160, 160, 160, 160, 160, 480, 160, 160, 160, 160, 160,
480, 160, 320, 160, 320, 320, 160, 320, 320 

};


int harmony1[] = {
0, NOTE_D6, NOTE_D6, NOTE_D6, 0, NOTE_C6, NOTE_C6, NOTE_F6, NOTE_G6,
0, NOTE_F6, NOTE_D6, NOTE_D6, NOTE_F6, NOTE_D6, NOTE_C6, NOTE_C6, NOTE_AS5


};


int harm1Durations[] = {

480, 320, 160, 320, 480, 320, 160, 160, 160,
480, 160, 320, 320, 160, 320, 160, 320, 320 

};

void setup() {
 Serial.println("play");
 Serial.begin(9600);
 for (unsigned char i = 0; i < NUM_OF_VOICES; i++) {
   pinMode(i+FIRST_TONE_PIN,OUTPUT);
   Serial.println(i);
 }

 //led indicator

 Serial.println("play");

}

void loop() {
 
 playTune(melody1, sizeof(melody1)/sizeof(int), mel1Durations, harmony1, sizeof(harmony1)/sizeof(int), harm1Durations);

}

// no need to modify anything below for basic use
void playTune(int melody[], int melLength, int melDurations[], int harmony[], int harmLength, int harmDurations[])
{
 unsigned int Counts[] = { 0, 0};
 unsigned int Periods[] = { 0, 0};
 unsigned char States[] = { 0, 0};

 unsigned long stopTimeNote1 = 0;
 unsigned long stopTimeNote2 = 0;
 unsigned char TimedPin = 0;
 unsigned int CurrentCount = 0;
 unsigned char i;
 unsigned char indexMel = 0, indexHarm = 0;

 while (1) {
   CurrentCount = Counts[0];
   TimedPin = 0;
   for (i=1;i<NUM_OF_VOICES;i++) {
     if (Counts < CurrentCount) {
       TimedPin = i;
       CurrentCount = Counts;
     } // for
   }
   if (CurrentCount > 3)
     delayMicroseconds(CurrentCount);
   if (Periods[TimedPin] < 65535)
     States[TimedPin] = !States[TimedPin];

   digitalWrite(FIRST_TONE_PIN+TimedPin,States[TimedPin]);

   if (millis() >= stopTimeNote1) {
     if (indexMel >= melLength)
       break;
     stopTimeNote1 = millis() + melDurations[indexMel];
     Periods[0] = 1000000 / melody[indexMel++];

   }
   if (millis() >= stopTimeNote2) {
     if (indexHarm >= harmLength)
       break;
     stopTimeNote2 = millis() + harmDurations[indexHarm];
     Periods[1] = 1000000 / harmony[indexHarm++];
   }

   for (i = 0; i < NUM_OF_VOICES; i++) {
     Counts = Counts - CurrentCount;
   }
   Counts[TimedPin] = Periods[TimedPin];
 }
}

Wawa

Are you using a 'buzzer' or a piezo disc (flat, used e.g. in greeting cards).
A buzzer (with a + sign on the case) has it's own buildin tone generator, and shouldn't be used for music.
It's like trying to use the claxon of a car as loudspeaker.
Leo..

larryd

unsigned int Periods[] = { 0, 0};
. . .
Periods[0] = 1000000 / melody[indexMel++];

Careful, unsigned int is 0 - 65535

.
No technical PMs.
The last thing you did is where you should start looking.

Go Up