Dear Arduino Forum members,
I hope this message finds you well. I am currently working on a project that involves both a PIR motion sensor and a soil moisture sensor. I have managed to write separate code for each sensor, but now I would like to combine the functionality of both sensors into a single code.
Below are the two separate codes I have written for the PIR motion sensor and the soil moisture sensor:
Code for PIR Motion Sensor:
cppCopy code
const int pirPin = 2; // PIR motion sensor output pin (change the pin number as per your wiring) const int lightSensorPin = A0; // Light sensor module analog input pin (change the pin number as per your wiring) const int ledPin = 12; // LED pin (built-in LED on most Arduino boards) int threshold = 500; // Adjust this value based on your light sensor module readings void setup() { pinMode(pirPin, INPUT); pinMode(ledPin, OUTPUT); digitalWrite(ledPin, LOW); // Turn off the LED initially Serial.begin(9600); } void loop() { int lightValue = analogRead(lightSensorPin); if (lightValue >= threshold) { // It is bright, PIR motion sensor should be active int motionDetected = digitalRead(pirPin); if (motionDetected == HIGH) { // Motion is detected during bright times triggerAlarm(); } } else { // It is dark, PIR motion sensor should be inactive digitalWrite(ledPin, LOW); // Turn off the LED } } void triggerAlarm() { // Blink the LED rapidly to indicate motion detected during bright times for (int i = 0; i < 5; i++) { digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); delay(100); } }
Code for Soil Moisture Sensor:
cppCopy code
const int soilMoisturePin = A0; // Soil moisture sensor connected to analog pin A0 const int lightSensorPin = A1; // Light sensor (photoresistor) connected to analog pin A1 const int moistureAlarmPin = 12; // Moisture alarm connected to digital pin 9 int soilMoistureThreshold = 500; // Adjust this threshold value as per your soil and sensor characteristics int lightThreshold = 500; // Adjust this threshold value to differentiate between bright and dark times int blinkDuration = 500; // Blink duration in milliseconds void setup() { pinMode(moistureAlarmPin, OUTPUT); digitalWrite(moistureAlarmPin, LOW); Serial.begin(9600); } void loop() { int soilMoisture = analogRead(soilMoisturePin); int lightLevel = analogRead(lightSensorPin); // Check if it's a bright time (assuming higher light level means it's bright) bool isBrightTime = lightLevel > lightThreshold; if (isBrightTime) { // Check if the soil is dry during bright times if (soilMoisture < soilMoistureThreshold) { // Trigger the blinking moisture alarm for (int i = 0; i < 3; i++) { // Blink the alarm 3 times digitalWrite(moistureAlarmPin, HIGH); delay(blinkDuration); digitalWrite(moistureAlarmPin, LOW); delay(blinkDuration); } Serial.println("Moisture Alarm: Soil is dry!"); } else { digitalWrite(moistureAlarmPin, LOW); } } else { // If it's not a bright time, the soil moisture sensor should be inactive digitalWrite(moistureAlarmPin, LOW); } delay(1000); // Adjust the delay time as needed for your application }
I would be very grateful if someone could help me merge these two codes into a single program that handles both the PIR motion sensor and the soil moisture sensor correctly. The desired behavior is as follows:
The PIR motion sensor should be active during dark times and trigger the motion alarm when motion is detected during dark times.
The PIR motion sensor should be inactive and not send signals during bright times.
The soil moisture sensor should be active during bright times and trigger the moisture alarm when the soil is dry during bright times.
The soil moisture sensor should be inactive and not send signals during dark times.
Thank you in advance for your assistance. If you need any additional information or clarification, please feel free to ask.
Best regards,Ananda