int BUZZER =26;
#define C 2109.89
#define D 1879.69
#define E 1674.62
#define A 1254.55
#define G 1408.18
#define F 1580.63
#define B 1117.67
double melody[]={C,D,C,F,E,C,D,C,G,F,C,C,A,F,E,D,B,A,F,G,F};
int max_count = sizeof(melody)/2;
long tempo = 10000;
int pause_= 1000;
int restcount = 2;
int tone_ = 0;
int beat=0;
long duration=0;
void setup(){
Serial.begin(9600);
pinMode(BUZZER,OUTPUT);
}
void loop(){
for(int i=0;i<max_count;i++)
{
tone_=melody[i];
beat=50;
duration=beat*tempo;
playTone();
delayMicroseconds(pause_);
}
}
void playTone(){
long elapsedTime = 0;
if(tone_ >0)
{
while(elapsedTime<duration)
{
digitalWrite(BUZZER,HIGH);
delayMicroseconds(tone_/2);
digitalWrite(BUZZER,LOW);
delayMicroseconds(tone_/2);
elapsedTime+=tone_;
}
}
else{
for(int j=0;j<restcount;j++)
{
delayMicroseconds(duration);
}
}
}