I buzzer sono dei dispositivi elettromeccanici in grado di emettere suoni.

Collegandosi al seguente link:

http://michelangelomezzelani.altervista.org/arduino-impariamo-a-usare-un-buzzer/

è possibile trovare la siegazione dell'esperienza passo-passo, per i più pratici invece è sufficiente collegare un buzzer come mostrato nella figura in alto e caricare nella board Arduino il seguente sketch:

 

const int buzzer = 12;     //buzzer connesso al pin 12

void setup(){
  pinMode(buzzer,OUTPUT);
}

void loop(){
  tone(buzzer,1000,200);     //suona nota sul pin 12 alla frequenza di 1000Hz
  delay(2000);                   //aspetta 2 secondi
  noTone(buzzer);                //non suonare più
  delay(1000);
}

si osserva che il pin a cui è collegato il buzzer deve essere inizializzato come uscita digitale e che il suono è gestito tramite le due funzioni tone( ) e notone( ).

 

 
Funzione Parametri
tone(pin, frequency)

tone(pin, frequency, duration)

pin: il pin in cui generare il tono

frequency: la frequenza in Hz del tono - unsigned int

duration: durata del tono in millisecondi (opzionale) - unsigned long

notone(pin) pin: il pin nel quale smettere di generare il suono

Il buzzer può essere utilizzato per comporre suoni più complessi, come per  esempio una musica.  Il file seguente genera una serie di toni corrispondenti alle varie note che messi in successione con la giusta durata formano un motivo molto conosciuto.

 

#define  c3    7634
#define  d3    6803
#define  e3    6061
#define  f3    5714
#define  g3    5102
#define  a3    4545
#define  b3    4049
#define  c4    3816    // 261 Hz 
#define  d4    3401    // 294 Hz 
#define  e4    3030    // 329 Hz 
#define  f4    2865    // 349 Hz 
#define  g4    2551    // 392 Hz 
#define  a4    2272    // 440 Hz 
#define  a4s   2146
#define  b4    2028    // 493 Hz 
#define  c5    1912    // 523 Hz
#define  d5    1706
#define  d5s   1608
#define  e5    1517
#define  f5    1433
#define  g5    1276
#define  a5    1136
#define  a5s   1073
#define  b5    1012
#define  c6    955


int speakerOut = 12;  // Il buzzer è al piedino 12

int DEBUG = 1;

void setup() { 
 pinMode(speakerOut, OUTPUT);
 if (DEBUG) { 
   Serial.begin(9600); 
 } 
}


int melody[] = {  f4,  f4, f4,  a4s,   f5,  d5s,  d5,  c5, a5s, f5, d5s,  d5,  c5, a5s, f5, d5s, d5, d5s,   c5};
int beats[]  = {  21,  21, 21,  128,  128,   21,  21,  21, 128, 64,  21,  21,  21, 128, 64,  21, 21,  21, 128 }; 

int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 50; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int toneM = 0;
int beat = 0;
long duration  = 0;

// PLAY TONE  ==============================================
// Pulse the speaker to play a tone for a particular duration
void playTone() {
 long elapsed_time = 0;
 if (toneM > 0) { // if this isn't a Rest beat, while the tone has 
   //  played less long than 'duration', pulse speaker HIGH and LOW
   while (elapsed_time < duration) {

     digitalWrite(speakerOut,HIGH);
     delayMicroseconds(toneM / 2);

     // DOWN
     digitalWrite(speakerOut, LOW);
     delayMicroseconds(toneM / 2);

     // Keep track of how long we pulsed
     elapsed_time += (toneM);
   } 
 }
 else { // Rest beat; loop times delay
   for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
     delayMicroseconds(duration);  
   }                                
 }                                 
}

// LET THE WILD RUMPUS BEGIN =============================
void loop() {
 // Set up a counter to pull from melody[] and beats[]
 for (int i=0; i<MAX_COUNT; i++) {
   toneM = melody[i];
   beat = beats[i];

   duration = beat * tempo; // Set up timing

   playTone(); 
   // A pause between notes...
   delayMicroseconds(pause);

   if (DEBUG) { // If debugging, report loop, tone, beat, and duration
     Serial.print(i);
     Serial.print(":");
     Serial.print(beat);
     Serial.print(" ");    
     Serial.print(toneM);
     Serial.print(" ");
     Serial.println(duration);
   }
 }
}

 

 

Ultime modifiche: mercoledì, 10 gennaio 2024, 18:03