I want when the rcwl 0516 detects movement the gsm 800l module makes a call to the number. The sensors are working properly but when I load the code the call is not made. The voltage is supplied accordingly.
t#include <SoftwareSerial.h>
// Define software serial port for SIM800L communication
SoftwareSerial gsmSerial(10, 11); // RX, TX (use appropriate pins for your setup)
// Pin connected to RCWL-0516 OUT pin
const int motionSensorPin = 2;
// Phone number to call
const String phoneNumber = "+995505052";
// Variable to keep track of motion detection state
bool motionDetected = false;
// Variable to keep track of the last motion detection time
unsigned long lastMotionTime = 0;
// Delay between motion detection and making a call (e.g., 5 seconds)
const unsigned long motionToCallDelay = 5000;
// Delay between calls (e.g., 30 seconds)
const unsigned long callDelay = 30000;
// Flag to indicate if a call is in progress
bool callInProgress = false;
void setup() {
// Initialize Serial for debugging
Serial.begin(9600);
// Initialize software serial for GSM module
gsmSerial.begin(9600);
// Set motion sensor pin as input
pinMode(motionSensorPin, INPUT);
Serial.println("Setup complete.");
}
void loop() {
// Check if motion is detected
if (digitalRead(motionSensorPin) == LOW) {
if (!motionDetected) {
Serial.println("Motion detected!");
motionDetected = true;
lastMotionTime = millis();
}
}
// Check if enough time has passed since motion was detected
if (motionDetected && (millis() - lastMotionTime >= motionToCallDelay) && !callInProgress) {
makePhoneCall();
motionDetected = false;
}
delay(100); // Short delay to avoid bouncing issues
}
void makePhoneCall() {
Serial.println("Dialing number...");
gsmSerial.println("ATD" + phoneNumber + ";");
// Increased delay to ensure the command is sent and processed
delay(5000);
// Read the GSM module response
while (gsmSerial.available()) {
char c = gsmSerial.read();
Serial.write(c);
}
Serial.println("In call...");
callInProgress = true;
delay(20000); // Adjust this delay based on the desired call duration
Serial.println("Hanging up...");
gsmSerial.println("ATH");
callInProgress = false;
}