#include "pitches.h"
// change this to make the song slower or faster
int tempo = 108;
int beat = 4;
double secends = 60000;
const byte ledPins[] = {8, 9, 10, 11};//-define pin numbers for LEDs
const byte buttonPins[] = {1, 2, 3, 4};//-define pin numbers for buttons
int buzzer = 5;
#define MAX_GAME_LENGTH 100
byte gameSequence[MAX_GAME_LENGTH] = {0};
byte gameIndex = 0;
// notes of the moledy followed by the duration.
// a 4 means a quarter note, 8 an eighteenth , 16 sixteenth, so on
// !!negative numbers are used to represent dotted notes,
// so -4 means a dotted quarter note, that is, a quarter plus an eighteenth!!
void setup()
{
Serial.begin(9600);
for (byte i = 0; i < 4; i++)
{
pinMode(ledPins[i], OUTPUT);
pinMode(buttonPins[i], INPUT_PULLUP);
}
playStartMusic();
randomSeed(analogRead(A0));
}
const PROGMEM int StartMusic[] =
{
NOTE_AS4,8, NOTE_AS4,8, NOTE_AS4,8,//1
NOTE_F5,2,
NOTE_C6,2,
NOTE_AS5,8,
NOTE_A5,8,
NOTE_G5,8,
NOTE_F6,2,
REST,4
};
void playStartMusic()
{
int notes = sizeof(StartMusic);
int wholenote = (secends * beat) / tempo;
int divider = 0;
int noteDuration = 0;
for (int thisStartMusic = 0; thisStartMusic < notes * 2; thisStartMusic = thisStartMusic + 2)
{
// calculates the duration of each note
divider = pgm_read_word_near(StartMusic+thisStartMusic + 1);
if (divider > 0)
{
// regular note, just proceed
noteDuration = (wholenote) / divider;
} else
if (divider < 0)
{
// dotted notes are represented with negative durations!!
noteDuration = (wholenote) / abs(divider);
noteDuration *= 1.5; // increases the duration in half for dotted notes
}
// we only play the note for 90% of the duration, leaving 10% as a pause
tone(buzzer, pgm_read_word_near(StartMusic+thisStartMusic ), noteDuration * 0.9);
// Wait for the specief duration before playing the next note.
delay(noteDuration);
// stop the waveform generation before the next note.
noTone(buzzer);
}
}
void loop()
{
gameSequence[gameIndex] = random(0, 4);
gameIndex++;
// no need to repeat the StartMusic.
for(int i = 0; i<4; i++)
{
digitalWrite(ledPins[i], HIGH);
delay(500);
}
}