//Pin Initialization
//Define the pin no for the push button
const int buttonPin = 9;
const int ledPin = 10 ;
const int buzzPin = 11;
int melody[] = {
294, 294, 392, 330, 294, 247, 262,
294, 294, 392, 330, 294, 247, 262,
294, 330, 349, 294, 330, 349, 392,
262, 330, 294, 262, 220, 247, 262
};
int noteDurations[] = {
4, 8, 8, 4, 8, 8, 2,
4, 8, 8, 4, 8, 8, 2,
4, 8, 8, 4, 8, 8, 2,
4, 8, 8, 4, 8, 8, 2
};
int melodyLength = sizeof(melody) / sizeof(melody[0]);
int buttonState = LOW;
int buzzerState = LOW ;
int dt = 300 ;
void setup() {
// put your setup code here, to run once:
//Start the serial monitor
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(buzzPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
//Read the current state of the button(HIGH or LOW)
buttonState = digitalRead(buttonPin);
Serial.print(" Button = ");
Serial.println(buttonState);
//Check if the button1 is currently pressed(LOW)
if(buttonState == LOW){
for(int i=0; i < melodyLength; i++){
int noteDurationMS = 1000 / noteDurations[i];
tone(buzzPin , melody[i] , noteDurationMS);
digitalWrite(ledPin , HIGH);
delay(noteDurationMS*1.3);
}
}else {
noTone(buzzPin);
digitalWrite(ledPin , LOW);
}
}
// push button 8,9
// led 10(pwm pin)
//buzzer 11
// if we use const then we can't change afterwards int he body too. it is reserved pin
// if we use #define then afterwards if we want we can change in the body.