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!

Piano di Frutta

Questo progetto è un semplice piano di frutta che legge gli input da un sensore touch MPR121 e riproduce musica tramite un DAC. In altre parole, abbiamo trasformato la frutta in una tastiera, permettendoti di suonare musica semplicemente toccandola.

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+

Elite Explorer Kit

Puoi anche acquistarli separatamente dai link sottostanti.

INTRODUZIONE COMPONENTI

LINK ACQUISTO

Arduino Uno R4 WiFi

-

Breadboard

ACQUISTA

Cavi Jumper

ACQUISTA

Resistenza

ACQUISTA

MPR121

-

Modulo Audio e Altoparlante

-

Collegamenti

../_images/02_fruit_piano_bb.png

Schema Elettrico

../_images/02_fruit_piano_schematic.png

Codice

Nota

  • Puoi aprire il file 02_fruit_piano.ino nel percorso elite-explorer-kit-main\fun_project\02_fruit_piano direttamente.

  • Oppure copia questo codice nell’Arduino IDE.

Nota

Per installare la libreria, usa l’Arduino Library Manager, cerca «Adafruit MPR121» e installala.

pitches.h
/*****************
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
02_fruit_piano.ino
 1/*
 2  The code is an Arduino program for a touch-sensitive piano. 
 3  It utilizes the MPR121 capacitive touch sensor and a DAC 
 4  (Digital-to-Analog Converter) to produce musical notes. 
 5  When a particular touch channel is triggered, it plays the 
 6  corresponding musical note.
 7
 8  Board: Arduino Uno R4 
 9  Component: MPR121, Audio Module, and Speaker
10  Library: https://github.com/adafruit/Adafruit_MPR121 (Adafruit_MPR121 by Adafruit)
11*/
12
13#include <Wire.h>             // Include the Wire library for I2C communication
14#include "Adafruit_MPR121.h"  // Include the Adafruit MPR121 library for capacitive touch sensing
15#include "analogWave.h"       // Include the analogWave library for wave generation
16#include "pitches.h"          // Include the pitches library that defines musical notes
17
18// Initialize MPR121 and analogWave objects
19Adafruit_MPR121 cap = Adafruit_MPR121();
20analogWave wave(DAC);
21
22// Define notes for each touch channel
23int notes[] = {
24  NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4,
25  NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5,
26  NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5
27};
28
29void setup() {
30  Serial.begin(9600);
31  while (!Serial);
32
33  Serial.println("MPR121 Water Fruit Piano Setup");
34
35  // Initialize MPR121 sensor
36  if (!cap.begin(0x5A)) {
37    Serial.println("MPR121 not found. Check wiring.");
38    while (1);  // Halt execution if MPR121 is not found
39  }
40  Serial.println("MPR121 initialized.");
41  cap.setAutoconfig(true);
42  wave.sine(10);  // Initialize the sine wave generator with a frequency of 10 Hz
43}
44
45void loop() {
46  uint16_t touched = cap.touched();  // Read the touch state from the MPR121 sensor
47
48  for (int i = 0; i < 12; i++) {
49    if (touched & (1 << i)) {  // Check if the channel is touched
50      playNote(notes[i]);      // Play the corresponding note
51    }
52  }
53
54  delay(10);  // Add a small delay to reduce sensor reading frequency
55}
56
57void playNote(int note) {
58  wave.freq(note);  // Set frequency for the DAC
59  delay(300);       // Delay for a short period to play the note
60  wave.stop();      // Stop the wave after the note is played
61}

Come funziona?

Ecco una spiegazione passo-passo del codice:

  1. Inizializzazione delle Librerie e degli Oggetti:

    Importa le librerie necessarie: la libreria Wire (per la comunicazione I2C), la libreria Adafruit_MPR121 (per controllare l’MPR121), la libreria analogWave (per generare forme d’onda analogiche) e pitches.h (che definisce le frequenze delle note). Crea istanze degli oggetti Adafruit_MPR121 e analogWave. Definisci un array di note per memorizzare la nota corrispondente a ciascun canale touch.

  2. setup():

    Inizializza la comunicazione Serial e attendi che inizi. Controlla e inizializza l’MPR121; se non trovato, stampa un messaggio di errore sul monitor seriale e interrompi l’esecuzione. Inizializza l’oggetto analogWave e imposta la frequenza iniziale dell’onda sinusoidale a 10Hz.

  3. loop():

    Leggi i canali attualmente toccati dell’MPR121. Itera attraverso tutti i canali, controlla quale è toccato e riproduci la nota corrispondente. Aggiungi un piccolo ritardo tra ogni iterazione.

  4. Riproduci Nota playNote():

    La funzione playNote prende un parametro note e imposta la frequenza del DAC per riprodurre la nota corrispondente. Ritarda per un periodo per riprodurre la nota. Interrompi la riproduzione della nota.