miércoles, 25 de noviembre de 2015

Stairway to Heaven played on Arduino with buzzer


SKETCH




CODE
#define NB0  31
#define NC1  33
#define NCS1 35
#define ND1  37
#define NDS1 39
#define NE1  41
#define NF1  44
#define NFS1 46
#define NG1  49
#define NGS1 52
#define NA1  55
#define NAS1 58
#define NB1  62
#define NC2  65
#define NCS2 69
#define ND2  73
#define NDS2 78
#define NE2  82
#define NF2  87
#define NFS2 93
#define NG2  98
#define NGS2 104
#define NA2  110
#define NAS2 117
#define NB2  123
#define NC3  131
#define NCS3 139
#define ND3  147
#define NDS3 156
#define NE3  165
#define NF3  175
#define NFS3 185
#define NG3  196
#define NGS3 208
#define NA3  220
#define NAS3 233
#define NB3  247
#define NC4  262
#define NCS4 277
#define ND4  294
#define NDS4 311
#define NE4  330
#define NF4  349
#define NFS4 370
#define NG4  392
#define NGS4 415
#define NA4  440
#define NAS4 466
#define NB4  494
#define NC5  523
#define NCS5 554
#define ND5  587
#define NDS5 622
#define NE5  659
#define NF5  698
#define NFS5 740
#define NG5  784
#define NGS5 831
#define NA5  880
#define NAS5 932
#define NB5  988
#define NC6  1047
#define NCS6 1109
#define ND6  1175
#define NDS6 1245
#define NE6  1319
#define NF6  1397
#define NFS6 1480
#define NG6  1568
#define NGS6 1661
#define NA6  1760
#define NAS6 1865
#define NB6  1976
#define NC7  2093
#define NCS7 2217
#define ND7  2349
#define NDS7 2489
#define NE7  2637
#define NF7  2794
#define NFS7 2960
#define NG7  3136
#define NGS7 3322
#define NA7  3520
#define NAS7 3729
#define NB7  3951
#define NC8  4186
#define NCS8 4435
#define ND8  4699
#define NDS8 4978

#define melodyPin 3

int melody[] = {
  NA4, NC5, NE5, NA5, NB5, NE5, NC5, NB5, 
  NC6, NE5, NC5, NC6, NFS5, ND5, NA4, NFS5, 
  NE5, NC5, NA4, NC5, 0, NE5, NC5, NA4, 
  NG4, NA4, NA4, 0, 0, NA4, NF5, NE5,

  NA4, NA4, NC5, NE5, NB5, NE5, NC5, NB5, 
  NC6, NE5, NC5, NC6, NFS5, ND5, NA4, NFS5, 
  NE5, NC5, NA4, NC5, 0, NE5, NC5, NA4, 
  NG4, NA4, NA4, 0, 0, 0, NA4, NB4,

  NC5, NE5, NG5, NC5, NB4, ND5, NG5, NB4, 
  NB4, NC5, NA4, NA4, NC5, NE5, NA5, NA4, 
  NB4, NC5, NG4, NC4, NC5, NG5, ND5, NG4, 
  NG5, NG5, NFS5, ND5, ND5, 0, 0, NA4, NB4, 

  NC5, NE5, NG5, NC5, NB4, ND5, NG5, NB4, 
  NB4, NC5, NA4, NA4, NC5, NE5, NA5, NA4, NB4, 
  NC5, NE5, NG5, NC6, ND5, NFS5, NA5, 
  ND6, NE5, NE5, NE5, 0, 0,
};

int tempo[] = {
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 4, 12, 8, 8, 8,
8, 8, 8, 8, 4, 8, 8, 8,

8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 4, 12, 8, 8, 8,
8, 8, 8, 8, 4, 8, 8, 8,

8, 8, 8, 8, 8, 8, 8, 8,
16, 16, 8, 8, 8, 8, 8, 8, 8, 
8, 8, 8, 8, 8, 8, 8, 8,
16, 16, 8, 4, 12, 8, 8, 8,

8, 8, 8, 8, 8, 8, 8, 8,
16, 16, 8, 8, 8, 8, 8, 8, 8, 
8, 8, 8, 8, 8, 8, 8, 
8, 8, 8, 8, 2, 4, 4,
};


void setup(void)
{
  pinMode(3, OUTPUT);
  pinMode(13, OUTPUT);
}
void loop()
{
  sing(1);
}
int song = 0;

void sing(int s) 
{
  song = s;
  if (song == 1) {
    Serial.println(" 'Stairway to Heaven'");
    int size = sizeof(melody) / sizeof(int);
    for (int thisNote = 0; thisNote < size; thisNote++) {

      int noteDuration = 1200 / tempo[thisNote];

      buzz(melodyPin, melody[thisNote], noteDuration);

      int pauseBetweenNotes = noteDuration * 1.30;
      delay(pauseBetweenNotes);

      buzz(melodyPin, 0, noteDuration);

    }

  } 
  }


void buzz(int targetPin, long frequency, long length) {
  digitalWrite(13, HIGH);

  long delayValue = 1000000 / frequency / 2;
  long numCycles = frequency * length / 1000; 
  for (long i = 0; i < numCycles; i++) { 
    digitalWrite(targetPin, HIGH);
    delayMicroseconds(delayValue);
    digitalWrite(targetPin, LOW); 
    delayMicroseconds(delayValue);
  }
  digitalWrite(13, LOW);
}

No hay comentarios:

Publicar un comentario