// Define the pins for the LEDs and sensors
const int ledPin1 = 9; // LED 1 is connected to digital pin 9
const int ledPin2 = 10; // LED 2 is connected to digital pin 10
const int potPin1 = A0; // Potentiometer 1 is connected to analog pin A0
const int potPin2 = A1; // Potentiometer 2 is connected to analog pin A1
const int pirPin1 = 2; // PIR Sensor 1 is connected to digital pin 2
const int pirPin2 = 3; // PIR Sensor 2 is connected to digital pin 3
bool motionDetected1 = false;
bool motionDetected2 = false;
int ledBrightness1 = 0;
int ledBrightness2 = 0;
int fadeIncrement = 5; // Adjust this value to control the fade speed
void setup() {
// Initialize the LED pins as outputs
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Initialize the PIR sensor pins as inputs
pinMode(pirPin1, INPUT);
pinMode(pirPin2, INPUT);
}
void loop() {
// Read the potentiometer values
int potValue1 = analogRead(potPin1);
int potValue2 = analogRead(potPin2);
// Map the potentiometer values to LED brightness (0-255)
ledBrightness1 = map(potValue1, 0, 1023, 0, 255);
ledBrightness2 = map(potValue2, 0, 1023, 0, 255);
// Read the PIR sensor values
int pirState1 = digitalRead(pirPin1);
int pirState2 = digitalRead(pirPin2);
// If motion is detected by PIR sensor 1 and LED 2 is not on, gradually fade on LED 1
if (pirState1 == HIGH && !motionDetected2) {
motionDetected1 = true;
motionDetected2 = false; // Ensure LED 2 is off
}
// If motion is detected by PIR sensor 2 and LED 1 is not on, gradually fade on LED 2
if (pirState2 == HIGH && !motionDetected1) {
motionDetected2 = true;
motionDetected1 = false; // Ensure LED 1 is off
}
// Gradually fade on LED 1
if (motionDetected1 && ledBrightness1 < 255) {
ledBrightness1 += fadeIncrement;
analogWrite(ledPin1, ledBrightness1);
delay(20); // Adjust this delay to control the fade speed
}
// Gradually fade on LED 2
if (motionDetected2 && ledBrightness2 < 255) {
ledBrightness2 += fadeIncrement;
analogWrite(ledPin2, ledBrightness2);
delay(20); // Adjust this delay to control the fade speed
}
// Check if motion is no longer detected and reset brightness
if (pirState1 == LOW) {
motionDetected1 = false;
ledBrightness1 = 0;
analogWrite(ledPin1, ledBrightness1);
}
if (pirState2 == LOW) {
motionDetected2 = false;
ledBrightness2 = 0;
analogWrite(ledPin2, ledBrightness2);
}
}