Nota
Ciao, benvenuto nella Community SunFounder Raspberry Pi & Arduino & ESP32 su Facebook! Approfondisci le tue conoscenze su Raspberry Pi, Arduino e ESP32 insieme ad altri appassionati.
Perché unirti?
Supporto esperto: Risolvi problemi post-vendita e sfide tecniche con l’aiuto della nostra community e del nostro team.
Impara e condividi: Scambia suggerimenti e tutorial per migliorare le tue abilità.
Anteprime esclusive: Ottieni accesso anticipato agli annunci dei nuovi prodotti e anteprime esclusive.
Sconti speciali: Godi di sconti esclusivi sui nostri prodotti più recenti.
Promozioni festive e giveaway: Partecipa a giveaway e promozioni festive.
👉 Pronto a esplorare e creare con noi? Clicca su [Qui] e unisciti oggi!
Convertitore Digitale-Analogico (DAC)
L’Arduino Uno R4 WiFi è dotato di una funzionalità DAC (Convertitore Digitale-Analogico) integrata. Un DAC è fondamentale per convertire i segnali digitali nei loro corrispettivi analogici, una funzionalità particolarmente vitale in applicazioni come l’elaborazione audio, la generazione di segnali analogici e altri scenari che richiedono un’uscita analogica precisa.
Il DAC sull’Uno R4 WiFi offre una risoluzione fino a 12 bit, fornendo capacità di uscita analogica reale che superano quelle dei pin PWM.
Suonare Musica con il DAC
Schema del Circuito
Caricare il Codice
Apri il file 07-dac.ino situato in elite-explorer-kit-main\r4_new_feature\07-dac, oppure incolla il seguente codice nel tuo Arduino IDE.
Nota
Assicurati di posizionare il file pitches.h nella stessa directory del codice per garantire il corretto funzionamento.
/*****************
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 This code is aimed at producing a melody through a DAC (Digital to
3 Analog Converter) on pin A0 of an Arduino Uno R4 board using the
4 analogWave library. The melody and rhythm are defined within an
5 array where each note is followed by its duration, based on a
6 specified tempo. The analogWave object is initialized to generate
7 sine waves, and the melody is played within the main loop. Each
8 note is played for 85% of its duration, followed by a pause of 15%
9 of the note's duration before proceeding to the next note. The
10 melody repeats indefinitely with a one-second pause between
11 repetitions.
12
13 Board: Arduino Uno R4
14*/
15
16
17#include "analogWave.h"
18#include "pitches.h"
19
20// Melody and rhythm defined as an array
21// Each note is followed by its duration (4 = quarter note, 8 = eighth note, etc.)
22int melody[] = {
23 // Super Mario Bros theme
24 // Score available at https://musescore.com/user/2123/scores/2145
25 // Theme by Koji Kondo
26 NOTE_E5,8, NOTE_E5,8, REST,8, NOTE_E5,8, REST,8, NOTE_C5,8, NOTE_E5,8, //1
27 NOTE_G5,4, REST,4, NOTE_G4,8, REST,4,
28 NOTE_C5,-4, NOTE_G4,8, REST,4, NOTE_E4,-4, // 3
29 NOTE_A4,4, NOTE_B4,4, NOTE_AS4,8, NOTE_A4,4,
30 NOTE_G4,-8, NOTE_E5,-8, NOTE_G5,-8, NOTE_A5,4, NOTE_F5,8, NOTE_G5,8,
31 REST,8, NOTE_E5,4,NOTE_C5,8, NOTE_D5,8, NOTE_B4,-4,
32};
33
34analogWave wave(DAC); // Initialize the analogWave object for DAC(A0)
35
36int noteCounter = 0; // Index to keep track of which note is being played
37int tempo = 200; // Set the tempo in BPM (Beats Per Minute)
38int wholenote = (60000 * 4) / tempo; // Calculate the duration of a whole note in ms
39int divider = 0, noteDuration = 0; // Variables to hold note duration
40
41void setup() {
42 wave.sine(10); // Initialize the sine wave generator with a frequency of 10 Hz
43}
44
45void loop() {
46 // Calculate the duration of the current note
47 divider = melody[noteCounter + 1];
48 if (divider > 0) {
49 // For regular notes
50 noteDuration = wholenote / divider;
51 } else if (divider < 0) {
52 // For dotted notes (duration increased by 50%)
53 noteDuration = wholenote / abs(divider);
54 noteDuration *= 1.5; // Increase the duration by 50% for dotted notes
55 }
56
57 // Play the note
58 wave.freq(melody[noteCounter]);
59 delay(noteDuration * 0.85); // Play the note for 85% of its duration
60 wave.stop();
61
62 // Pause between notes
63 delay(noteDuration * 0.15); // Pause for 15% of the note duration
64
65 // Increment the note counter by 2 (because each note is followed by its duration)
66 noteCounter += 2;
67
68 // Reset the counter when reaching the end of the melody
69 int totalNotes = sizeof(melody) / sizeof(melody[0]);
70 noteCounter = noteCounter % totalNotes;
71
72 // Add a delay between repetitions of the melody
73 if (noteCounter == 0) {
74 delay(1000);
75 }
76}
Questo progetto sfrutta l’Arduino e il DAC (Convertitore Digitale-Analogico) per suonare il famoso tema di Super Mario Bros. Utilizza una libreria chiamata analogWave per la generazione di onde sinusoidali e un’altra libreria, pitches.h, per definire le frequenze delle note.
melody[]: Questo array contiene le note da suonare insieme alle loro durate. Le note sono rappresentate da altezze predefinite (ad esempio,NOTE_E5) e le durate sono rappresentate in termini di battiti (ad esempio, 4 indica una semiminima). Puoi provare a comporre la tua melodia modificando le note e le durate nell’array melody[]. Se sei interessato, esiste un repository su GitHub (robsoncouto/arduino-songs) che fornisce codice Arduino per suonare diverse canzoni. Sebbene il loro approccio possa essere diverso da questo progetto, puoi fare riferimento alle loro note e durate. (Sostituisci semplicemente il melody[] nella traccia corrispondente con il codice in questo progetto.)tempo: Il tempo per questo progetto è impostato a 200 BPM (Battiti Per Minuto), utilizzato per calcolare la durata di ogni nota. Modificando questo valore, cambierai la velocità dell’esecuzione della canzone.Generatore di onde sinusoidali: La funzione
sinedella libreriaanalogWaveinizializza un generatore di onde sinusoidali a 10 Hz, utilizzato per emettere le note tramite il DAC.Durata della nota: In base al tempo impostato e al conteggio dei battiti per ogni nota, viene calcolata la durata di ogni nota.
Riproduzione e pausa: Ogni nota viene suonata per l’85% della sua durata calcolata, seguita da una pausa del 15% per distinguere tra le note.
Ciclo: Al termine della melodia, il codice si resetta automaticamente e ricomincia a suonare.
Questo è un esempio che dimostra come utilizzare Arduino e hardware esterno (DAC) per generare musica. Mostra anche come utilizzare array e cicli per semplificare la logica della riproduzione musicale.
Riferimenti