Pages: 1 [2] 3 4   Go Down
Author Topic: interrupts with conditions  (Read 2199 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 148
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <avr/interrupt.h>
volatile int doorlock;

void setup(){
pinMode(2,INPUT);
digitalWrite(2,HIGH);
attachInterrupt(0, doordoingsomething, FALLING);  // or attachInterrupt(0, doordoingsomething, RISING); or attachInterrupt(0, doordoingsomething, CHANGE);
doorlock = 0
// other setup
interrupts();  // starts the int's
}
void loop(){
if (doorlock == 1){  // if this = 1 then int has fired
alarmneedstohappen();
}
// normal loop stuff
}

void doordoingsomething();
doorlock=1; // sets flag to 1 so loop if catches it
}

void alarmneedstohappen();{
// we are here because int flag happened.
doorlock = 0 // reset int flag
// perform other alarm stuff of whatever
}
« Last Edit: March 20, 2013, 08:15:25 pm by grendle » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4768
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have modified the code & hardware too. digital pin 5(relay control, OUTPUT PIN) & digital pin2 (door lock monitor, INPUT) will be shorted so that if the relay is ON the door lock monitor is also on. Now the problem arises I have added the door monitor logic into the void loop() function. Two functio will be in the loop() function.

if pin2 is HIGH> check for the status of door sensor> if door sensor detects any change, switch on the buzzer/led

check for new sms> process sms> actuate the relays

Code:
#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 dlm=2; // door lock monitor
int door_sensor=3;  // door sensor
int led=13;
// EN: Code PIN of the SIM card (if applied)
//String SIM_PIN_CODE = String( "XXXX" );
 
void setup()
{
  Serial.begin(19200);                 // the GPRS baud rate
  // Initialize  PINs
  pinMode( 4, OUTPUT );
  pinMode( 5, OUTPUT );
  pinMode( 6, OUTPUT );
  pinMode( 7, OUTPUT );
  pinMode( 2, INPUT );
  pinMode( 3,INPUT );
  pinMode( 13, OUTPUT );
  digitalWrite( 4, LOW );
  digitalWrite( 5, LOW );
  digitalWrite( 6, LOW );
  digitalWrite( 7, LOW );
  Serial.println( "AT+CMGF=1" );
  delay(200);
}
 
void loop()
{
 {
  if (digitalRead(2,HIGH))
  {
     if (digitalRead(3,HIGH)
     {
       (digitalWrite(13,HIGH)
     }
  }
     else{
   }
 }
 
    char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
       
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------
       // EN: If the message ends with <CR> then process the message
        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);
         }
     }   
}
// EN: Make action based on the content of the SMS.
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
 
  if( sms.indexOf("ona") >= 0 ){
    digitalWrite( relay_a, HIGH );
  }
   if( sms.indexOf("onb") >= 0 ){
    digitalWrite(  relay_b, HIGH );
  }
   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 ){
   //  Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
   //  Serial.print( "SMS stored at " );
  //   Serial.println( SmsStorePos );     
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
 
  // EN: SMS store readed through UART (result of GprsReadSmsStore request) 
  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 ){
 //   Serial.println( "*** SMS MESSAGE CONTENT ***" );
 //   Serial.println( msg );
 //   Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
 
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0;
}


In this code the void loop shows error.
Code:
C:\Users\User\Desktop\arduino-1.5.2\hardware\arduino\avr\cores\arduino/Arduino.h: In function 'void loop()':
C:\Users\User\Desktop\arduino-1.5.2\hardware\arduino\avr\cores\arduino/Arduino.h:102: error: too many arguments to function 'int digitalRead(uint8_t)'
Using_SMS_to_Control_Relay_Hardware_Serial:41: error: at this point in file
C:\Users\User\Desktop\arduino-1.5.2\hardware\arduino\avr\cores\arduino/Arduino.h:102: error: too many arguments to function 'int digitalRead(uint8_t)'
Using_SMS_to_Control_Relay_Hardware_Serial:43: error: at this point in file
Using_SMS_to_Control_Relay_Hardware_Serial:44: error: expected `)' before '{' token
Using_SMS_to_Control_Relay_Hardware_Serial:47: error: expected primary-expression before '}' token
Using_SMS_to_Control_Relay_Hardware_Serial:47: error: expected `;' before '}' token

Actually I want yo attach an interrupt function that can ONLY be activated when the door lock monitor (pin 2) is high & the door sensor detects a change (preferably Falling trigger).



From what I can see you want to add increasing complexity to get around your shortcomings in understanding code. It is like spinning the wheels in mud, you only get stuck deeper.

Read about if-else and switch-case and practice on simple examples. Then find a clear explanation on finite state machines. Proper tools and the understanding of how to use them will give you a clear view of far easier ways.

You do not need interrupts for something so slow as button press. If your code does not prevent itself from being responsive, 1 millisecond to Arduino is like whole minutes to you.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

http://gammon.com.au/interrupts
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 109
Posts: 4078
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  if (digitalRead(2,HIGH))
do you mean
Code:
  if (digitalRead(2) == HIGH)
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Actually I want yo attach an interrupt function that can ONLY be activated when the door lock monitor (pin 2) is high & the door sensor detects a change (preferably Falling trigger).
Why do you insist on using an interrupt? I don't see why you need an interrupt.

In any case, you can attach the interrupt handler when the pin goes high (NOT IS high), and detach it when the pin goes low (NOT IS low).

Code:
  if (digitalRead(2,HIGH))
Have you seen, anywhere, any one else try to misuse digitalRead() this way?

The digitalRead() function takes ONE argument, the pin to read from. It returns a value - HIGH or LOW. You have to compare the return value to HIGH or LOW:

Code:
  if (digitalRead(2) == HIGH)

Code:
     if (digitalRead(3,HIGH)
The correct ratio of close parens to open parens is 1 to 1. Any other ratio is WRONG!

Code:
       (digitalWrite(13,HIGH)
Why is there a ( at the beginning of this statement? Why is there NOT a ; at the end?
Logged

Offline Offline
Sr. Member
****
Karma: 2
Posts: 344
YES! It is possible. Even Impossible Says I m Possible. :)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Thanks Mr. Nick for the excellent tutorial...
I have written an ISR for my code.
Code:
void doormonitor ()
{
  if (digitalRead (12) == HIGH && digitalRead (2)==HIGH )
   digitalWrite (led, HIGH);
  else
    digitalWrite (led, LOW);
}  // end of pinChange

In the main program, if I send "onb" to the microcontroller the door lock relay (pin5) is on. Now I have shorted the pin5 with door status monitor(pin 12) to check if the door is locked or not. Because I cant found any other process to check if an OUTPUT PIN (pin5) in high or not.
I have connected the door sensor with digital pin 2

Now, I want that if the door status monitor is HIGH && DOOR SENSOR is HIGH ONLY THEN the pin 13 will respond (In the PIN13 part I will add a program to send sms to my cell phone later.) The sms sending code is here...
Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,10);
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;   
int buttonState = 0;   
void setup()
{
mySerial.begin(9600); // the GPRS baud rate
delay(2000);
mySerial.println("AT");
delay(2000);
mySerial.println("AT+CSMP=17,167,0,0");
delay(2000);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   

}
void loop()
{
    // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) { 
  digitalWrite(ledPin, HIGH);
  delay(100);
mySerial.println("AT+CMGF=1"); // set the SMS mode to text
delay(2500);
mySerial.write("AT+CMGS=");
mySerial.write(34); //ASCII of “
mySerial.write("+123456...");
mySerial.write(34);
mySerial.write(13);
mySerial.write(10);
delay(2500);
mySerial.println("Yahoo, You are Successful...!"); //this is the message to be sent
delay(2500);
mySerial.write(26);
mySerial.write(13);
mySerial.write(10);//Ascii code of ctrl+z to send the message
}

  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}
sms sending not an issue ri8 now. I will do it accordingly. But the issue is in the interrupt

1. the internal pull-down resistor is not working properly for pin 2. after the first interrupt the pin2 sees ALWAYS HIGH. It means the LED is always glowing except when I connect pin2 with ground pin, It is happening. And it also means that the SMS sending code will execute over & over again just for one RISING edge detection, thats what I dont want.

2. I still cannot get when & why to use volatile int() function in the in terrupt routine. Has is something to do with the led state.?

Quote
Why do you insist on using an interrupt? I don't see why you need an interrupt.

I want to use interrupts for one pulse per RISING edge. suppose, If the door lock is activated & someone break the door (door sensor HIGH), arduino will sense that & send sms continuously until the door is closed if I put it in the Void loop() function I suppose. Thats the reason I want to use interrupt for. Only one pulse per RISING edge detection.
Kindly mention if there is any alternate way of doing that.

My full code is here
Code:
#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

 // Interrupt Service Routine (ISR)
