Katie 1. Buzzer
SKEEM:

KASUTATUD KOMPONEENDID:

KOOD:
// 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);
}
PRAKTILINE ÜLESANNE, BUZZERI KASUTAMINE:
SKEEM:

KASUTATUD KOMPONENDID:
- 22 juhtmed
- 1 pieso
- 1 resistoor
- 1 LCD ekraan
- 1 Arduino plaat
- 1 potentsioomeeter
KOOD:
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
const int buzzerPin = 9;
int tempo = 150;
const int NOTE_C4 = 262;
const int NOTE_D4 = 294;
const int NOTE_E4 = 330;
const int NOTE_F4 = 349;
const int NOTE_G4 = 392;
const int NOTE_A4 = 440;
const int NOTE_B4 = 494;
int sensorPin = 0;
int sensorValue = 0;
int Value_new;
String name1 = "Twinkle";
String name2 = "Jingle Bells";
String name3 = "have a good day";
int melody[] = {
NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4,
NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4
};
int durations[] = {
4, 4, 4, 4, 4, 4, 2, // Первые 7 нот
4, 4, 4, 4, 4, 4, 2 // Следующие 7 нот
};
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2);
}
void music1(){
int melodyLength = sizeof(melody) / sizeof(melody[0]);
for (int i = 0; i < melodyLength; i++) {
int noteDuration = 1000 / durations[i];
tone(buzzerPin, melody[i], noteDuration);
int pauseDuration = noteDuration * 1.3;
delay(pauseDuration);
noTone(buzzerPin);
}
}
void mucic1_name() {
lcd.setCursor(0,0);
lcd.clear();
lcd.print(name1);
for(int i =0; i<16-name1.length(); i++)
{
lcd.scrollDisplayRight();
delay(200);
}
lcd.clear();
lcd.setCursor(15-name1.length(),1);
lcd.print(name1);
for(int j =1; j<16-name1.length(); j++)
{
lcd.scrollDisplayLeft();
delay(200);
}
lcd.clear();
}
void music2(){
const int NOTE_E4 = 330;
const int NOTE_G4 = 392;
const int NOTE_A4 = 440;
const int NOTE_B4 = 494;
const int NOTE_D5 = 587;
const int NOTE_E5 = 659;
int melody[] = {
NOTE_E4, NOTE_E4, NOTE_E4,
NOTE_E4, NOTE_E4, NOTE_E4,
NOTE_E4, NOTE_G4, NOTE_C4, NOTE_D4, NOTE_E4,
NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_F4, NOTE_E4, NOTE_E4,
NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_E4, NOTE_D4, NOTE_G4
};
int durations[] = {
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4, 4, 2,
4, 4, 4, 4, 4
};
int melodyLength = sizeof(melody) / sizeof(melody[0]);
for (int i = 0; i < melodyLength; i++) {
int noteDuration = 1000 / durations[i];
tone(buzzerPin, melody[i], noteDuration);
int pauseDuration = noteDuration * 1.3;
delay(pauseDuration);
noTone(buzzerPin);
}
}
void music2_name(){
lcd.setCursor(0,0);
lcd.clear();
lcd.print(name2);
for(int i =0; i<16-name2.length(); i++)
{
lcd.scrollDisplayRight();
delay(200);
}
lcd.clear();
lcd.setCursor(15-name2.length(),1);
lcd.print(name2);
for(int j =1; j<16-name2.length(); j++)
{
lcd.scrollDisplayLeft();
delay(200);
}
lcd.clear();
}
void goodbye() {
lcd.setCursor(0,0);
lcd.clear();
lcd.print(name3);
for(int i =0; i<16-name3.length(); i++)
{
lcd.scrollDisplayRight();
delay(200);
}
lcd.clear();
lcd.setCursor(15-name3.length(),1);
lcd.print(name3);
for(int j =1; j<16-name3.length(); j++)
{
lcd.scrollDisplayLeft();
delay(200);
}
lcd.clear();
}
void loop() {
Value_new = analogRead(sensorPin);
Serial.print(Value_new);
Value_new = map(Value_new, 0, 1023, 1, 3);
if (Value_new == 1) {
music2_name();
music2();
delay(2000);
} else {
goodbye();
}
}
KUS VÕIB KASUTADA:
Seda seadet saab autos kasutada raadiosaatjana, kuna laulu nimi on sinna kirjutatud ja see mängib
LINK: