const int photoResistorPin = A0; // Photoresistor pin
const int buzzerPin = 9; // Passive buzzer pin
const int buttonPin = 10; // Button pin
const int redPin = 6; // Red LED pin
const int greenPin = 3; // Green LED pin
const int bluePin = 5; // Blue LED pin
const int dt = 100;
int photoValue = 0; // Analog value read from the photoresistor
// Define variables for the analog values from the RGB LED channels
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
// Define variables for the frequency and duration of the tone played by the buzzer
int toneFrequency = 0;
int toneDuration = 0;
bool stopSound = false; // Flag variable to stop the sound
bool stopLight = false; // Flag variable to stop the light
void setup() {
pinMode(photoResistorPin, INPUT); // Set the photoresistor pin as input
pinMode(buzzerPin, OUTPUT); // Set the buzzer pin as output
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input with pull-up resistor
pinMode(redPin, OUTPUT); // Set the red LED pin as output
pinMode(greenPin, OUTPUT); // Set the green LED pin as output
pinMode(bluePin, OUTPUT); // Set the blue LED pin as output
Serial.begin(9600); // Initialize serial communication
}
void loop() {
// Read the analog value from the photoresistor
photoValue = analogRead(photoResistorPin);
// Map the photoresistor reading to RGB values between 0 and 255
redValue = map(photoValue, 0, 1023, 0, 255);
greenValue = map(photoValue, 0, 1023, 0, 255);
blueValue = map(photoValue, 0, 1023, 0, 255);
// Map the photoresistor reading to a frequency between 200 and 2000 Hz
toneFrequency = map(photoValue, 0, 1023, 200, 2000);
// Check if the button is pressed to stop the sound and light
if (digitalRead(buttonPin) == LOW) {
if(stopSound || stopLight) {
stopSound = false; // Reset the flag variable to allow sound to start
stopLight = false; // Reset the flag variable to allow light to start
} else {
stopSound = true; // Set the flag variable to stop the sound
stopLight = true; // Set the flag variable to stop the light
}
}
// Adjust the values of the RGB LED channels based on the mapped brightness value
int brightness = redValue;
// Adjust the frequency and duration of the tone played by the buzzer based on the mapped brightness value
toneFrequency = map(brightness, 0, 255, 200, 2000);
toneDuration = map(brightness, 0, 255, 50, 500);
// Generate the tone if the flag variable is not set
if (!stopSound) {
tone(buzzerPin, toneFrequency, toneDuration); // Generate the tone
} else {
noTone(buzzerPin); // Stop the tone
}
// Generate the light if the flag variable is not set
if (!stopLight) {
// Control the light based on the lightOn variable
analogWrite(redPin, redValue); // Set the red channel of the RGB LED
analogWrite(greenPin, greenValue); // Set the green channel of the RGB LED
analogWrite(bluePin, blueValue); // Set the blue channel of the RGB LED
} else {
digitalWrite(redPin, LOW); // Turn off the red LED
digitalWrite(greenPin, LOW); // Turn off the green LED
digitalWrite(bluePin, LOW); // Turn off the blue LED
}
// Print the photoresistor reading and frequency to the serial monitor
Serial.print("Photoresistor reading: ");
Serial.print(photoValue);
Serial.print(", LDR Value: ");
Serial.print(photoValue);
Serial.print(", Red Value: ");
Serial.print(redValue);
Serial.print(", Green Value: ");
Serial.print(greenValue);
Serial.print(", Blue Value: ");
Serial.print(blueValue);
Serial.print(", Tone Frequency: ");
Serial.print(toneFrequency);
Serial.print(", Tone Duration: ");
Serial.print(toneDuration);
Serial.print(", Frequency: ");
Serial.println(toneFrequency);
delay(dt); // Wait for 100 ms before reading again
}