void doormonitor ()
{
  if (digitalRead (12) == HIGH && digitalRead (2)==HIGH )
   digitalWrite (led, HIGH);
  else
    digitalWrite (led, LOW);
}  // end of pinChange
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(200);
 pinMode(13, OUTPUT); // Interrupt monitor
 pinMode(12, INPUT); // Door status monitor
 pinMode(2, INPUT); //Door sensor monitor
 digitalWrite (2, LOW);  // internal pull-up resistor
 attachInterrupt(0, doormonitor, RISING); //Interrupt 0 is Digital Pin 2
  //When the Input goes from High to Low it will trigger a function called "doormonitor"
}
 
void loop()
{
  char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
       
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------
       // EN: If the message ends with <CR> then process the message
        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);
         }
     }   
}
// EN: Make action based on the content of the SMS.
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
 
  if( sms.indexOf("ona") >= 0 ){
    digitalWrite( relay_a, HIGH );
  }
   if( sms.indexOf("onb") >= 0 ){
    digitalWrite(  relay_b, HIGH );
  }
   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 ){
   //  Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
   //  Serial.print( "SMS stored at " );
  //   Serial.println( SmsStorePos );     
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
 
  // EN: SMS store readed through UART (result of GprsReadSmsStore request) 
  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 ){
 //   Serial.println( "*** SMS MESSAGE CONTENT ***" );
 //   Serial.println( msg );
 //   Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
 
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0;
}

Any suggestions/ comments for this issue? kindly correct me if I am doing wrong in the logic or coding.
Thanks for all your generous support.
« Last Edit: March 22, 2013, 04:36:34 pm by nightcrawler218 » Logged

"Imagination is more important than knowledge."
-Einstein

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to use interrupts for one pulse per RISING edge. suppose, If the door lock is activated & someone break the door (door sensor HIGH), arduino will sense that & send sms continuously until the door is closed if I put it in the Void loop() function I suppose. Thats the reason I want to use interrupt for. Only one pulse per RISING edge detection.
Kindly mention if there is any alternate way of doing that.
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.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4768
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now I want that if the door lock is activated & the sensor value is changed pin13 will glow. Should I need interrupts for that..? If so then how to program the interrupt ONLY IF the relay1 is activated..?

Your program that activates/deactivates the relay should know IF the relay1 is activated.

How far must the door move before the door magnet sensor switches state?  That can be checked more than 1,000 times a second and easily still have time to take care of other things without interrupts.

Quote
Actually I want yo attach an interrupt function that can ONLY be activated when the door lock monitor (pin 2) is high & the door sensor detects a change (preferably Falling trigger).

If the lock is activated and the door has been opened or tried to be opened?

Quote
In the main program, if I send "onb" to the microcontroller the door lock relay (pin5) is on. Now I have shorted the pin5 with door status monitor(pin 12) to check if the door is locked or not. Because I cant found any other process to check if an OUTPUT PIN (pin5) in high or not.
I have connected the door sensor with digital pin 2

You read pin 12 to see if pin 5 is LOW or HIGH? Your code changes pin 5. Same code can set a variable to say "pin 5 is LOW" or "pin 5 is HIGH".
Do you need to watch your feet to know that you are walking? It would be just as silly.

If you are worried about someone bashing or rattling the door then attach a piezo disk (bare disk used for audio pickup) to the door as used by Door Knock Sensor code to detect that. Put pickups on the door frame while you're at it, if the wall is attacked to get to the lock mechanism you will know that too.
All those are sensors. None of those are the relay pin. Watch the sensors, keep track of the pin through the code that changes it. If you need to know if the bolt is engaged then put a contact or sensor inside the bolt hole.

If the door opens even 1mm and the lock is supposed to be engaged (pin 5) then you want an alarm to go off. The timing on that can be 10 milliseconds and -still- more than 10X faster than you need.

Broken is broken. Alarm going off is for humans that 1/10th second is more than fast enough.

BTW, if you plan on using radio or RFID for a lock key then sophisticated thieves will be able to read your entry code without you knowing. Lots of expensive cars are stolen that way.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Sr. Member
****
Karma: 2
Posts: 344
YES! It is possible. Even Impossible Says I m Possible. :)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Paul & GFS for your support...

You got it righ8 GFS, I am trying to implement the code as a car security systems. The project will include GPS+GSM+ARDUINO. Right now I am trying to resolve the GSM part.

I have completed the following parts till now.

How to trigger the arduino to send an sms or call

