Практика 6 Buzzer

Buzzer — elektromehaaniline, elektrooniline või piesoelektriline signaalseade.

Ülesanne 6 Buzzeri kasutamine

Подобный прибор может использоваться в многих приборах где издаются незамысловатые звуки ( духовка, микроволновка, старые телефоны)

char notes1[] = «cdfda ag cdfdg gf»; задает ноты с помощью букв, каждая буква определенная нота

int beats1[] = {1, 1, 1, 1, 1, 1, 4, 4, 2, 1, 1, 1, 1, 1, 1, 4, 4, 2}; продолжительность каждой ноты

int tempo1 = 150; темп мелодии, чем меньше значение, тем быстрее темп

int songLength2 = 29; длинна мелодии

Я использовал в своей сборке: 7 проводов, потенциометр и базер

töövideo:

https://drive.google.com/file/d/1Qkq72dK014cYe5AOBWekO8XafyHV0YoO/view?usp=sharing

https://drive.google.com/file/d/1GpQjftfhyaA74pR1cdSz3qaws1RToJ61/view?usp=sharing

const int buzzerPin = 9;
const int potepin = A0;

// 1
char notes1[] = "cdfda ag cdfdg gf";
int beats1[] = {1, 1, 1, 1, 1, 1, 4, 4, 2, 1, 1, 1, 1, 1, 1, 4, 4, 2};
int tempo1 = 150;
int songLength1 = 18;

// 2
char notes2[] = "dbbabgdddbbCaD DeeCCbagdbbabg";
int beats2[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 , 2, 1};
int tempo2 = 200;
int songLength2 = 29;

//3
char melody[] = "ccggeCFFCC GGFFECCggccggeCFFCC GGFFECCg";

int Durations[] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4};

int Value;

void setup() {
  pinMode(buzzerPin, OUTPUT);
  pinMode(potepin, INPUT);
}

void loop() {
  Value = analogRead(potepin);
  Value = map(Value, 0, 1023, 1, 6);
  Value = constrain(Value, 1, 6);

  int i, duration;
  
  if (Value == 1) {
    for (i = 0; i < songLength1; i++) {
      duration = beats1[i] * tempo1;
      if (notes1[i] == ' ') {
        delay(duration);
      } else {
        tone(buzzerPin, frequency(notes1[i]), duration);
        delay(duration);
      }
      delay(tempo1 / 10);
    }
    while(true){}
  }
  
  else if (Value == 2) {
    for (i = 0; i < songLength2; i++) {
      duration = beats2[i] * tempo2;
      if (notes2[i] == ' ') {
        delay(duration);
      } else {
        tone(buzzerPin, frequency(notes2[i]), duration);
        delay(duration);
      }
      delay(tempo2 / 10);
    }
    while(true){}
  }
  else if (Value == 3) {
    for(int i=0;i<2;i++){
      for (int Note = 0; Note < 28; Note++) {
        int Duration = 1000/Durations[Note];
        tone(buzzerPin, melody[Note], Duration);

        int pauseNotes = Duration * 1.30;
        delay(pauseNotes);
        noTone(buzzerPin);
      }
      delay(200);
    }
    delay(6000);
  }
}

int frequency(char note) {
  int i;
  const int numNotes = 8;
  char names[] = {'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C'};
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  for (i = 0; i < numNotes; i++) {
    if (names[i] == note) {
      return frequencies[i];
    }
  }
  return 0;
}