Practice 6 Buzzer

6.1 Katse (Buzzer)

Komponeendid:
1 Piezor
2 juhtme

skeem:

code:

// Meloodiate mängimine.

// Käsk Arduino tone() - noote tihedus.

// Noodid:

// note 	frequency

// c     262 Hz

// d     294 Hz

// e     330 Hz

// f     349 Hz

// g     392 Hz

// a     440 Hz

// b     494 Hz

// C     523 Hz

const int buzzerPin = 9;

// pikkus on nootide ja pausite koguste summa

const int songLength = 18;

char notes[] = "cdfda ag cdfdg gf "; // tähed on noodid ja tühik on paus

// Rütmi seadistamine.

int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};

// "tempo" meloodia kiirus. Kui väiksem tempo_ siis suurem kiirus.

int tempo = 150;

void setup() 

{

  pinMode(buzzerPin, OUTPUT);

}

void loop() 

{

  int i, duration;

  for (i = 0; i < songLength; i++)

  {

    duration = beats[i] * tempo; 

    if (notes[i] == ' ')          // kui noot puudub

    {

      delay(duration);          

    }

    else                         

    {

      tone(buzzerPin, frequency(notes[i]), duration);

      delay(duration);          

    }

    delay(tempo/10);              // väike paus nootide vahel

  }

  while(true){}

}

int frequency(char note) 

{

  int i;

  const int numNotes = 8;  // nootide kogus

  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };

  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};

  // kui noot on olemas, siis tagastame selle tiheduse 

  for (i = 0; i < numNotes; i++)  

  {

    if (names[i] == note)       

    {

      return(frequencies[i]);    

    }

  }

  return(0); 

}

Ülesanne 6 Buzzeri kasutamine

Koostis:

1 lcd ekraan

1 takesti (330 Ω)

18 juhtmed

1 plaat ARDUINO UNO

1 pote(reguleerimisratas)

1 breadboard small

1 – Buzzer(piezo)

“Buzzeri” tööpõhimõte

Kui toiteallikas hakkab mängima esimest lugu, mis on seatud mängima juhul, kui potentsiomeetri asend on võrdne 0 ja ekraanil kuvatakse loo nimi, sama juhtub ka juhul, kui ratast keerates muutub lugu ja nimi ekraanil.

skeem:

code:

#include <LiquidCrystal.h>
int tonePin = 9;// пин пищалки
const int SPEAKER=9;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
 
 
void setup() 
{
 
 
}
 
void gimn()
{
   
  tone(tonePin, 174, 249.99975);
    delay(277.7775);
    tone(tonePin, 233, 499.9995);
    delay(555.555);
    tone(tonePin, 174, 374.999625);
    delay(416.66625);
    tone(tonePin, 195, 124.999875);
    delay(138.88875);
    tone(tonePin, 220, 499.9995);
    delay(555.555);
    tone(tonePin, 146, 249.99975);
    delay(277.7775);
    tone(tonePin, 146, 249.99975);
    delay(277.7775);
    tone(tonePin, 195, 499.9995);
    delay(555.555);
    tone(tonePin, 174, 374.999625);
    delay(416.66625);
    tone(tonePin, 155, 124.999875);
    delay(138.88875);
    tone(tonePin, 174, 499.9995);
    delay(555.555);
    tone(tonePin, 116, 249.99975);
    delay(277.7775);
    tone(tonePin, 116, 249.99975);
    delay(277.7775);
    tone(tonePin, 130, 499.9995);
    delay(555.555);
    tone(tonePin, 130, 374.999625);
    delay(416.66625);
    tone(tonePin, 146, 124.999875);
    delay(138.88875);
    tone(tonePin, 155, 499.9995);
    delay(555.555);
    tone(tonePin, 155, 374.999625);
    delay(416.66625);
    tone(tonePin, 174, 124.999875);
    delay(138.88875);
    tone(tonePin, 195, 499.9995);
    delay(555.555);
    tone(tonePin, 220, 374.999625);
    delay(416.66625);
    tone(tonePin, 233, 124.999875);
    delay(138.88875);
    tone(tonePin, 261, 749.99925);
    delay(833.3325);
    tone(tonePin, 174, 249.99975);
    delay(277.7775);
    tone(tonePin, 293, 499.9995);
    delay(555.555);
    tone(tonePin, 261, 374.999625);
    delay(416.66625);
    tone(tonePin, 233, 124.999875);
    delay(138.88875);
    tone(tonePin, 261, 499.9995);
    delay(555.555);
    tone(tonePin, 174, 249.99975);
    delay(277.7775);
    tone(tonePin, 174, 249.99975);
    delay(277.7775);
    tone(tonePin, 233, 499.9995);
    delay(555.555);
     
}
 
void mars()
{
 
  int notes[] = {
  392, 392, 392, 311, 466, 392, 311, 466, 392,
  587, 587, 587, 622, 466, 369, 311, 466, 392,
  784, 392, 392, 784, 739, 698, 659, 622, 659,
  415, 554, 523, 493, 466, 440, 466,
  311, 369, 311, 466, 392
};
int times[] = {
  350, 350, 350, 250, 100, 350, 250, 100, 700,
  350, 350, 350, 250, 100, 350, 250, 100, 700,
  350, 250, 100, 350, 250, 100, 100, 100, 450,
  150, 350, 250, 100, 100, 100, 450,
  150, 350, 250, 100, 750
};
   
  for (int i = 0; i < 39; i++)
  {
  tone(SPEAKER, notes[i], times[i]*2);
  delay(times[i]*2);
  noTone(SPEAKER);
 }
}
 
void mario()
{
  int notes[] = {
 1318, 1318, 1318, 1046, 1318, 1568, 784,
 1046, 784, 659, 880, 987, 932, 880, 784,
 1318, 1568, 1750, 1396, 1568, 1318, 1046, 1174, 987,
 1046, 784, 659, 880, 987, 932, 880,
 784, 1318, 1568, 1750, 1396, 1568, 1318, 1046, 1174, 987,
 1568, 1480, 1396, 1244, 1318, 830, 880, 1046, 880, 1046, 1174, 
};
int times[] = {
 150, 300, 150, 150, 300, 600, 600,
 450, 150, 300, 300, 150, 150, 300, 210,
 210, 150, 300, 150, 150, 300, 150, 150, 450,
 450, 150, 300, 300, 150, 150, 300,
 210, 210, 150, 300, 150, 150, 300, 150, 150, 450,
 150, 150, 150, 300, 150, 150, 150, 150, 150, 150, 150,
};
int delays[] = {
 150, 300, 300, 150, 300, 600, 600,
 450, 450, 450, 300, 300, 150, 300, 210,
 210, 150, 300, 150, 300, 300, 150, 150, 450,
 450, 450, 450, 300, 300, 150, 300,
 210, 210, 150, 300, 150, 300, 300, 150, 150, 600,
 150, 150, 150, 300, 300, 150, 150, 300, 150, 150, 150,
 300, 150, 150, 150, 300, 300, 300, 150, 600,
 150, 150, 150, 300, 300, 150, 150, 300, 150, 150, 450, 450, 450, 1200,
};
   
  for (int i = 0; i < 75; i++){
  tone(SPEAKER, notes[i], times[i]);
  delay(delays[i]);
 }
 noTone(SPEAKER);
}
 
void bumer()
{
 
tone(tonePin, 1244, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1479, 624.99975);
 
delay(694.444166667);
 
delay(520.833125);
 
tone(tonePin, 1479, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1244, 624.99975);
 
delay(694.444166667);
 
delay(520.833125);
 
tone(tonePin, 1661, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1479, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1661, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1479, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1661, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1479, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1661, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1479, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1661, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 1864, 624.99975);
 
delay(694.444166667);
 
delay(520.833125);
 
tone(tonePin, 466, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1244, 78.12496875);
 
delay(86.8055208333);
 
delay(173.611041667);
 
tone(tonePin, 466, 78.12496875);
 
delay(86.8055208333);
 
delay(86.8055208333);
 
tone(tonePin, 466, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1479, 234.37490625);
 
delay(260.4165625);
 
tone(tonePin, 466, 78.12496875);
 
delay(86.8055208333);
 
delay(86.8055208333);
 
tone(tonePin, 466, 78.12496875);
 
delay(86.8055208333);
 
delay(260.4165625);
 
tone(tonePin, 311, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1479, 78.12496875);
 
delay(86.8055208333);
 
delay(173.611041667);
 
tone(tonePin, 311, 78.12496875);
 
delay(86.8055208333);
 
delay(86.8055208333);
 
tone(tonePin, 311, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1244, 234.37490625);
 
delay(260.4165625);
 
tone(tonePin, 311, 78.12496875);
 
delay(86.8055208333);
 
delay(86.8055208333);
 
tone(tonePin, 311, 78.12496875);
 
delay(86.8055208333);
 
delay(260.4165625);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1661, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1479, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1661, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1479, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1661, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1479, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1661, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1479, 156.2499375);
 
delay(173.611041667);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1661, 78.12496875);
 
delay(86.8055208333);
 
delay(173.611041667);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
delay(86.8055208333);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1864, 234.37490625);
 
delay(260.4165625);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
delay(86.8055208333);
 
tone(tonePin, 493, 78.12496875);
 
delay(86.8055208333);
 
delay(260.4165625);
 
tone(tonePin, 466, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 1244, 78.12496875);
 
delay(86.8055208333);
 
delay(173.611041667);
 
tone(tonePin, 466, 78.12496875);
 
delay(86.8055208333);
 
delay(86.8055208333);
 
tone(tonePin, 466, 78.12496875);
 
delay(86.8055208333);
 
tone(tonePin, 311, 234.37490625);
 
delay(260.4165625);
 
tone(tonePin, 466, 78.12496875);
 
delay(86.8055208333);
 
delay(86.8055208333);
}
 
void loop() 
{
  lcd.print("Imperskij mars");
  delay(3000);
  mars();
  delay(2000);
  lcd.clear();
  lcd.print("Melodia Mario");
  delay(5000);
  mario();
  delay(3000);
  lcd.clear();
  lcd.print("Sovetskij gimn");
  gimn();
  delay(2000);
  lcd.clear();
  lcd.print("melodia Bumer");
  bumer();
  delay(9000);
   
}

demonstratsiooni buzzeri töö:

Vinget kasutatakse :

  • Mänguasjad
  • Autod
  • Kellad
  • Mikrolaineahjud
  • Telefonid