PaulS:
Of course there is. If the current state of the switch doesn't match the previous state of the switch, a transition occurred. The transition was either a RISING edge, if the current state is HIGH, or a FALLING edge, if the current state is LOW. So, all you need to do is keep track of the previous state, by adding a new variable and assigning it a value at the end of loop. That value is the current state of the pin.
Today I tried to modified the code using your logic, I assigned a variable called "doorlock"
In the initial setup I stated declared a variablevolatile int doorlock;
In the void setup() I have declared
pinMode(2, INPUT); //Door sensor monitor
digitalWrite (2, LOW); // internal pull-up resistor
//When the Input goes from High to Low it will trigger a function called "doormonitor"
doorlock = 0;
Finally in the main loop I added that the doorlock=1 after the relay_b is on
if( sms.indexOf("onb") >= 0 ){
digitalWrite( relay_b, HIGH );
doorlock=1;
}
I have also added a code that says...
if(digitalRead(2)==HIGH && doorlock==1)
{digitalWrite(13,HIGH);
delay (1000);
doorlock=0;
}
else
digitalWrite(13, LOW);
OUTPUT
After sending the "onb" command pin 13 glows only once & then goes off. Nothing happen if I short pin 2 with GND or VCC.
Expected Output
After the doorlock relay is high, the pin 2 will sense state change ONLY ONCE until the relay is activated again.
I guess I am missing something in my logic. Will help me find the error in the logic of the code.?
The full code I am working on is this...
#include <SoftwareSerial.h>
// EN: String buffer for the GPRS shield message
String msg = String("");
// EN: Set to 1 when the next GPRS shield message will contains the SMS message
int SmsContentFlag = 0;
//control pins of relay.
int relay_a=4;
int relay_b=5;
int relay_c=6;
int relay_d=7;
int led = 13; // Interrupt monitor
//int door = 12; // Door Status monitor
int button=2; // Door Sensor monitor
volatile int doorlock;
// Interrupt Service Routine (ISR)
void setup()
{
Serial.begin(19200); // the GPRS baud rate
// Initialize PINs
pinMode( 4, OUTPUT );
pinMode( 5, OUTPUT );
pinMode( 6, OUTPUT );
pinMode( 7, OUTPUT );
digitalWrite( 4, LOW );
digitalWrite( 5, LOW );
digitalWrite( 6, LOW );
digitalWrite( 7, LOW );
Serial.println( "AT+CMGF=1" );
delay(2000);
Serial.print( "AT+CMGDA=" );
Serial.write(34);
Serial.print("DEL ALL");
Serial.write(34);
Serial.write(13);
Serial.write(10);
delay(6000);
Serial.println( "Let's Test..." );
pinMode(13, OUTPUT); // Interrupt monitor
//pinMode(12, INPUT); // Door status monitor
pinMode(2, INPUT); //Door sensor monitor
digitalWrite (2, LOW); // internal pull-up resistor
//When the Input goes from High to Low it will trigger a function called "doormonitor"
doorlock = 0;
}
void loop()
{
{ if(digitalRead(2)==HIGH && doorlock==1)
{digitalWrite(13,HIGH);
delay (1000);
doorlock=0;
}
else
digitalWrite(13, LOW);
char SerialInByte;
if(Serial.available())
{
SerialInByte = (unsigned char)Serial.read();
delay(5);
if( SerialInByte == 13 ){
// EN: Store the char into the message buffer
ProcessGprsMsg();
}
if( SerialInByte == 10 ){
// EN: Skip Line feed
}
else {
// EN: store the current character in the message string buffer
msg += String(SerialInByte);
}
}
}
void ProcessSms( String sms ){
if( sms.indexOf("ona") >= 0 ){
digitalWrite( relay_a, HIGH );
}
if( sms.indexOf("onb") >= 0 ){
digitalWrite( relay_b, HIGH );
doorlock=1;
}
if( sms.indexOf("onc") >= 0 ){
digitalWrite( relay_c, HIGH );
}
if( sms.indexOf("ond") >= 0 ){
digitalWrite( relay_d, HIGH );
}
if( sms.indexOf("offa") >= 0 ){
digitalWrite( relay_a, LOW );
}
if( sms.indexOf("offb") >= 0 ){
digitalWrite( relay_b, LOW );
}
if( sms.indexOf("offc") >= 0 ){
digitalWrite( relay_c, LOW );
}
if( sms.indexOf("offd") >= 0 ){
digitalWrite( relay_d, LOW );
}
}
// EN: Request Text Mode for SMS messaging
void GprsTextModeSMS(){
Serial.println( "AT+CMGF=1" );
}
void GprsReadSmsStore( String SmsStorePos ){
Serial.print( "AT+CMGR=" );
Serial.println( SmsStorePos );
}
// EN: Clear the GPRS shield message buffer
void ClearGprsMsg(){
msg = "";
}
// EN: interpret the GPRS shield message and act appropiately
void ProcessGprsMsg() {
if( msg.indexOf( "Call Ready" ) >= 0 ){
// Serial.println( "*** GPRS Shield registered on Mobile Network ***" );
GprsTextModeSMS();
}
// EN: unsolicited message received when getting a SMS message
if( msg.indexOf( "+CMTI" ) >= 0 ){
int iPos = msg.indexOf( "," );
String SmsStorePos = msg.substring( iPos+1 );
GprsReadSmsStore( SmsStorePos );
}
if( msg.indexOf( "+CMGR:" ) >= 0 ){
// EN: Next message will contains the BODY of SMS
SmsContentFlag = 1;
// EN: Following lines are essentiel to not clear the flag!
ClearGprsMsg();
return;
}
// EN: +CMGR message just before indicate that the following GRPS Shield message
// (this message) will contains the SMS body
if( SmsContentFlag == 1 ){
ProcessSms( msg );
}
ClearGprsMsg();
// EN: Always clear the flag
SmsContentFlag = 0;
}
Any suggestions?