Arduino Uno R4와 기타 부품에 전원을 공급하는 가장 효과적인 방법을 확인하시려면, 아래 링크를 참조하세요: 아두이노 우노 R4 전원 공급 방법.
부저 프로그래밍 방법
Arduino UNO R4 라이브러리를 사용하면 멜로디 재생이 간단해집니다. 사각파 생성 방법을 이해할 필요가 없습니다. 라이브러리의 두 가지 함수, tone()와 noTone()만 사용하면 됩니다.
아두이노 UNO R4 코드
/* * 이 아두이노 우노 R4 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 우노 R4 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-r4/arduino-uno-r4-piezo-buzzer */#include"pitches.h"#define BUZZER_PIN 6 // The Arduino UNO R4 pin connected to the buzzer// notes in the melody:int melody[] = {NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4};// note durations: 4 = quarter note, 8 = eighth note, etc.:int noteDurations[] = { 4, 8, 8, 4, 4, 4, 4, 4};voidsetup() {// iterate over the notes of the melody:for (int thisNote = 0; thisNote < 8; thisNote++) {// to calculate the note duration, take one second divided by the note type.//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.int noteDuration = 1000 / noteDurations[thisNote];tone(BUZZER_PIN, melody[thisNote], noteDuration);// to distinguish the notes, set a minimum time between them.// the note's duration + 30% seems to work well:int pauseBetweenNotes = noteDuration * 1.30;delay(pauseBetweenNotes);// stop the tone playing:noTone(BUZZER_PIN); }}voidloop() {// no need to repeat the melody.}
Arduino IDE에서 Upload 버튼을 클릭하여 코드를 Arduino UNO R4에 로드하세요.
음악을 들어 보세요.
아두이노 UNO R4 코드 수정하기
이제 "징글벨" 노래를 재생하도록 코드를 변경하겠습니다.
우리는 단지 두 배열인 int melody[]와 int noteDurations[]만 수정하면 됩니다.
/* * 이 아두이노 우노 R4 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 우노 R4 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-r4/arduino-uno-r4-piezo-buzzer */#include"pitches.h"#define BUZZER_PIN 6 // The Arduino UNO R4 pin connected to the buzzer// notes in the melody:int melody[] = {NOTE_E5, NOTE_E5, NOTE_E5,NOTE_E5, NOTE_E5, NOTE_E5,NOTE_E5, NOTE_G5, NOTE_C5, NOTE_D5,NOTE_E5,NOTE_F5, NOTE_F5, NOTE_F5, NOTE_F5,NOTE_F5, NOTE_E5, NOTE_E5, NOTE_E5, NOTE_E5,NOTE_E5, NOTE_D5, NOTE_D5, NOTE_E5,NOTE_D5, NOTE_G5};// note durations: 4 = quarter note, 8 = eighth note, etc, also called tempo:int noteDurations[] = { 8, 8, 4, 8, 8, 4, 8, 8, 8, 8, 2, 8, 8, 8, 8, 8, 8, 8, 16, 16, 8, 8, 8, 8, 4, 4};voidsetup() {// iterate over the notes of the melody:intsize = sizeof(noteDurations) / sizeof(int);for (int thisNote = 0; thisNote < size; thisNote++) {// to calculate the note duration, take one second divided by the note type.//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.int noteDuration = 1000 / noteDurations[thisNote];tone(BUZZER_PIN, melody[thisNote], noteDuration);// to distinguish the notes, set a minimum time between them.// the note's duration + 30% seems to work well:int pauseBetweenNotes = noteDuration * 1.30;delay(pauseBetweenNotes);// stop the tone playing:noTone(BUZZER_PIN); }}voidloop() {// no need to repeat the melody.}
※ 주의:
언급된 코드는 delay() 함수를 사용하며, 멜로디가 재생되는 동안 다른 코드가 실행되지 않도록 중지합니다. 이를 방지하기 위해 ezBuzzer 라이브러리를 사용할 수 있습니다. 이 라이브러리는 부저가 다른 코드의 실행을 멈추지 않고 삑 소리를 내거나 멜로디를 재생할 수 있게 해줍니다.
동영상
비디오 제작은 시간이 많이 걸리는 작업입니다. 비디오 튜토리얼이 학습에 도움이 되었다면, YouTube 채널 을 구독하여 알려 주시기 바랍니다. 비디오에 대한 높은 수요가 있다면, 비디오를 만들기 위해 노력하겠습니다.
아래 동영상 데모는 아래 코드를 사용합니다:
/* * 이 아두이노 우노 R4 코드는 newbiely.kr 에서 개발되었습니다 * 이 아두이노 우노 R4 코드는 어떠한 제한 없이 공개 사용을 위해 제공됩니다. * 상세한 지침 및 연결도에 대해서는 다음을 방문하세요: * https://newbiely.kr/tutorials/arduino-uno-r4/arduino-uno-r4-piezo-buzzer */#define BUZZER_PIN 10constint ledPins[4] = {2, 3, 4, 5};constint buttonPins[4] = {6, 7, 8, 9};constint tones[4] = { 262, 330, 392, 523};constint MAX_LEVEL = 50;int sequence[MAX_LEVEL];int level = 1;voidsetup() {for (int i = 0; i < 4; i++) {pinMode(ledPins[i], OUTPUT);digitalWrite(ledPins[i], HIGH);pinMode(buttonPins[i], INPUT_PULLUP); }pinMode(BUZZER_PIN, OUTPUT);randomSeed(analogRead(A0)); startSound();delay(500); sequence[0] = random(0, 4); playSequence();}voidloop() {for (int i = 0; i < level; i++) {intbutton = waitForButton(); playTone(button);if (button != sequence[i]) { gameOver();return; } } level++;if (level > MAX_LEVEL) { winGame();return; }delay(500); sequence[level - 1] = random(0, 4); playSequence();}void playSequence() {for (int i = 0; i < level; i++) { playTone(sequence[i]);delay(150); }}void playTone(int index) {digitalWrite(ledPins[index], LOW);tone(BUZZER_PIN, tones[index]);delay(300);noTone(BUZZER_PIN);digitalWrite(ledPins[index], HIGH);delay(100);}int waitForButton() {while (true) {for (int i = 0; i < 4; i++) {if (digitalRead(buttonPins[i]) == LOW) {delay(30);while (digitalRead(buttonPins[i]) == LOW) { }delay(30);return i; } } }}void startSound() {tone(BUZZER_PIN, 523);delay(150);tone(BUZZER_PIN, 659);delay(150);tone(BUZZER_PIN, 784);delay(200);noTone(BUZZER_PIN);}void gameOver() {for (int i = 0; i < 3; i++) {tone(BUZZER_PIN, 150);for (int j = 0; j < 4; j++) {digitalWrite(ledPins[j], LOW); }delay(200);noTone(BUZZER_PIN);for (int j = 0; j < 4; j++) {digitalWrite(ledPins[j], HIGH); }delay(150); } level = 1;delay(1000); sequence[0] = random(0, 4); playSequence();}void winGame() {for (int i = 0; i < 4; i++) {digitalWrite(ledPins[i], LOW); }tone(BUZZER_PIN, 523);delay(150);tone(BUZZER_PIN, 659);delay(150);tone(BUZZER_PIN, 784);delay(150);tone(BUZZER_PIN, 1047);delay(400);noTone(BUZZER_PIN);for (int i = 0; i < 4; i++) {digitalWrite(ledPins[i], HIGH); } level = 1;delay(1000); sequence[0] = random(0, 4); playSequence();}