Komponendid:
- 4 Juhtmed ;
- 1 Piezo;
- 1 Breadboard Small ;
- 1 Arduino Uno R3.

// Meloodiate mängimine.
// Käsk Arduino tone() - noote tihedus.
// Noodid:
// note frequency
// c 262 Hz
// d 294 Hz
// e 330 Hz
// f 349 Hz
// g 392 Hz
// a 440 Hz
// b 494 Hz
// C 523 Hz
const int buzzerPin = 9;
// pikkus on nootide ja pausite koguste summa
const int songLength = 18;
char notes[] = "cdfda ag cdfdg gf "; // tähed on noodid ja tühik on paus
// Rütmi seadistamine.
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
// "tempo" meloodia kiirus. Kui väiksem tempo_ siis suurem kiirus.
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] == ' ') // kui noot puudub
{
delay(duration);
}
else
{
tone(buzzerPin, frequency(notes[i]), duration);
delay(duration);
}
delay(tempo/10); // väike paus nootide vahel
}
while(true){}
}
int frequency(char note)
{
int i;
const int numNotes = 8; // nootide kogus
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
// kui noot on olemas, siis tagastame selle tiheduse
for (i = 0; i < numNotes; i++)
{
if (names[i] == note)
{
return(frequencies[i]);
}
}
return(0);
}
Ülesanne 6 Buzzeri kasutamine
Komponeendid:
- Arduino Uno R3
- Breadboard Small
- 1 Potentsiomeeter
- 18 Juhtmed
- 1 Piezo
- 1 Resistor
- 1602 LCD ekraan
Tinkercad skeem:

Tinkercad proekt: https://www.tinkercad.com/things/7tZZi73eyw4-buzzer?sharecode=A6Dhkr464ssdZHxNhpjp4q_5_J__hWGXSfUVkYHQL2I
Video:
https://drive.google.com/file/d/1Y-0zDI9X9b-hyTM_3XVZXdvdXHksTW4X/view?usp=drivesdk
Kood:
#include <LiquidCrystal.h>
int sensorPin = 0;
int buzzerPin = 9;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte packman1[8] = {
0b11100,
0b11110,
0b11111,
0b11111,
0b11111,
0b11111,
0b11110,
0b11100
};
byte packmanEat[8] = {
0b00011,
0b00111,
0b01110,
0b00011,
0b00001,
0b00001,
0b00011,
0b00111
};
byte tort[8] = {
0b00000,
0b10101,
0b10101,
0b11111,
0b11111,
0b11111,
0b00000,
0b00000
};
byte man[8] = {
0b01110,
0b01110,
0b00100,
0b11111,
0b10101,
0b00100,
0b01110,
0b01010
};
byte treeL[8] = {
0b00001,
0b00001,
0b00011,
0b00111,
0b00011,
0b00111,
0b00011,
0b00101
};
byte treeR[8] = {
0b10000,
0b10000,
0b11000,
0b11100,
0b11000,
0b11100,
0b11000,
0b10100
};
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
lcd.createChar(1, packman1);
lcd.createChar(2, packmanEat);
lcd.createChar(3, tort);
lcd.createChar(4, man);
lcd.createChar(5, treeL);
lcd.createChar(6, treeR);
}
void pacman()
{
int notes[] = {
264, 330, 264, 264, 330, 264, 0, 220, 264, 220, 0, 175,
209, 0, 220, 264, 0, 175, 209, 0, 220, 264, 0, 175,
175, 0, 175, 220, 0, 146, 175, 0, 175, 220, 0, 146,
175, 0, 146, 196, 0, 123, 146, 0, 146, 196, 0, 123,
131, 0, 131, 175, 0, 110, 131, 0, 131, 175, 0, 110,
131, 0, 110, 147, 0, 88, 110, 0, 110, 147, 0, 88,
88, 0, 88, 117, 0, 70, 88, 0, 88, 117, 0, 70,
88, 0, 70, 117, 0, 88, 88, 0, 88, 117, 0, 70
};
int times[] = {
150, 150, 150, 150, 150, 150, 100, 300, 150, 150, 100, 300,
150, 100, 300, 150, 100, 300, 150, 100, 300, 150, 100, 300,
150, 100, 300, 150, 100, 300, 150, 100, 300, 150, 100, 300,
150, 100, 300, 150, 100, 300, 150, 100, 300, 150, 100, 300,
150, 100, 300, 150, 100, 300, 150, 100, 300, 150, 100, 300,
150, 100, 300, 150, 100, 300, 150, 100, 300, 150, 100, 300,
150, 100, 300, 150, 100, 300, 150, 100, 300, 150, 100, 300,
150, 100, 300, 150, 100, 300, 150, 100, 300, 150, 100, 300
};
for (int i = 0; i < 96; i++){
tone(buzzerPin, notes[i], times[i]);
delay(times[i]);
}
noTone(buzzerPin);
}
void happyBirthday()
{
int notes[] = {262, 262, 294, 262, 349, 330, 262, 262, 294, 262, 392, 349, 262, 262, 523, 440, 349, 330, 294, 466, 466, 440, 349, 392, 349};
int times[] = {400, 400, 800, 400, 800, 800, 400, 400, 800, 400, 800, 800, 400, 400, 800, 400, 400, 800, 400, 800, 400, 200, 200, 400, 400};
for (int i = 0; i < 25; i++) {
tone(buzzerPin, notes[i], times[i]);
delay(times[i]);
}
noTone(buzzerPin);
}
void mario()
{
int notes[] = {
1318, 1318, 1318, 1046, 1318, 1568, 784,
1046, 784, 659, 880, 987, 932, 880, 784,
1318, 1568, 1750, 1396, 1568, 1318, 1046, 1174, 987,
1046, 784, 659, 880, 987, 932, 880,
784, 1318, 1568, 1750, 1396, 1568, 1318, 1046, 1174, 987,
1568, 1480, 1396, 1244, 1318, 830, 880, 1046, 880, 1046, 1174,
};
int times[] = {
150, 300, 150, 150, 300, 600, 600,
450, 150, 300, 300, 150, 150, 300, 210,
210, 150, 300, 150, 150, 300, 150, 150, 450,
450, 150, 300, 300, 150, 150, 300,
210, 210, 150, 300, 150, 150, 300, 150, 150, 450,
150, 150, 150, 300, 150, 150, 150, 150, 150, 150, 150,
};
int delays[] = {
150, 300, 300, 150, 300, 600, 600,
450, 450, 450, 300, 300, 150, 300, 210,
210, 150, 300, 150, 300, 300, 150, 150, 450,
450, 450, 450, 300, 300, 150, 300,
210, 210, 150, 300, 150, 300, 300, 150, 150, 600,
150, 150, 150, 300, 300, 150, 150, 300, 150, 150, 150,
300, 150, 150, 150, 300, 300, 300, 150, 600,
150, 150, 150, 300, 300, 150, 150, 300, 150, 150, 450, 450, 450, 1200,
};
int min_length = min(min(sizeof(notes) / sizeof(notes[0]), sizeof(times) / sizeof(times[0])), sizeof(delays) / sizeof(delays[0]));
for (int i = 0; i < min_length; i++){
tone(buzzerPin, notes[i], times[i]);
delay(delays[i]);
}
noTone(buzzerPin);
}
void londonBridge()
{
int notes[] = {330, 294, 262, 294, 330, 330, 330, 294, 294, 294, 330, 392, 392, 330, 294, 262, 294, 330, 330, 330, 294, 294, 330, 294, 262};
int times[] = {400, 400, 400, 400, 400, 400, 800, 400, 200, 200, 400, 400, 800, 400, 400, 400, 400, 400, 400, 800, 400, 200, 200, 400, 400};
for (int i = 0; i < 25; i++) {
tone(buzzerPin, notes[i], times[i]);
delay(times[i]);
}
noTone(buzzerPin);
}
void christmas_tree()
{
int notes[] = {
262, 440, 440, 392,
440, 349, 262, 262,
262, 440, 440, 466,
392, 523, 0, 523, 294,
294, 466, 466, 440,
392, 349, 262, 440,
440, 392, 440, 349
};
int times[] = {
400, 400, 400, 400,
400, 400, 400, 400,
400, 400, 400, 400,
400, 600, 20, 400, 400,
400, 400, 400, 400,
400, 400, 400, 400,
400, 400, 400, 600
};
for (int i = 0; i < 29; i++) {
tone(buzzerPin, notes[i], times[i]);
delay(times[i]);
}
noTone(buzzerPin);
}
void loop()
{
int val = analogRead(sensorPin);
val = map(val, 0, 1023, 1, 2);
val = constrain(val, 1, 2);
if (val == 1) {
lcd.clear();
lcd.setCursor(13, 0);
lcd.write(byte(1));
lcd.setCursor(12, 0);
lcd.write(byte(2));
lcd.setCursor(4, 0);
lcd.print("Pac-man");
pacman();
delay(2000);
lcd.clear();
lcd.setCursor(8, 1);
lcd.write(byte(3));
lcd.setCursor(1, 0);
lcd.print("Happy Birthday!");
happyBirthday();
delay(2000);
lcd.clear();
lcd.setCursor(11, 0);
lcd.write(byte(4));
lcd.setCursor(5, 0);
lcd.print("Mario");
mario();
delay(2000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("London Bridge");
londonBridge();
delay(2000);
lcd.clear();
lcd.setCursor(7, 1);
lcd.write(byte(5));
lcd.setCursor(8, 1);
lcd.write(byte(6));
lcd.setCursor(0, 0);
lcd.print("Christmas tree");
christmas_tree();
delay(2000);
}
if (val == 2) {
lcd.clear();
}
}
Tööpõhimõte:
- Potentsiomeeter sisaldab ja lülitab LCD-ekraani välja
- Piezo teeb häält (meloodia)
- Siis mängivad meloodiad ja LCD-monitoril kirjutatakse meloodia nimi ja joonis
- Koostas skeemi ja salvestas koodi keskkonda «tinkercad.com»
Kasutatud koodi selgitus:
notes[]– Massiiv, kus tähed esindavad muusikapala noote ja tühikud pausi. See sätestab muusikalise järjestuse.beats[]– Massiiv, mis määrab iga noodi kestuse meloodias, mõjutades rütmi.tempo– Muutuja, mis reguleerib meloodia kiirust, kus väiksem väärtus tähendab suuremat kiirust.songLength– Konstant, mis määrab muusikapala pikkuse täisarvuna, aidates määratleda meloodia lõppu.duration– Muutuja, mis tähistab helisignaali kestust millisekundites.frequency– Muutuja, mis salvestab helisignaali sageduse väärtuse.