For sms
Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,10);
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;   
int buttonState = 0;   
void setup()
{
mySerial.begin(9600); // the GPRS baud rate
delay(2000);
mySerial.println("AT");
delay(2000);
mySerial.println("AT+CSMP=17,167,0,0");
delay(2000);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   

}
void loop()
{
    // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) { 
  digitalWrite(ledPin, HIGH);
  delay(100);
mySerial.println("AT+CMGF=1"); // set the SMS mode to text
delay(2500);
mySerial.write("AT+CMGS=");
mySerial.write(34); //ASCII of “
mySerial.write("+123456");
mySerial.write(34); //ASCII of "
mySerial.write(13); // ASCII of Enter
mySerial.write(10); //ASCII of \n
delay(2500);
mySerial.println("Yahoo, You are Successful...!"); //this is the message to be sent
delay(2500);
mySerial.write(26);
mySerial.write(13);
mySerial.write(10);//Ascii code of ctrl+z to send the message
}

  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

For Call
Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,10);
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;   
int buttonState = 0;   
void setup()
{
mySerial.begin(9600); // the GPRS baud rate
delay(2000);
mySerial.println("AT");
delay(2000);

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   

}
void loop()
{
    // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) { 
  digitalWrite(ledPin, HIGH);
  delay(1000);
mySerial.write("ATD +123456...;");
mySerial.write(13);
mySerial.write(10);
}

  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

& finally the main Example Code What I am trying to modify...
Code:
#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;

// EN: Code PIN of the SIM card (if applied)
//String SIM_PIN_CODE = String( "XXXX" );
 
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(200);
}
 
void loop()
{
    char SerialInByte;
    if(Serial.available())
    {       
        SerialInByte = (unsigned char)Serial.read();
       delay(5);
       
        // -------------------------------------------------------------------
        // EN: Program also listen to the GPRS shield message.
        // -------------------------------------------------------------------
       // EN: If the message ends with <CR> then process the message
        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);
         }
     }   
}
// EN: Make action based on the content of the SMS.
//     Notice than SMS content is the result of the processing of several GPRS shield messages.
void ProcessSms( String sms ){
 
  if( sms.indexOf("ona") >= 0 ){
    digitalWrite( relay_a, HIGH );
  }
   if( sms.indexOf("onb") >= 0 ){
    digitalWrite(  relay_b, HIGH );
  }
   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 ){
   //  Serial.println( "*** SMS Received ***" );
     // EN: Look for the coma in the full message (+CMTI: "SM",6)
     //     In the sample, the SMS is stored at position 6
     int iPos = msg.indexOf( "," );
     String SmsStorePos = msg.substring( iPos+1 );
   //  Serial.print( "SMS stored at " );
  //   Serial.println( SmsStorePos );     
     // EN: Ask to read the SMS store
     GprsReadSmsStore( SmsStorePos );
  }
 
  // EN: SMS store readed through UART (result of GprsReadSmsStore request) 
  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 ){
 //   Serial.println( "*** SMS MESSAGE CONTENT ***" );
 //   Serial.println( msg );
 //   Serial.println( "*** END OF SMS MESSAGE ***" );
    ProcessSms( msg );
  }
 
  ClearGprsMsg();
  // EN: Always clear the flag
  SmsContentFlag = 0;
}


Just because the main code doesn't have instructions to remove the ENTIRE SMS memory, I have to add DELETE SMS command in the void loop(). The code is...
Code:
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..." );

DEL SMS command issue is resolved today, I was doing wrong with the delay to communicate with the GSM module.Suddenly the delay issue stuck on my mind & I found the solution. This is my current progress till now.

The GPS monitoring will be done after the GSM part is completely done, now I am trying to alter & mix the codes with the "door breaking" warning (led/ sms sending acknowledgement) which is the final part of the GSM+ARDUINO.

What I am trying to do is

1. send "command" to lock the door (relay 1 high/activated)
2. set a magnetic sensor as door position sensor
3. Check If the door sensor sees the door position is opened while the door is locked(relay is high). If door broken or opened, arduino will send SMS to my cell phone that someone is trying to steal my car.

Only these 3 steps are giving me pain right now. There are additional 3 relays to control the engine, brakes, Ignition control valve of the car. they are working fine. trying to find out how to implement the step 1,2& 3 in my main code right now.

Thanks for all your support guys...
« Last Edit: March 22, 2013, 04:38:52 pm by nightcrawler218 » Logged

"Imagination is more important than knowledge."
-Einstein

Offline Offline
Sr. Member
****
Karma: 2
Posts: 344
YES! It is possible. Even Impossible Says I m Possible. :)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <avr/interrupt.h>
volatile int doorlock;

