Звук на arduino

Нет ответов
seroja
Offline
Зарегистрирован: 14.12.2015

В интернете много тем по звукогенерации на ардуино. Но, как правило, ограничиваются простейшими мелодиями. Хочу поделиться своим опытом, как проиграть мелодию на ардуино. Для начала видео рабочего проекта:
https://youtu.be/3R19_3YpOPM 
Т.к. музыкального образования не имею, в качестве мелодий выбрал коды мелодий nokia 3310. Благо, в интернетах остался стратегический запас еще лет на 200 https://vk.com/topic-96676_22391837
После переделки пары мелодий для ардуино, написал скриптик, котрый упрощает эту рутинную работу https://drive.google.com/file/d/0BxDGOGmLrG9NdnV0ZkRjRHp4VmM/view?usp=sharing

Впоросы можно задать здесь: https://vk.com/id15431633

Вот полный скетч:
 

#include "pitches.h"

//int melody_1[] = { NOTE_E2, NOTE_G2, 0, NOTE_G2, NOTE_E2, 0, NOTE_A2, NOTE_G2, NOTE_A2, NOTE_G2, NOTE_A2, NOTE_G2, NOTE_A2, NOTE_G2, NOTE_A2, NOTE_B2 };
//int noteDurations_1[] =  { 8, 4, 4, 8, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4 };

int melody_1[] = {NOTE_E4, NOTE_G4, 0, NOTE_G4, NOTE_E4, 0, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_G4, NOTE_A4, NOTE_B4};
int noteDurations_1[] = {8, 4, 4, 8, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4};

int melody_2[] = {NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_FS4, 0, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_B4, NOTE_FS4, 0, NOTE_B4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, 0, 0, 0, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_E4, 0, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_A4, NOTE_E4, 0, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_F4, NOTE_E4, NOTE_F4};
int noteDurations_2[] = {8, 8, 8, 4, 4, 8, 8, 8, 8, 4, 4, 8, 4, 4, 8, 8, 8, 4, 8, 8, 8, 8, 8, 8, 4, 4, 8, 8, 8, 8, 4, 4, 8, 4, 4, 8, 8, 8, 4};

int melody_3[] = {NOTE_GS4, NOTE_E4, NOTE_GS4, NOTE_CS5, NOTE_A4, 0, NOTE_FS4, NOTE_DS4, NOTE_FS4, NOTE_B4, NOTE_GS4, NOTE_FS4, NOTE_E4, 0, NOTE_E4, NOTE_CS4, NOTE_FS4, NOTE_CS4, 0, NOTE_FS4, NOTE_E4, NOTE_GS4, NOTE_FS4};
int noteDurations_3[] = {8, 8, 8, 8, 4, 4, 8, 8, 8, 8, 4, 8, 8, 4, 8, 8, 4, 4, 4, 8, 8, 4, 4};

int melody_4[] = { NOTE_D3, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, NOTE_FS3, NOTE_E3, NOTE_C3, NOTE_E3, NOTE_A3, NOTE_A3, NOTE_B3, NOTE_A3, NOTE_G3, NOTE_FS3, NOTE_D3, NOTE_D3, NOTE_B3, NOTE_B3, NOTE_C4, NOTE_B3, NOTE_A3, NOTE_G3, NOTE_E3, NOTE_D3, NOTE_D3, NOTE_E3, NOTE_A3, NOTE_FS3, NOTE_G3};
int noteDurations_4[] = {4, 4, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 8, 8, 4, 4, 4, 2};

int melody_5[] = {NOTE_E4, NOTE_E4, NOTE_GS4, NOTE_A4, 0, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_E4, NOTE_E4, NOTE_GS4, NOTE_A4, 0, NOTE_C5, NOTE_B4, NOTE_A4, NOTE_F4, NOTE_F4, NOTE_GS4, NOTE_A4, 0, NOTE_F4, NOTE_GS4, NOTE_A4, NOTE_C5, NOTE_C5, NOTE_B4, NOTE_B4, NOTE_A4, NOTE_A4, NOTE_GS4,  NOTE_GS4, NOTE_A4};
int noteDurations_5[] = {8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 2};

int melody_6[] = {NOTE_E3, NOTE_C4, NOTE_B3, NOTE_A3, NOTE_GS3, NOTE_A3, 03, 03, 03, 03, NOTE_E3, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_B3, NOTE_C4, 0, 0, 0, 0, NOTE_A3, NOTE_F4, 0, NOTE_E4, NOTE_D4, 0, NOTE_C4, NOTE_B3, NOTE_C4, NOTE_D4, 0, 0, NOTE_F4, NOTE_E4, NOTE_D4, NOTE_C4, NOTE_D4, NOTE_B3};
int noteDurations_6[] = {4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 4, 4, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 4, 4, 4, 4, 4, 4};

int melody_7[] = {NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_D4, NOTE_F4, NOTE_DS4, NOTE_C4, 0, NOTE_DS3, NOTE_G3, NOTE_C4, NOTE_D4, 0, NOTE_G3, NOTE_B3, NOTE_D4, NOTE_DS4, 0, NOTE_G3, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_FS4, NOTE_G4, NOTE_D4, NOTE_F4, NOTE_DS4, NOTE_C4, 0, NOTE_DS3, NOTE_G3, NOTE_C4, NOTE_D4, 0, NOTE_G3, NOTE_DS4, NOTE_D4, NOTE_C4};
int noteDurations_7[] = {8, 8, 8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4, 8, 8, 8, 8, 4};

void play(int melody[],int noteDurations[],int msize, int slow){
  
    for (int thisNote = 0; thisNote < msize; thisNote++) {

    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.

    int noteDuration = 1000 / noteDurations[thisNote];
    digitalWrite(13, HIGH);
    tone(8, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    digitalWrite(13, LOW);
    
    int pauseBetweenNotes = noteDuration * slow;
    delay(pauseBetweenNotes);
      
    noTone(8);
  }
}

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

void loop() {
  // no need to repeat the melody.
  play(melody_1, noteDurations_1, 16, 1.9);
  delay(5000);
  
  play(melody_2, noteDurations_2, 40, 1.99);
  delay(5000); 
  
  play(melody_3, noteDurations_3, 24, 1.9);
  delay(5000); 
  
  play(melody_4, noteDurations_4, 30, 1.2);
  delay(5000);  
  
  play(melody_5, noteDurations_5, 33, 1.9);
  delay(5000);    
  
  play(melody_6, noteDurations_6, 41, 1.9);
  delay(5000);   

  play(melody_7, noteDurations_7, 40, 1.9);
  delay(5000); 
  
}