const int ldrPin = 2; // Broche du capteur LDR
const int ledPin = 4; // Broche de la LED (utilisation d'une broche PWM pour contrôler la luminosité)
const int pirPin = 5; // Broche du capteur PIR
int pirState = LOW;
int lastPirState = LOW;
void setup() {
Serial.begin(115200);
Serial.println("LDR readings...");
pinMode(ldrPin, INPUT);
pinMode(ledPin, OUTPUT); // Déclaration de la broche de la LED
}
void loop() {
delay(10);
pirState = digitalRead(pirPin);
// Check for changes in the PIR sensor state
if (pirState != lastPirState) {
if (pirState == HIGH) {
// Motion detected
Serial.println("Motion detected!");
// Increase LED brightness when motion is detected
for (int i = 0; i <= 255; i++) {
analogWrite(ledPin, i);
delay(10);
}
} else {
// Motion not detected
Serial.println("No motion detected.");
// Decrease LED brightness when motion is not detected
for (int i = 255; i >= 0; i--) {
analogWrite(ledPin, i);
delay(10);
}
}
lastPirState = pirState;
}
int ldrValue = analogRead(ldrPin);
Serial.println(ldrValue);
// Control the brightness of the LED based on the LDR reading
int brightness = map(ldrValue, 0, 1023, 0, 255);
analogWrite(ledPin, brightness);
delay(1000); // This delay speeds up the simulation
}