Send SMS, when a letter drops by!

Hello!

I am truly sorry, if this has been asked before, but I am in need of help for a project at school!

I want to do the following:

When a physical letter drops by my PIR motion sensor, it sends an sms to my phone.

I have the following:

Arduino Uno board
Parallax PIR Sensor (Rev B) - http://learn.parallax.com/KickStart/555-28027
Arduino GSM Shield
Working SIM Card.

I tested all above with example codes, to check it's working.

The PIR sensor can give me an output of : ID = 0 or 1. 1, when something is in front of it, and 0 when nothing is blocking it (no movement).

The problem persists mainly of me writing a code. I have absolutely no idea if, what I am doing to be honest, I have only managed to get the example codes to work, and I have altered some values in it etc.

I don't want an SMS-flood, just because the PIR sensor gives a reading of 1's for a time.

How would I solve this.

I am in desperate need of help here - just to get me started on it!

Thank you so much in advance!

Best regards!

  • Thomas

/*
Snail Mail Detector v1.0

Initializes PIR Sensor and waits for it to calibrate (30 secs)
Checks pin 4 (pirPin) to detect state change when Digital PIR Sensor detects motion.
Writes to Serial Port the Motion Detected Alert message

*/
//Libraries
#include GSM.h

//the time we give the sensor to calibrate (10-60 secs according to the datasheet)
int calibrationTime = 30; // 30 seconds for Parallax PIR
int pirPin = 4; // Pir sensor output is connected to Pin 4
int pirState = HIGH; // initialize PIR Sensor state to HIGH
int val = 0;

void setup() {
// initialize serial port:
Serial.begin(9600);
pinMode(pirPin, INPUT);

// give sensor time to calibrate
Serial.print(“calibrating sensor “);
for (int i = 0; i < calibrationTime; i++) {
Serial.print(”.”);
delay(1000);
}

Serial.println(" done");
Serial.println(“SENSOR ACTIVE”);
delay(30);

}

void loop() {

// read Arduino pin to detect change in logic state
// When motion is detected, logic state should be LOW
// and HIGH when there is no motion detected
val = digitalRead(pirPin);

if (val == LOW) {
if (pirState == HIGH) {
// we have just turned on
Serial.println(“Motion ended!”);
// We only want to print on the output change, not
// state
pirState = LOW;
}
}
else {
if (pirState == LOW) {
Serial.println(“Motion detected!”);
pirState = HIGH;

}
}
}

Thats the code I use so far. I need to implement it to send an sms somehow, when motion has been detected, but NOT flood my phone with texts.

For instance - if more letters drop in the mailbox in a timeframe of perhaps 2 minutes it only has to detect it once, and send one sms!

Just off the top of my head I have used code similar to the below to achieve this. It may not be the best way to do it but it works for me.

//All this goes at the top with global variable declarations
long prevTime = millis(); //variable to hold the time the code last ran
long timeDelay = 120000; //time in milliseconds

//This goes into the loop code around the code you want to run at timed delay

if (millis() - prevTime >= timeDelay) {

//This is the code which will run if time since last run is more than time delay
//i.e. send a text message...

//Reset the prevTime to now.
prevTime = millis();
}

Sorry for any errors, I don't have Arduino IDE on this PC to verify it.