#include "Arduino.h"
// Define pin numbers
const int LDR_PIN = A0; // LDR analog pin connected to A0
const int SERVO_PIN = A1; // Servo signal pin connected to PA1
const int PIR_PIN = A2; // PIR motion sensor pin connected to PA2
const int RELAY_PIN = A3; // Relay pin connected to PA3
// Threshold for flame detection (adjust based on your testing)
const int LDR_THRESHOLD = 500; //
const int OFF_POSITION = 0; // PWM value for extreme right (off state)
const int ON_POSITION = 90; // PWM value for desired on position (example)
// Timer variables
unsigned long motionDetectedTime = 0;
const unsigned long LIGHT_OFF_DELAY = 10000; // Time to turn off light (10 seconds)
void setup() {
pinMode(SERVO_PIN, OUTPUT);
pinMode(PIR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
Serial.begin(9600); //
// Start the servo in the off position
analogWrite(SERVO_PIN, OFF_POSITION);
digitalWrite(RELAY_PIN, HIGH); // Turn off the relay initially (active LOW relay)
}
void loop() {
// Read the LDR value
int ldrValue = analogRead(LDR_PIN);
Serial.print("LDR Value: ");
Serial.println(ldrValue); // Print LDR value for debugging
// Check if flame (light) is detected
if (ldrValue > LDR_THRESHOLD) {
// Flame detected, do nothing
analogWrite(SERVO_PIN, OFF_POSITION); // Ensure servo stays in off position
} else {
// Flame not detected, move servo to the on position
analogWrite(SERVO_PIN, ON_POSITION); // Move to the desired position
}
// Check for motion
int motionState = digitalRead(PIR_PIN);
if (motionState == HIGH) {
// Motion detected, reset the timer and turn on the light
motionDetectedTime = millis();
digitalWrite(RELAY_PIN, LOW); // Turn on the light (active LOW)
Serial.println("Motion Detected: Light ON");
} else {
// No motion detected, check the timer
if (millis() - motionDetectedTime > LIGHT_OFF_DELAY) {
digitalWrite(RELAY_PIN, HIGH); // Turn off the light (active LOW)
Serial.println("No Motion: Light OFF");
}
}
// Control the LED based on the relay state
if (digitalRead(RELAY_PIN) == LOW) {
// Relay is on (light is on)
digitalWrite(LED_BUILTIN, HIGH); // Turn LED on
} else {
// Relay is off (light is off)
digitalWrite(LED_BUILTIN, LOW); // Turn LED off
}
// Delay for stability
delay(100);
}
Loading
stm32-bluepill
stm32-bluepill