arduino based security lock

i have a security lock that will be use at doors or vaults that has a NUMERIC KEYPAD, SOLENOID LOCK, A GSM SHIELD, LEDs to be use as indicators. RED = LOCK state, GREEN = UNLOCK state, YELLOW = WILL BLINK WHEN ANY KEYS ARE PRESS.

operation: initially the device is in “LOCK” state . then if i want it to “UNLOCK” you have 3 trials to input secretcode.

case 1: at third wrong input of the secretcode,the device will alarm, GSM will send a text message informing the owner that someone is trying to unlock the lock.
case 2: if correct input of the secretcode, the device will be “UNLOCK”, and the GSM will send a text message informing the owner that the device is unlock. then if " * " is pressed, it will be at “LOCKED” state.

the device is working.

here is the code

#include <Keypad.h>
char Rx_data[50];
unsigned char Rx_index = 0;
int i = 0;
char msg[160];
int sig;
char* secretCode = "00000";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
  {'1','2','3'  },
  {'4','5','6'  },
  {'7','8','9'  },
  {'*','0','#'  }
};
byte rowPins[rows] = {
  6,7,8,9};
byte colPins[cols] = {
  10,11,12};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 2;
int greenPin = 3;
int yellowPin = 13;

int x = 0;
int y = 0

;
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Arduino Security Lock System");
}
void loop()
{
  char key = keypad.getKey();
  Serial.println(key);
  if (key == '*')
  {
    digitalWrite(yellowPin, HIGH);  // yellow led will blink at  
    delay(50);                             // 50ms once the Asterisk at the keypad is 
    digitalWrite(yellowPin, LOW);   //  push
    setLocked(true);
    Serial.println("LOCKED");
    Serial.begin(38400);

    initGSM();
    send_msg("09062012708", "LOCKED");
  }
  if (key == '#')
  {
    digitalWrite(yellowPin, HIGH);   // yellow led will blink at 
    delay(50);                              // 50ms once the number sign at the keypad is 
    digitalWrite(yellowPin, LOW);    //  push
    position = 0;
    x = 0;
    Serial.println("Reset");
    Serial.begin(38400);
  }
  if (key == secretCode[position])
  {
    position ++;
  }
  if (position == 5)
  {
    setLocked(false);
    Serial.begin(38400);

    initGSM();
    send_msg("09062012708", "UNLOCK");
  }
  delay(100);

  if ((position >= 0) && (position <= 4))
  {
    if ((key >= '0') && (key <= '9'))
    { 
      x = x+5;
      digitalWrite(yellowPin, HIGH);
      delay(50);
      digitalWrite(yellowPin, LOW);
      Serial.println(x);
      delay(100);
    }
  }
  if (x == 25)
  {
    digitalWrite(redPin, HIGH);
    digitalWrite(yellowPin, HIGH);
    y++;
    if (y==3)
    { 
      digitalWrite(redPin, HIGH);         // at 3rd wrong trial,  
      digitalWrite(yellowPin, HIGH);     // red and yellow led
      delay(2000);                             // will blink for 2 sec
      digitalWrite(yellowPin, LOW);      // yellow led will be off, red led will remain on
      y = 0;
      x = 0;
      Serial.begin(38400);

      initGSM();
      send_msg("09062012708", "WARNING: SOMEONE IS TRYING TO UNLOCK");
      delay(100);
    }


    delay(100);                               // from this line 
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);      // yellow led will blink 6 times if secret code is wrong
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);     // down to this line 
    x = 0;
  }

}
void setLocked(int locked)
{
  if (locked)
  {
    digitalWrite(redPin, HIGH);          // lock state, red = on
    digitalWrite(greenPin, LOW);          
    delay(100);
  }
  else
  {
    digitalWrite(redPin, LOW);          //  unlock state, green = on
    digitalWrite(greenPin, HIGH);
    x = 0;
    y = 0;
    delay(100);
  }
}





void send_msg(char *number, char *msg)
{
  char at_cmgs_cmd[30] = {
    '\0'  };
  char msg1[160] = {
    '\0'  };
  char ctl_z = 0x1A;

  sprintf(msg1, "%s%c", msg, ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);

  sendGSM(at_cmgs_cmd);
  delay(100);
  delay(100);
  delay(100);
  sendGSM(msg1);
  delay(100);
}

void sendGSM(char *string){
  Serial.write(string);
  delay(90);
}

void clearString(char *strArray) {
  int j;
  for (j = 100; j > 0; j--)
    strArray[j] = 0x00;
}

void send_cmd(char *at_cmd, char clr){
  char *stat = '\0';
  while(!stat){
    sendGSM(at_cmd);
    delay(90);
    readSerialString(Rx_data);

    stat = strstr(Rx_data, "OK");
  }
  if (clr){
    clearString(Rx_data);
    delay(200);
    stat = '\0';
  }
}

void initGSM(){

  send_cmd("AT\r\n",1);						
  //  send_cmd("ATE0\r\n",1); // Turn off automatic echo of the GSM Module	

  send_cmd("AT+CMGF=1\r\n",1);			// Set message format to text mode
  //Sucess

  Serial.println("Success");

  delay(1000);
  delay(1000);
  delay(1000);
}

void readSerialString (char *strArray) {

  if(!Serial.available()) {
    return;
  }

  while(Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}

but i need to add another ability to it. i want to “unable and disable” the keypad of the device via text message or call from the owner’s cellphone. by this way, an intruder cannot input password any more the owner of the device wishes to.

PLEASE I NEED YOUR HELP… this is my requirement for my degree

I don't understand
why your code wanders
all over
the page randomly.

Haven't you learned how to
use Tools + Auto Format
to stop that?

i want to "unable and disable" the keypad of the device via text message or call from the owner's cellphone.

So, what's the problem?

int y = 0

;

Or, should I say:
So, what's the problem

?

Sir my problem is how to enable/disable of inputting secret code to the device by a text or call from a cellphone…

i will post again my code in a better way

#include <Keypad.h>
char Rx_data[50];
unsigned char Rx_index = 0;
int i = 0;
char msg[160];
int sig;
char* secretCode = "12345";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
  {'1','2','3'  },
  {'4','5','6'  },
  {'7','8','9'  },
  {'*','0','#'  }
};
byte rowPins[rows] = {
  6,7,8,9};
byte colPins[cols] = {
  10,11,12};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 2;
int greenPin = 3;
int yellowPin = 13;

int x = 0;
int y = 0

;
void setup()
{
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("Arduino Security Lock System");
}
void loop()
{
  char key = keypad.getKey();
  Serial.println(key);
  if (key == '*')
  {
    digitalWrite(yellowPin, HIGH);
    delay(50);
    digitalWrite(yellowPin, LOW);
    setLocked(true);
    Serial.println("LOCKED");
    Serial.begin(38400);

    initGSM();
    send_msg("09062012708", "LOCKED");
  }
  if (key == '#')
  {
    digitalWrite(yellowPin, HIGH);
    delay(50);
    digitalWrite(yellowPin, LOW);
    position = 0;
    x = 0;
    Serial.println("Reset");
    Serial.begin(38400);
  }
  if (key == secretCode[position])
  {
    position ++;
  }
  if (position == 5)
  {
    setLocked(false);
    Serial.begin(38400);

    initGSM();
    send_msg("09062012708", "UNLOCK");
  }
  delay(100);

  if (position == 0)
  {
    if (key == '1' || key == '2' ||  key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0' || key == '4' || key == '3')
    { 
      x = x+5;
      digitalWrite(yellowPin, HIGH);
      delay(50);
      digitalWrite(yellowPin, LOW);
      Serial.println(x);
      delay(100);
    }
  }
  if (position == 1)
  {
    if (key == '1' || key == '2' ||  key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0' || key == '4' || key == '3')
    { 
      x = x+5;
      digitalWrite(yellowPin, HIGH);
      delay(50);
      digitalWrite(yellowPin, LOW);
      Serial.println(x);
      delay(100);
    }
  }
  if (position == 2)
  {
    if (key == '1' || key == '2' ||  key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0' || key == '4' || key == '3')
    { 
      x = x+5;
      digitalWrite(yellowPin, HIGH);
      delay(50);
      digitalWrite(yellowPin, LOW);
      Serial.println(x);
      delay(100);
    }
  }
  if (position == 3)
  {
    if (key == '1' || key == '2' ||  key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0' || key == '4' || key == '3')
    { 
      x = x+5;
      Serial.println(x);
      digitalWrite(yellowPin, HIGH);
      delay(50);
      digitalWrite(yellowPin, LOW);
      delay(100);
    }
  }
  if (position == 4)
  {
    if (key == '1' || key == '2' ||  key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0' || key == '4' || key == '3')
    { 
      x = x+5;
      digitalWrite(yellowPin, HIGH);
      delay(50);
      digitalWrite(yellowPin, LOW);
      Serial.println(x);
      delay(100);
    }
  }
  if (x == 25)
  {
    digitalWrite(redPin, HIGH);
    digitalWrite(yellowPin, HIGH);
    y++;
    if (y==3)
    {
      digitalWrite(redPin, HIGH);
      digitalWrite(yellowPin, HIGH);
      delay(2000);
      digitalWrite(yellowPin, LOW);
      y = 0;
      x = 0;
      Serial.begin(38400);

      initGSM();
      send_msg("09062012708", "WARNING: SOMEONE IS TRYING TO UNLOCK");
      delay(100);
    }


    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    delay(100);
    digitalWrite(yellowPin, HIGH);
    delay(100);
    digitalWrite(yellowPin, LOW);
    x = 0;
  }

}
void setLocked(int locked)
{
  if (locked)
  {
    digitalWrite(redPin, HIGH);
    digitalWrite(greenPin, LOW);
    delay(100);
  }
  else
  {
    digitalWrite(redPin, LOW);
    digitalWrite(greenPin, HIGH);
    x = 0;
    y = 0;
    delay(100);
  }
}





void send_msg(char *number, char *msg)
{
  char at_cmgs_cmd[30] = {
    '\0'  };
  char msg1[160] = {
    '\0'  };
  char ctl_z = 0x1A;

  sprintf(msg1, "%s%c", msg, ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);

  sendGSM(at_cmgs_cmd);
  delay(100);
  delay(100);
  delay(100);
  sendGSM(msg1);
  delay(100);
}

void sendGSM(char *string){
  Serial.write(string);
  delay(90);
}

void clearString(char *strArray) {
  int j;
  for (j = 100; j > 0; j--)
    strArray[j] = 0x00;
}

void send_cmd(char *at_cmd, char clr){
  char *stat = '\0';
  while(!stat){
    sendGSM(at_cmd);
    delay(90);
    readSerialString(Rx_data);

    stat = strstr(Rx_data, "OK");
  }
  if (clr){
    clearString(Rx_data);
    delay(200);
    stat = '\0';
  }
}

void initGSM(){

  send_cmd("AT\r\n",1);						
  //  send_cmd("ATE0\r\n",1); // Turn off automatic echo of the GSM Module	

  send_cmd("AT+CMGF=1\r\n",1);			// Set message format to text mode
  //Sucess

  Serial.println("Success");

  delay(1000);
  delay(1000);
  delay(1000);
}

void readSerialString (char *strArray) {

  if(!Serial.available()) {
    return;
  }

  while(Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}

Why is this:

if (position == 0)
  {
    if (key == '1' || key == '2' ||  key == '5' || key == '6' || key == '7' || key == '8' || key == '9' || key == '0' || key == '4' || key == '3')
    { 
      x = x+5;
      digitalWrite(yellowPin, HIGH);
      delay(50);
      digitalWrite(yellowPin, LOW);
      Serial.println(x);
      delay(100);
    }
  }

repeated four times?
And why not if ((key >= '0') && (key <= '9')) ?

First of all, I think you can clean the code up a lot, but you have probably gathered that from the comments above. To do what you want depends on the capability of the GSM shield. I assume it can receive messages? You need to understand how to interrogate it to see if messages are waiting to be processed, and how to get the message from the shield into your program. Then you can work on the program code:

Pseudo code:

setup()
{
    Initialise inputs and outputs
    enable keypad
    initialise GSM module
}


loop()
{
    check for GSM message
    if GSM message is available {
        if GSM message is to lock keypad {
            disable keypad
        } else if GSM message is to unlock keypad {
            enable keypad
        }
    }

    if keypad is enabled {
        process key
    }
}

I haven't made any attempt to untangle the input processing and the handling of the wrong entries - I leave that for you.

thank you for your replies... :slight_smile:
hope to have more from you sir!

actually, it wasn't me who did on this part of the program,

void send_msg(char *number, char *msg)
{
  char at_cmgs_cmd[30] = {
    '\0'  };
  char msg1[160] = {
    '\0'  };
  char ctl_z = 0x1A;

  sprintf(msg1, "%s%c", msg, ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);

  sendGSM(at_cmgs_cmd);
  delay(100);
  delay(100);
  delay(100);
  sendGSM(msg1);
  delay(100);
}

void sendGSM(char *string){
  Serial.write(string);
  delay(90);
}

void clearString(char *strArray) {
  int j;
  for (j = 100; j > 0; j--)
    strArray[j] = 0x00;
}

void send_cmd(char *at_cmd, char clr){
  char *stat = '\0';
  while(!stat){
    sendGSM(at_cmd);
    delay(90);
    readSerialString(Rx_data);

    stat = strstr(Rx_data, "OK");
  }
  if (clr){
    clearString(Rx_data);
    delay(200);
    stat = '\0';
  }
}

void initGSM(){

  send_cmd("AT\r\n",1);						
  //  send_cmd("ATE0\r\n",1); // Turn off automatic echo of the GSM Module	

  send_cmd("AT+CMGF=1\r\n",1);			// Set message format to text mode
  //Sucess

  Serial.println("Success");

  delay(1000);
  delay(1000);
  delay(1000);
}

void readSerialString (char *strArray) {

  if(!Serial.available()) {
    return;
  }

  while(Serial.available()) {
    strArray[i] = Serial.read();
    i++;
  }
}

it was my friend who did this and he wasn't able to explain to me..
anyone who can explain to me this part?

You will really have to try harder! This is supposed to be your degree! If you just mash up code from different sources without trying to understand how and why the code is written, then the project would be meaningless. From the way you have written the code, you haven’t really grasped the simplest concepts of C++, and now you have admitted that the GSM part of the code has been written by someone else. Trust me - it isn’t difficult to do with a little understanding and research :slight_smile:

A quick bit of googling tells me:

  • If you are using Arduino 1.0.4, there is a GSM library included which manages the GSM shield. It treats the shield as an object in C++ terms, so you will need to understand the implications.
  • If you are not using 1.0.4, then you can take the concepts from that library and include it in your program. I suspect you will find this beyond your coding skills at the moment.
  • Here is the documentation link for the GSM library: http://arduino.cc/en/Reference/GSM
  • Here is an example of how to receive SMS: http://arduino.cc/en/Tutorial/GSMExamplesReceiveSMS

One of the positive aspects of the arduino is how easy it is to get up and running with micro controllers. One of the downsides is that there are several concepts to be mastered before real progress can be made, and many people seem to fall into the trap you have found - mashing up code from several sources without grasping what is happening. This is one of the reasons (IMHO) you see a lack of help for people who come on the board with “Can you help me - I just bought an arduino and want to build X with it” type questions.

Finally, a little story: I studied Materials Science and Technology at university back in the 1980’s and one of the other students in my hall of residence had some course work to do with a small board microcontroller (6502 :)) and an X-Y table driving a pen around with two stepper motors. He didn’t understand it, and I wanted to take the challenge so I did his work for him. He was questioned about the code (assembler), but couldn’t explain what was happening, and in the end he had to admit he hadn’t done it himself. I hope you don’t find yourself in this position.