아두이노 부저 송 모음 (11번 핀으로 설정되어 있음)
https://github.com/robsoncouto/arduino-songs
징글벨
int speakerPin = 8;
int length = 26;
char notes[] = "eeeeeeegcde fffffeeeeddedg";
int beats[] = { 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(speakerPin, HIGH);
delayMicroseconds(tone);
digitalWrite(speakerPin, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
// play the tone corresponding to the note name
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(speakerPin, OUTPUT);
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo); // rest
} else {
playNote(notes[i], beats[i] * tempo);
}
// pause between notes
delay(tempo / 2);
}
}
void loop() {
}
학교종이 땡땡땡
#define NOTE_C7 2093
#define NOTE_D7 2349
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_G7 3136
#define NOTE_A7 3520
#define NOTE_B7 3951
int speakerpin = 8; //스피커가 연결된 디지털핀 설정
//음계 데이터 (24개)
int melody[] = {NOTE_G7,NOTE_G7,NOTE_A7,NOTE_A7,NOTE_G7,
NOTE_G7,NOTE_E7,NOTE_G7,NOTE_G7,NOTE_E7,
NOTE_E7,NOTE_D7,NOTE_G7,NOTE_G7,NOTE_A7,
NOTE_A7,NOTE_G7,NOTE_G7,NOTE_E7,NOTE_G7,
NOTE_E7,NOTE_D7,NOTE_E7,NOTE_C7};
//음의길이, 4:4분음표, 2:2분음표
int noteDurations[] = {4,4,4,4,4,4,2,4,4,4,4,1,4,4,4,4,4,4,2,4,4,4,4,1};
void setup() {
for (int thisNote = 0; thisNote < 24; thisNote++)
{
int noteDuration = 1000 / noteDurations[thisNote];
tone(speakerpin, melody[thisNote], noteDuration); //소리를 낸다.
int pauseBetweenNotes = noteDuration * 1.30; //delay 계산식
delay(pauseBetweenNotes); //delay
noTone(speakerpin); //대상핀 출력 중단
}
}
void loop() {
}
작은별
#define PIEZOPIN 8
int length = 15;
char notes[] = "ccggaag ffeeddc ggffeed ggffeed ccggaag ffeeddc ";
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
digitalWrite(PIEZOPIN, HIGH);
delayMicroseconds(tone);
digitalWrite(PIEZOPIN, LOW);
delayMicroseconds(tone);
}
}
void playNote(char note, int duration) {
char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
for (int i = 0; i < 8; i++) {
if (names[i] == note) {
playTone(tones[i], duration);
}
}
}
void setup() {
pinMode(PIEZOPIN, OUTPUT);
for (int i = 0; i < length; i++) {
if (notes[i] == ' ') {
delay(beats[i] * tempo);
} else {
playNote(notes[i], beats[i] * tempo);
}
delay(tempo / 2);
}
}
void loop() {
}
아기상어
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define PIEZO_PIN 8
//notes in the melody:
int melody[] = {
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
0,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
0,
NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
};
// note durations: 4 = quarter note, 8 = eighth note, etc
int noteDurations[] = {
2, 2, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
2,
2, 2, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
2,
2, 2, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
};
void setup(){
for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(melody[0]); thisNote++) {
int noteDuration = 1000/noteDurations[thisNote];
tone(PIEZO_PIN, melody[thisNote],noteDuration);
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
noTone(PIEZO_PIN);
}
}
void loop(){}
수정중
//#define NOTE_C4 262
//#define NOTE_D4 294
//#define NOTE_E4 330
//#define NOTE_F4 349
//#define NOTE_G4 392
//#define NOTE_A4 440
//
//#define PIEZO_PIN 2
//
////notes in the melody:
//int melody[] = {
// NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
// NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
// NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
// 0,
// NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
// NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
// NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
// 0,
// NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
// NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
// NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
//};
//
//// note durations: 4 = quarter note, 8 = eighth note, etc
//int noteDurations[] = {
// 2, 2, 4, 4, 4, 8, 4, 8, 4,
// 4, 4, 4, 4, 4, 8, 4, 8, 4,
// 4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
// 2,
// 2, 2, 4, 4, 4, 8, 4, 8, 4,
// 4, 4, 4, 4, 4, 8, 4, 8, 4,
// 4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
// 2,
// 2, 2, 4, 4, 4, 8, 4, 8, 4,
// 4, 4, 4, 4, 4, 8, 4, 8, 4,
// 4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
//};
//
//void setup(){
// for (int thisNote = 0; thisNote < sizeof(melody) / sizeof(melody[0]); thisNote++) {
// int noteDuration = 1000/noteDurations[thisNote];
// tone(PIEZO_PIN, melody[thisNote],noteDuration);
//
// int pauseBetweenNotes = noteDuration * 1.30;
// delay(pauseBetweenNotes);
//
// noTone(PIEZO_PIN);
// }
//}
//
//void loop(){}
const int tonePin = 2;
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
//notes in the melody:
int melody[] = {
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
0,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
0,
NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4,
NOTE_C4, NOTE_D4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_F4, NOTE_E4,
};
// note durations: 4 = quarter note, 8 = eighth note, etc
int noteDurations[] = {
2, 2, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
2,
2, 2, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
2,
2, 2, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4,
4, 4, 4, 4, 4, 8, 4, 8, 4, 4, 4, 2,
};
unsigned long previousMillis = 0;
unsigned long currentMillis;
boolean outputTone = false; // Records current state
int melodyLength = sizeof(melody) / sizeof(melody[0]);
int melodyIndex = 0;
void setup() {
}
void loop() {
currentMillis = millis();
PlayMelody();
}
void PlayMelody() {
if (outputTone) {
if (currentMillis - previousMillis >= (1000/noteDurations[melodyIndex])) {
previousMillis = currentMillis;
noTone(tonePin);
outputTone = false;
melodyIndex = melodyIndex + 1;
if (melodyIndex >= melodyLength) {
melodyIndex = 0;
}
}
} else { //최초로 outputTone이 false로 시작
if (currentMillis - previousMillis >= (300/noteDurations[melodyIndex])) {
previousMillis = currentMillis;
tone(tonePin, melody[melodyIndex], 1000/noteDurations[melodyIndex]);
outputTone = true;
}
}
}
'Embedded > Arduino' 카테고리의 다른 글
Arduino - Blink(v0714) (0) | 2021.07.14 |
---|---|
Arduino - PIR 센서 (0) | 2021.07.14 |
Arduino - Analog Read (Serial.println) (0) | 2021.07.13 |
LilyPad - RGB LED 제어(공통양극) (0) | 2021.07.12 |
Arduino - LED Blink (0) | 2021.07.08 |