Komponeendid:
1 Piezor
2 juhtme
Skeem:

kood:
// 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); }
Katse 6.1
Töö protsess:
Veerutage potentsiomeetrid ja tulevad erinevad hääled
Komponendid:
1 Buzzer
1 Arendusplaat
7 Juhtmed
1 Arduino Uno
1 Potentsiomeeter
Reaalse elu rakendused:
Piezoelektrilist efekti kasutatakse mitmel otstarbel reaalelus, sealhulgas andurites, ultraheli meditsiinis, kõlarites, süüteküünaldes ja aktuaatorites. See võimaldab muuta mehaanilise energia elektriliseks signaaliks ning vastupidi, mis on oluline paljudes rakendustes.
Skeem:

Kood:
const int BUZZERPIN = 9; const int POTENPIN = A0; const int melodyCount = 3; const int melodyThresholds[melodyCount] = {341, 682, 1023}; const int melody1[] = {261, 329, 392, 523}; const int noteDurations1[] = {4, 4, 4, 4}; const int melody2[] = {440, 392, 349, 330, 294, 261}; const int noteDurations2[] = {8, 8, 8, 8, 8, 8}; const int melody3[] = {329, 349, 392, 440, 494, 523}; const int noteDurations3[] = {2, 2, 2, 2, 2, 2}; void setup() { pinMode(BUZZERPIN, OUTPUT); } void loop() { int potValue = analogRead(POTENPIN); int selectedMelody = 0; for (int i = 0; i < melodyCount; i++) { if (potValue < melodyThresholds[i]) { selectedMelody = i; break; } } playMelody(selectedMelody); delay(100); } void playMelody(int melodyIndex) { const int* melody; const int* noteDurations; int noteCount; switch (melodyIndex) { case 0: melody = melody1; noteDurations = noteDurations1; noteCount = sizeof(melody1) / sizeof(int); break; case 1: melody = melody2; noteDurations = noteDurations2; noteCount = sizeof(melody2) / sizeof(int); break; case 2: melody = melody3; noteDurations = noteDurations3; noteCount = sizeof(melody3) / sizeof(int); break; default: return; } for (int i = 0; i < noteCount; i++) { int noteFrequency = melody[i]; int noteDuration = 1000 / noteDurations[i]; tone(BUZZERPIN, noteFrequency, noteDuration); delay(noteDuration * 1.3); noTone(BUZZERPIN); delay(50); } }
Uued funktsioonid:
- tone(buzzerPin, sagedus): See funktsioon mängib heli piezo emitteriga, mis on ühendatud buzzerPin’ile, kindlaksmääratud sagedusega hertsides.
- noTone(buzzerPin): See funktsioon lõpetab heli mängimise piezo emitteril, mis on ühendatud buzzerPiniga.
Video: