Note
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
参加する理由は?
- エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
- 学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。
- 独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。
- 特別割引:最新製品の独占割引をお楽しみください。
- 祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[|link_sf_facebook|]をクリックして今すぐ参加しましょう!
この「ドアベル」プロジェクトは、ボタンが押された際にArduinoを使って事前に定義されたメロディをパッシブブザーモジュールで再生する、というドアベルの機能をシミュレートすることを目的としています。
ultimate-sensor-kit\fun_project\07-Doorbell
のパス下で07-Doorbell.ino
ファイルを開くか、このコードを Arduino IDE にコピーしてください。
このプロジェクトの主要なコンセプトは、Arduino Unoボードを使ってボタンの押下を検出し、それに応じてパッシブブザーでメロディを再生することです。このメロディは、音のピッチ(定義された音程)とその持続時間によって構成されます。
必要なライブラリとグローバル変数の設定
#include "pitches.h" // This library provides the frequency values for musical notes. const int buttonPin = 7; // Button connected to digital pin 7 const int buzzerPin = 8; // Buzzer connected to digital pin 8 // Arrays to define the melody and the corresponding note durations int melody[] = {...}; int noteDurations[] = {...};
このセグメントには、音符のための必要なライブラリが含まれ、コンポーネント用のピンが設定されます。さらに、メロディとその長さは配列で定義されています。
ボタンの初期化とシリアル通信の開始
void setup() { Serial.begin(9600); // Start serial communication at 9600 baud rate pinMode(buttonPin, INPUT); // Set the button pin as an input }
setup()
関数では、シリアル通信を開始し、buttonPinを入力として構成します。ボタンの押下を監視し、メロディを再生
void loop() { int buttonState = digitalRead(buttonPin); // Read the state of the button if (buttonState == LOW) { // Check if the button is pressed Serial.println("Button pressed"); // Send a message to serial monitor buzzer(); // Play the buzzer melody } }
ここでは、ループ内でボタンの状態を常にチェックしています。もし押された場合は、シリアルモニターにメッセージが送信され、
buzzer()
関数(メロディを演奏する)が呼び出されます。buzzer()
関数でメロディを再生void buzzer() { int size = sizeof(noteDurations) / sizeof(int); // Calculate the number of notes for (int thisNote = 0; thisNote < size; thisNote++) { int noteDuration = 1000 / noteDurations[thisNote]; // Calculate note's play duration tone(buzzerPin, melody[thisNote], noteDuration); // Play the note on the buzzer int pauseBetweenNotes = noteDuration * 1.30; // Calculate pause between notes delay(pauseBetweenNotes); // Introduce the pause noTone(buzzerPin); // Stop playing the note } }
buzzer()
関数では、メロディの音符が順番に演奏されます。tone()
関数は、指定された時間だけブザーでトーンを生成します。各音符を演奏した後、次の音符を演奏する前に短い休止があります。