#define C 277.81
#define c 261.63
#define D 293.66
#define E 329.63
#define F 349.23
#define G 392.00
#define A 440.00
#define B 493.88
#define R 0
// define pin
int speakerOut = 26;
int DEBUG = 1;
void setup() {
pinMode(speakerOut, OUTPUT);
if (DEBUG) {
Serial.begin(9600);
}
}
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 paaus = 1000;
int rest_count = 2;
int tone_ = 0;
int beat = 0;
long duration = 0;
void playTone() {
long elapsed_time=0;
if(tone_>0){
while(elapsed_time<duration){
digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone_/2);
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone_/2);
elapsed_time+=(tone_);
}
}
else {
for (int j = 0; j < rest_count; j++) {
delayMicroseconds(duration);
}
}
}
void loop() {
for(int i=0; i<MAX_COUNT; i++){
tone_=melody[i];
beat=50;
duration=beat*tempo;
playTone();
delayMicroseconds(paaus);
}
}