void setup(){
pinMode(2,INPUT);
digitalWrite(2,HIGH);
attachInterrupt(0, doordoingsomething, FALLING);  // or attachInterrupt(0, doordoingsomething, RISING); or attachInterrupt(0, doordoingsomething, CHANGE);
doorlock = 0
// other setup
interrupts();  // starts the int's
}
void loop(){
if (doorlock == 1){  // if this = 1 then int has fired
alarmneedstohappen();
}
// normal loop stuff
}

void doordoingsomething();
doorlock=1; // sets flag to 1 so loop if catches it
}

void alarmneedstohappen();{
// we are here because int flag happened.
doorlock = 0 // reset int flag
// perform other alarm stuff of whatever
}

Thanks grandle for the code...
Logged

"Imagination is more important than knowledge."
-Einstein

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4768
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought house door. With cars there's more you can do, like tilt sensor (accelerometer) and ultrasonic which can also pick up on window smashing. Buuut the tilt sensor requires work to not keep alarming in wind gusts or when trucks pass by.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Sr. Member
****
Karma: 2
Posts: 344
YES! It is possible. Even Impossible Says I m Possible. :)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, you are right GFS... I was thinking of adding the accelerometer & a PIR sensor in this project. I shall update my status tomorrow regarding the project. Thanks again to all, for your constant support.
Logged

"Imagination is more important than knowledge."
-Einstein

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 96
Posts: 4768
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Best to get 1.0 done working before adding!

With the extras there will be ways they can trigger false alarms you will need to test and either set thresholds or work around, perhaps look to more than 1 sensor for a positive.

With ultrasonics, I don't mean range finders. There is a type of ultrasonic system that sets up a wave pattern of a closed space and looks for change to that pattern. It's been in use since at least 1966 for store security.



Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Sr. Member
****
Karma: 2
Posts: 344
YES! It is possible. Even Impossible Says I m Possible. :)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#include <avr/interrupt.h>
volatile int doorlock;

void setup(){
pinMode(2,INPUT);
digitalWrite(2,HIGH);
attachInterrupt(0, doordoingsomething, FALLING);  // or attachInterrupt(0, doordoingsomething, RISING); or attachInterrupt(0, doordoingsomething, CHANGE);
doorlock = 0
// other setup
interrupts();  // starts the int's
}
void loop(){
if (doorlock == 1){  // if this = 1 then int has fired
alarmneedstohappen();
}
// normal loop stuff
}

void doordoingsomething();
doorlock=1; // sets flag to 1 so loop if catches it
}

void alarmneedstohappen();{
// we are here because int flag happened.
doorlock = 0 // reset int flag
// perform other alarm stuff of whatever
}

Hello Grendle,

Thanks for your code but I have decided not to use interrupts because I cannot use the delay function in the ISR. When the signal is detected in pin 2 I am actually trying to send an SMS via sim300...
Code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(9,10);
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;   
int buttonState = 0;   
void setup()
{
mySerial.begin(9600); // the GPRS baud rate
delay(2000);
mySerial.println("AT");
delay(2000);
mySerial.println("AT+CSMP=17,167,0,0");
delay(2000);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);   

}
void loop()
{
    // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) { 
  digitalWrite(ledPin, HIGH);
  delay(100);
mySerial.println("AT+CMGF=1"); // set the SMS mode to text
delay(2500);
mySerial.write("AT+CMGS=");
mySerial.write(34); //ASCII of “
mySerial.write("+1123456...");
mySerial.write(34);
mySerial.write(13);
mySerial.write(10);
delay(2500);
mySerial.println("Yahoo, You are Successful...!"); //this is the message to be sent
delay(2500);
mySerial.write(26);
mySerial.write(13);
mySerial.write(10);//Ascii code of ctrl+z to send the message
}

  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

So I am trying to implement this code in the main loop. Will you kindly have look at the main code? Thanks
Logged

"Imagination is more important than knowledge."
-Einstein

Offline Offline
Sr. Member
****
Karma: 2
Posts: 344
YES! It is possible. Even Impossible Says I m Possible. :)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 variable
Code:
volatile int doorlock;

In the void setup() I have declared
Code:
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
Code:
  if( sms.indexOf("onb") >= 0 ){
    digitalWrite(  relay_b, HIGH );
    doorlock=1;  
  }

I have also added a code that says...
Code:
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...
Code:
#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?
Logged

"Imagination is more important than knowledge."
-Einstein

Pages: 1 [2] 3 4   Go Up
Jump to: