#define C 523
#define D 587
#define E 659
#define F 693
#define G 784
#define A 880
#define B 988
#define R 0
int t[7]={523, 587, 659, 698, 784, 880, 988};
int speakerOut= 26;
int DEBUG=1;
double melody[]={
E,E,E,R,
E,E,E,R,
E,G,C,D,E,R,
f,f,f,f,f,E,E,E,E,D,D,E,D,R,G,R,
E,E,E,R,
E,E,E,R,
E,G,C,D,E,R,
f,f,f,f,f,E,E,E,G,G,f,D,C,R
};
int max_count= sizeof(melody)/2;
long tempo=10000;
int paause=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 setup() {
// put your setup code here, to run once:
pinMode(speakerOut, OUTPUT);
if(DEBUG){
Serial.begin(9600);
}
Serial.println("Hello, ESP32!");
}
void loop() {
// put your main code here, to run repeatedly:
for (int i=0;i<max_count;i++){
tone_=melody[i];
beat=50;
duration=beat*tempo;
playtone();
delayMicroseconds(paause);
}
delay(10); // this speeds up the simulation
}