2021年6月24日

蜂鳴器

 Arduino 開放硬體中的「蜂鳴器」,有分為「有源蜂鳴器」、「無源蜂鳴器」;這個有源、無源,指的就是內建震盪源。從外表來看,很難分辨到底是有源蜂鳴器 or 無源蜂鳴器。盡管網路上有幾篇從外觀上的分辨來區分,但總是有些許落差。本篇就用實作,來分辨有源蜂鳴器 or 無源蜂鳴器。

1.接數位高電位,會正常發出逼逼聲的,就是有源蜂鳴器;而無源蜂鳴器,接數位高電位,不會發出正常的逼逼聲。

2.無論是有源蜂鳴器 or 無源蜂鳴器,兩者都可以唱歌。

3.音量差異,實作結果:有源蜂鳴器可以唱比較大聲,甚至容易破聲;而無源蜂鳴器發出的音量比有源蜂鳴器小聲。

 

1-數位高電壓,有源蜂鳴器可以發出逼逼聲

2-數位頻率,有源蜂鳴器較大聲,甚至破音

數位 6 號腳位發出聲音

void setup() {
  pinMode(6, OUTPUT);
}

void loop() {
  digitalWrite(6, HIGH);
  delay(2000);
  digitalWrite(6, LOW);
  delay(1000);

}

數位 4 號腳位不斷唱歌

#include "pitches.h"

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
};

void setup() {

}

void loop() {

    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(4, 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(4);
  }
}

 

沒有留言: