[알찬 아두이노 예제 22] 패시브 버저, Passive Buzzer

Passive Buzzer

본 예제는 알찬 아두이노 키트(입문자용) 의  패시브 버저 예제입니다.

번호

번호 핀명 기능
 1 + VCC
 2 GND

실험

핀 연결
번호 I/O UNO R3
 1 + 2
 2 GND

소스코드1

최신 소스코드는 [파일]=>[예제]=>[WhiteAT UNO]=>[Starter] => [22_Passive_Buzzer1]  에 있으며
라이브러리는 아두이노 개발환경  을 참조하세요

/*
 알찬 아두이노 키트(입문자용) 의
 UNO 와 패시브 부저 예제입니다.

 알찬 아두이노 키트(입문자용): https://kit128.com/goods/view?no=133

 개별 부품
 .UNO R3 : https://kit128.com/goods/view?no=337

 출처: http://whiteat.com/Arduino
*/ 

int S = 2; // OUTPUT PIN

// 프로그램 시작 - 초기화 작업
void setup()
{
 Serial.begin(115200); // 디버깅 포트 초기화
 Serial.println("Arduino Examples - Passive Buzzer");
 Serial.println(" https://docs.whiteat.com/?p=2363");
 pinMode(S, OUTPUT);
}
int i;

void loop()
{ 
 for (i = 0; i <160; i ++) // outputs sound 1
 {
 digitalWrite (S, HIGH) ;// send pulse
 delay (1) ;// delay 1ms
 digitalWrite (S, LOW) ; // do not send pulse
 delay (1) ;// delay 1ms
 }

 for (i = 0; i <80; i ++) // outputs sound 2
 {
 digitalWrite (S, HIGH) ;// send pulse
 delay (2) ;// delay 1ms
 digitalWrite (S, LOW) ; // do not send pulse
 delay (2) ;// delay 1ms
 }
}

2가지 음이 반복적으로 재생됩니다.

소스코드2

최신 소스코드는 [파일]=>[예제]=>[WhiteAT UNO] =>[Starter] => [22_Passive_Buzzer2] 에 있습니다.

/*
 알찬 아두이노 키트(입문자용) 의
 UNO 와 패시브 부저(멜로디) 예제입니다.

 알찬 아두이노 키트(입문자용): https://kit128.com/goods/view?no=133

 개별 부품
 .UNO R3 : https://kit128.com/goods/view?no=337

 출처: http://whiteat.com/Arduino
*/ 


int S = 2; // OUTPUT PIN

// TONES ==========================================
// Start by defining the relationship between 
// note, period, & frequency. 
#define m_c 3830 // 261 Hz 
#define d 3400 // 294 Hz 
#define e 3038 // 329 Hz 
#define f 2864 // 349 Hz 
#define g 2550 // 392 Hz 
#define a 2272 // 440 Hz 
#define b 2028 // 493 Hz 
#define C 1912 // 523 Hz 
// Define a special note, 'R', to represent a rest
#define R 0

// MELODY and TIMING =======================================
// melody[] is an array of notes, accompanied by beats[], 
// which sets each note's relative length (higher #, longer note) 
int melody[] = { C, b, g, C, b, e, R, C, m_c, g, a, C };
int beats[] = { 16, 16, 16, 8, 8, 16, 32, 16, 16, 16, 8, 8 };
int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int tone_ = 0;
int beat = 0;
long duration = 0;
 
// PLAY TONE ==============================================
// Pulse the speaker to play a tone for a particular duration
void playTone() {
 long elapsed_time = 0;
 if (tone_ > 0) { // if this isn't a Rest beat, while the tone has 
 // played less long than 'duration', pulse speaker HIGH and LOW
 while (elapsed_time < duration) {

 digitalWrite(S, HIGH);
 delayMicroseconds(tone_ / 2);

 // DOWN
 digitalWrite(S, LOW);
 delayMicroseconds(tone_ / 2);

 // Keep track of how long we pulsed
 elapsed_time += (tone_);
 }
 }
 else { // Rest beat; loop times delay
 for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
 delayMicroseconds(duration);
 }
 }
}

// 프로그램 시작 - 초기화 작업
void setup()
{
 Serial.begin(115200); // 디버깅 포트 초기화
 Serial.println("Arduino Examples - Passive Buzzer(Melody)");
 Serial.println(" https://docs.whiteat.com/?p=2363");
 pinMode(S, OUTPUT);
} 

void loop()
{ 
 for (int i = 0; i<MAX_COUNT; i++) {
 tone_ = melody[i];
 beat = beats[i];

 duration = beat * tempo; // Set up timing

 playTone();
 // A pause between notes...
 delayMicroseconds(pause); 
 }
}

멜로디 예제 입니다.

제품 구매

[WAT-AK133]알찬 아두이노 키트(초보자용) 은 https://kit128.com/goods/view?no=133 에서
패시브 부저 Passive Buzzer 수동 버저 은 https://kit128.com/goods/view?no=244 에서
구매하실 수 있습니다.

.

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다