注釈
こんにちは、SunFounderのRaspberry Pi & Arduino & ESP32愛好家コミュニティへようこそ!Facebook上でRaspberry Pi、Arduino、ESP32についてもっと深く掘り下げ、他の愛好家と交流しましょう。
参加する理由は?
エキスパートサポート:コミュニティやチームの助けを借りて、販売後の問題や技術的な課題を解決します。
学び&共有:ヒントやチュートリアルを交換してスキルを向上させましょう。
独占的なプレビュー:新製品の発表や先行プレビューに早期アクセスしましょう。
特別割引:最新製品の独占割引をお楽しみください。
祭りのプロモーションとギフト:ギフトや祝日のプロモーションに参加しましょう。
👉 私たちと一緒に探索し、創造する準備はできていますか?[ ここ]をクリックして今すぐ参加しましょう!
ウェルカム
このプロジェクトでは、PIRセンサーを使用して人の存在を検出し、スピーカーを使用してコンビニエンスストアの入口のドアベルに似たドアベルをシミュレートします。 歩行者がPIRセンサーの範囲内に現れると、スピーカーが鳴り、ドアベルを模倣します。
必要なコンポーネント
このプロジェクトには以下のコンポーネントが必要です。
全体のキットを購入すると便利です。こちらがリンクです:
名称 |
このキットのアイテム数 |
リンク |
|---|---|---|
Elite Explorer Kit |
300+ |
以下のリンクから別々に購入することもできます。
コンポーネント紹介 |
購入リンク |
|---|---|
- |
|
- |
配線図
回路図
コード
注釈
ファイル
01_welcome.inoをelite-explorer-kit-main\fun_project\01_welcomeのパスから直接開くことができます。または、このコードをArduino IDEにコピーしてください。
/*****************
Public Constants
*****************/
#define NOTE_B0 31
#define NOTE_C1 33
#define NOTE_CS1 35
#define NOTE_D1 37
#define NOTE_DS1 39
#define NOTE_E1 41
#define NOTE_F1 44
#define NOTE_FS1 46
#define NOTE_G1 49
#define NOTE_GS1 52
#define NOTE_A1 55
#define NOTE_AS1 58
#define NOTE_B1 62
#define NOTE_C2 65
#define NOTE_CS2 69
#define NOTE_D2 73
#define NOTE_DS2 78
#define NOTE_E2 82
#define NOTE_F2 87
#define NOTE_FS2 93
#define NOTE_G2 98
#define NOTE_GS2 104
#define NOTE_A2 110
#define NOTE_AS2 117
#define NOTE_B2 123
#define NOTE_C3 131
#define NOTE_CS3 139
#define NOTE_D3 147
#define NOTE_DS3 156
#define NOTE_E3 165
#define NOTE_F3 175
#define NOTE_FS3 185
#define NOTE_G3 196
#define NOTE_GS3 208
#define NOTE_A3 220
#define NOTE_AS3 233
#define NOTE_B3 247
#define NOTE_C4 262
#define NOTE_CS4 277
#define NOTE_D4 294
#define NOTE_DS4 311
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_FS4 370
#define NOTE_G4 392
#define NOTE_GS4 415
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_B4 494
#define NOTE_C5 523
#define NOTE_CS5 554
#define NOTE_D5 587
#define NOTE_DS5 622
#define NOTE_E5 659
#define NOTE_F5 698
#define NOTE_FS5 740
#define NOTE_G5 784
#define NOTE_GS5 831
#define NOTE_A5 880
#define NOTE_AS5 932
#define NOTE_B5 988
#define NOTE_C6 1047
#define NOTE_CS6 1109
#define NOTE_D6 1175
#define NOTE_DS6 1245
#define NOTE_E6 1319
#define NOTE_F6 1397
#define NOTE_FS6 1480
#define NOTE_G6 1568
#define NOTE_GS6 1661
#define NOTE_A6 1760
#define NOTE_AS6 1865
#define NOTE_B6 1976
#define NOTE_C7 2093
#define NOTE_CS7 2217
#define NOTE_D7 2349
#define NOTE_DS7 2489
#define NOTE_E7 2637
#define NOTE_F7 2794
#define NOTE_FS7 2960
#define NOTE_G7 3136
#define NOTE_GS7 3322
#define NOTE_A7 3520
#define NOTE_AS7 3729
#define NOTE_B7 3951
#define NOTE_C8 4186
#define NOTE_CS8 4435
#define NOTE_D8 4699
#define NOTE_DS8 49
1/*
2 The code is for an Arduino project that plays a melody when motion is detected by
3 a PIR (Passive Infrared) motion sensor.
4
5 Board: Arduino Uno R4
6 Component: PIR Motion Sensor Module, Audio Module, and Speaker
7*/
8
9#include "analogWave.h"
10#include "pitches.h"
11
12analogWave wave(DAC);
13
14#define PIR_PIN 8
15
16int melody[] = {
17 NOTE_C4, 4, NOTE_E4, 4, NOTE_G4, 4, NOTE_C5, 4, NOTE_G4, 4, NOTE_E4, 4, NOTE_C4,4
18};
19
20int noteCounter = 0;
21int bpm = 60;
22float beatDuration = 60.0 / bpm * 1000;
23int divider = 0, noteDuration = 0; // Variables to hold note duration
24
25void setup() {
26 Serial.begin(9600);
27 pinMode(PIR_PIN, INPUT); // Set PIR motion sensor pin as input
28 wave.sine(10);
29}
30
31void loop() {
32 int pirValue = digitalRead(PIR_PIN); // Read the PIR sensor
33 Serial.println(pirValue);
34
35 if (pirValue == HIGH) {
36 playMelody();
37 delay(5000); // wait for 5 seconds before checking again to avoid repetitive playing
38 }
39}
40
41void playMelody() {
42 // Calculate the duration of the current note
43 while (1) {
44 divider = melody[noteCounter + 1];
45 if (divider > 0) {
46 // For regular notes
47 noteDuration = beatDuration / divider;
48 } else if (divider < 0) {
49 // For dotted notes (duration increased by 50%)
50 noteDuration = beatDuration / abs(divider);
51 noteDuration *= 1.5; // Increase the duration by 50% for dotted notes
52 }
53
54 // Play the note
55 wave.freq(melody[noteCounter]);
56 delay(noteDuration * 0.85); // Play the note for 85% of its duration
57 wave.stop();
58
59 // Pause between notes
60 delay(noteDuration * 0.15); // Pause for 15% of the note duration
61
62 // Increment the note counter by 2 (because each note is followed by its duration)
63 noteCounter += 2;
64
65 // Reset the counter when reaching the end of the melody
66 int totalNotes = sizeof(melody) / sizeof(melody[0]);
67 noteCounter = noteCounter % totalNotes;
68
69 // Exit the loop after the melody finishes playing
70 if (noteCounter == 0) {
71 break;
72 }
73 }
74}
どのように動作しますか?
以下はコードの詳細な説明です:
ヘッダーファイルのインクルード:
analogWave.hとpitches.hという二つのヘッダーファイルをインクルードします。analogWave.hファイルにはanalogWaveクラスの定義が含まれており、pitches.hには音楽ノートの定義が含まれています。オブジェクトのインスタンス化と定数の定義:
analogWaveクラスを使用してwaveオブジェクトを作成し、PIRセンサーに接続されたピンとしてPIR_PINを2として定義します。メロディ配列:
melody配列は、音楽的なメロディを定義し、各ノートにはその持続時間を表す数が続きます。 負の数は点付きのノートを表し(持続時間を50%増加)、符点をつけます。グローバル変数:
関数間でデータを共有するためのグローバル変数を定義します。
setup():PIR_PINを入力として初期化し、wave.sine(10)を使用して正弦波の周波数を10Hzに設定します。loop():PIRセンサーの値を継続的に監視します。 人の存在が検出されると(pirValueがHIGHの場合)、
playMelody()関数を呼び出してメロディを演奏し、メロディの反復的な再生を防ぐために10秒待ちます。playMelody():この関数は
melody配列のデータに基づいて各ノートの持続時間を計算し、対応するノートを演奏します。ノート間には短い一時停止があります。 この関数はwave.freq()を使用して波形の周波数を設定し、delay()関数を使用してノートとノート間の一時停止の持続時間を制御します。注意:このコードを実行する前に、
pitches.hヘッダーファイルが実際に存在することを確認してください。