HI guys!
I'm using SIM900 , an arduino mega 2560, and a PIR sensor. I want my arduino to send SMS alerts everytime the PIR sensor detects motion. I already know how to send SMS, and I already knew how PIR sensor works with my mega2560. However I dont know how they work together.
this is the code for sending SMS and it works fine:
void setup()
{
SIM900.begin(19200);
SIM900power();
delay(20000); // give time to log on to network.
}
void SIM900power()
// software equivalent of pressing the GSM shield "power" button
{
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(5000);
}
void sendSMS()
{
SIM900.print("AT+CMGF=1\r"); // AT command to send SMS message
delay(1000);
SIM900.println("AT + CMGS = "+639368266683""); // recipient's mobile number, in international format
delay(1000);
SIM900.println("Hello, world. This is a text message from an Arduino Mega 2560."); // message to send
delay(1000);
SIM900.println((char)26); // End AT command with a ^Z, ASCII code 26
delay(1000);
SIM900.println();
delay(5000); // give module time to send SMS
SIM900power(); // turn off module
}
while this is the code for testing my PIR sensor:
/*
PIR sensor tester
*/
int ledPin = 13; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for PIR sensor)
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
void setup() {
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare sensor as input
Serial.begin(9600);
}
void loop(){
val = digitalRead(inputPin); // read input value
if (val == HIGH) { // check if the input is HIGH
digitalWrite(ledPin, HIGH); // turn LED ON
if (pirState == LOW) {
// we have just turned on
Serial.println("Motion detected!");
// We only want to print on the output change, not state
pirState = HIGH;
}
} else {
digitalWrite(ledPin, LOW); // turn LED OFF
if (pirState == HIGH){
// we have just turned of
Serial.println("Motion ended!");
// We only want to print on the output change, not state
pirState = LOW;
}
}
}
Copy all your definitions, etc, and put the contents of setup() into one, copy all your routines into the sketch and use the loop() from the PIR sketch instead of the SMS one. Then change the line as I suggested. There are several posts and articles explaining how to combine two sketches, do a search if you need a more detailed explanation.