int LDR_pin = A0; // initializing pin 4 as the sensor output pin
int buzzer_pin = 3; // initializing pin 8 as the buzzer pin
int led_pin = 13; // initializing the pin 2 as the led pin
int LDRState = 0; // state of sensor
void setup()
{
pinMode(led_pin, OUTPUT); // declaring led pin as output pin
pinMode(LDR_pin, INPUT); // declaring sensor pin as input pin for Arduino
pinMode(buzzer_pin, OUTPUT); // declaring buzzer pin as output pin
Serial.begin(9600); // setting baud rate at 9600
}
void loop()
{
LDRState = analogRead(LDR_pin); // reading from the sensor
Serial.println(LDRState);
if (LDRState >= 8 && LDRState <= 900 ) // applying condition
{
Serial.println("It is dark now");
digitalWrite(led_pin, HIGH); // if state is high, then turn high the led
playMelody(); // play a melody on the buzzer
delay(1000);
}
else
{
Serial.println("It is still daytime");
digitalWrite(led_pin, LOW); // otherwise turn it low
// noTone(buzzer_pin);
delay(1000);// stop playing any tone
}
}
void playMelody()
{
// Play a simple melody: C4, E4, G4, C5
tone(buzzer_pin, 262, 200); // C4
delay(200);
tone(buzzer_pin, 330, 200); // E4
delay(200);
tone(buzzer_pin, 392, 200); // G4
delay(200);
tone(buzzer_pin, 523, 200); // C5
delay(200);
}