Subject: Combining PIR Motion Sensor and Soil Moisture Sensor Code

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

I moved your topic to an appropriate forum category @ananada1988.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

i @ananada1988
welcome to the arduino-forum.

No I'm sorry to say it does yet not well.
You posted your code as a fluid text which is very hard to read.

You should have taken the time to read the

how to get the best out of this forum -tutorial

that you received as a welcome-message.

To re-edit your posting follow this tutorial.

scroll down to re-edit a posting

best regards Stefan

Hello ananada1988

Welcome to the worldbest forum.

What is the task of the programme in real life?

We need some additional information.
Post your sketch, well formated, with well-tempered comments and in so called
code tags "< code >" and a detailed circuit diagram to see how we can help.

Have a nice day and enjoy coding in C++.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.