Nota
Ciao, benvenuto nella community SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts su Facebook! Approfondisci Raspberry Pi, Arduino ed ESP32 con altri appassionati.
Perché unirsi?
Supporto Esperto: Risolvi problemi post-vendita e sfide tecniche con l’aiuto della nostra community e del nostro team.
Impara & Condividi: Scambia consigli e tutorial per migliorare le tue competenze.
Anteprime Esclusive: Ottieni l’accesso anticipato agli annunci di nuovi prodotti e anteprime esclusive.
Sconti Speciali: Goditi sconti esclusivi sui nostri prodotti più recenti.
Promozioni e Concorsi Festivi: Partecipa a concorsi e promozioni festive.
👉 Pronto a esplorare e creare con noi? Clicca [Qui] e unisciti oggi stesso!
Benvenuto
In questo progetto, useremo un sensore PIR per rilevare la presenza umana e un altoparlante per simulare un campanello, simile ai campanelli delle porte nei negozi di convenienza. Quando un pedone appare nel raggio del sensore PIR, l’altoparlante suonerà, imitando un campanello.
Componenti Necessari
In questo progetto, abbiamo bisogno dei seguenti componenti.
È sicuramente conveniente acquistare un kit completo, ecco il link:
Nome |
ELEMENTI IN QUESTO KIT |
LINK |
|---|---|---|
Elite Explorer Kit |
300+ |
Puoi anche acquistarli separatamente dai link sottostanti.
INTRODUZIONE COMPONENTI |
LINK ACQUISTO |
|---|---|
- |
|
- |
Collegamenti
Schema Elettrico
Codice
Nota
Puoi aprire il file
01_welcome.inonel percorsoelite-explorer-kit-main\fun_project\01_welcomedirettamente.Oppure copia questo codice nell’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}
Come funziona?
Ecco una spiegazione passo-passo del codice:
Includere i File Header:
Includi due file header,
analogWave.hepitches.h. Il fileanalogWave.hcontiene la definizione della classeanalogWave, mentrepitches.hcontiene le definizioni delle note musicali.Istanziamento degli Oggetti e Definizione delle Costanti:
Crea un oggetto
waveutilizzando la classeanalogWavee definisciPIR_PINcome 2, che è il pin collegato al sensore PIR.Array della Melodia:
L’array
melodydefinisce una melodia musicale, con ogni nota seguita da un numero che rappresenta la sua durata. I numeri negativi rappresentano note puntate (aumentando la durata del 50%).Variabili Globali:
Definisci alcune variabili globali per la condivisione dei dati tra le funzioni.
setup():Inizializza
PIR_PINcome input e imposta la frequenza dell’onda sinusoidale a 10 Hz utilizzandowave.sine(10).loop():Monitora continuamente il valore del sensore PIR. Se viene rilevata la presenza umana (pirValue è HIGH), chiama la funzione
playMelody()per riprodurre la melodia e attendi 10 secondi per evitare la riproduzione ripetitiva della melodia.playMelody():Questa funzione calcola la durata di ogni nota in base ai dati nell’array
melodye riproduce la nota corrispondente. C’è una breve pausa tra le note. La funzione imposta la frequenza della forma d’onda utilizzandowave.freq()e controlla la durata delle note e delle pause tra le note utilizzando la funzionedelay().Nota: Assicurati che il file header
pitches.hesista effettivamente prima di eseguire questo codice.