6 Katse Buzzer
kasutades buzzer mängida meloodia
const int buzzerPin = 9;
const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf ";
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
int tempo = 150;
void setup()
{
pinMode(buzzerPin, OUTPUT);
}
void loop()
{
int i, duration;
for (i = 0; i < songLength; i++)
{
duration = beats[i] * tempo;
if (notes[i] == ' ')
{
delay(duration);
}
else
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
}
delay(tempo/10);
}
while(true){}
}
int frequency(char note)
{
int i;
const int numNotes = 8;
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
}
return(0);
}
Ülesanne 6 Buzzeri kasutamine
Töö kirjeldus
Mängib meloodiat buzzeriga ja muudab potentsiomeetri abil meloodiat. Koodis 5 valmis meloodiat
Töö protsess
Potentsiomeetri abil saab meloodiat muuta, plaat loeb seda ja edastab signaali buzzerile, mis mängib meloodiat
Kasutatud komponeendid
- Arduino UNO plaat (1tk)
- Arendusplaat (1tk)
- Juhtmed (7tk)
- Potentsiomeeter(1tk)
- Buzzer(1tk)
Skeem


Kood
const int buzzerPin = 9;
const int potPin = A0;
int songLength = 18;
void setup()
{
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int potValue = analogRead(potPin);
potValue = map(potValue, 0, 1023, 0, 6);
Serial.println(potValue);
if(potValue == 1)
{
song1();
}
else if(potValue == 2)
{
song2();
}
else if(potValue == 3)
{
song3();
}
else if(potValue == 4)
{
song4();
}
else if(potValue == 5)
{
song5();
}
else if(potValue == 6)
{
song6();
}
}
int frequency(char note)
{
int i;
const int numNotes = 14;
char names[] = { 'x', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm' };
const int frequencies[] = {0, 50, 100, 150, 300, 350, 400, 450, 500, 700, 750, 800, 850, 900};
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
}
return(0);
}
void playSong(const char* notes, const int* beats, int songLength, int tempo)
{
int i, duration;
for (i = 0; i < songLength; i++)
{
duration = beats[i] * tempo;
if (notes[i] == ' ')
{
delay(duration);
}
else
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
delay(tempo/10);
}
}
}
void song1()
{
int tempo = 100; // Adjust tempo as desired
songLength = 30; // Adjust song length accordingly
const char notes[] = "ccggaagffeeddc";
const int beats[] = {4,4,4,4,4,4,2,4,4,4,4,4,4,2,4,4,4,4,4,4,2,4,4,4,4,4,4,4,4,4}; // Adjust note durations as desired
playSong(notes, beats, songLength, tempo);
}
void song2()
{
int tempo = 80;
songLength = 46;
const char notes[] = "kxjxijklmjxxkxjxijklmjxxkxjxijklmjxxkxjxijklmj";
const int beats [] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
playSong(notes, beats, songLength, tempo);
}
void song3()
{
int tempo = 72;
songLength = 32;
const char notes[] = "mmmljkjimmmljkjimmmljkjimmmljkji";
const int beats [] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
playSong(notes, beats, songLength, tempo);
}
void song4()
{
int tempo = 75;
songLength = 19;
const char notes[] = "kkklmlkkklmlkkkllmm";
const int beats [] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
playSong(notes, beats, songLength, tempo);
}
void song5()
{
int tempo = 50;
songLength = 74;
const char notes[] = "aaaabbbbccccddddeeeeffffgggghhhhiiiijjjjxxjjjjiiiihhhhggggffffeeeeddddccccaaaa";
const int beats [] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2};
playSong(notes, beats, songLength, tempo);
}
void song6()
{
int tempo = 75;
songLength = 11;
const char notes[] = "kjkkjkkjklm";
const int beats [] = {2,2,2,2,2,2,2,2,2,2,2};
playSong(notes, beats, songLength, tempo);
}
Video
https://drive.google.com/file/d/14loh8bOT4Ix1LlUw9n_GpwDCU6HpXAD6/view?pli=1
Kus neid komponente kasutada?
vargusvastane alarm, hoiatussüsteemid sõidukites, mõned kõne meloodiad
Uued funktsioonid
tone(pin, frequency, duration) – Tekitab määratud sagedusega ruudukujulise laine (ja 50% töötsükli) pin’il