Pages: 1 2 [3] 4 5   Go Down
Author Topic: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please  (Read 8136 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7256
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you liudr. I have had a quick look at your code. Before i even try to use it I will have a read about state machines. I will get back to you and let you know how I get on.
regards
Jeremy

You are welcome.

Add the following in setup():

Code:
  Serial.begin(9600);
  pinMode(alarm_pin,OUTPUT);
  digitalWrite(alarm_pin,LOW);
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7256
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The original code was close to work. Here is code after I tested on my phi-3 shield:

Code:
#include <phi_interfaces.h>

#define buttons_per_column 6 // Each analog pin has five buttons with resistors.
#define buttons_per_row 1 // There are two analog pins in use.

byte keypad_type=Analog_keypad;
char mapping[]={'1','2','3','4','5','6'}; // This is an analog keypad.
byte pins[]={0}; // The pin numbers are analog pin numbers.
int values[]={14, 378, 563, 618, 656, 704}; //These numbers need to increase monotonically.
phi_analog_keypads panel_keypad(mapping, pins, values, buttons_per_row, buttons_per_column);
multiple_button_input* pad1=&panel_keypad;

#define alarm_pin 13
enum States {get_password, alarm, cleared};
States system_state=get_password;

unsigned long alarm_starting, cleared_starting;
char password[]="13531";
char buffer[20];
int pointer=0;

void setup()
{
  Serial.begin(9600);
  pinMode(alarm_pin,OUTPUT);
  digitalWrite(alarm_pin,LOW);
  digitalWrite(A0,HIGH); // DO this only if you are using phi-3 shield.
}

void loop()
{
  byte temp;
  switch (system_state)
  {
    case get_password:
    // do get password stuff
    temp=panel_keypad.getKey(); // Use phi_keypads object to access the keypad
    if (temp!=NO_KEY)
    {
      Serial.write(temp);
      buffer[pointer++]=temp;
      if (pointer==5)
      {
        buffer[pointer]=0;
        pointer=0;
        if (!strcmp(buffer,password))
        {
          Serial.println("Authenticated!");
          system_state=cleared;
          cleared_starting=millis();
          // Open a door or something
        }
       
        else
        {
          Serial.println("Unauthorized!");
          system_state=alarm;
          alarm_starting=millis();
          digitalWrite(alarm_pin,HIGH);
        }
      }
    }
    break;
   
    case alarm:
    if ((panel_keypad.getKey()!=NO_KEY)||(millis()-alarm_starting>=30000))
    {
      system_state=get_password;
      digitalWrite(alarm_pin,LOW);
    }
    break;
   
    case cleared:
    if ((!panel_keypad.getKey())||(millis()-cleared_starting>=30000))
    {
      system_state=get_password;
      // Close the door or something.
    }
    break;
  }
}
Logged


UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi liudr. Thank you for the revised code. This does work but when the alarm is tripped any keypad press turns the alarm off. I tried it this morning. I am reading all I can find about state machines too.
Perhaps I should leave it today as I have had more injections into my spine and they hurt badly. I may have a look later if I feel better. I hope I am not taking up too much of your time on this.
Regards
Jeremy
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7256
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi liudr. Thank you for the revised code. This does work but when the alarm is tripped any keypad press turns the alarm off. I tried it this morning. I am reading all I can find about state machines too.
Perhaps I should leave it today as I have had more injections into my spine and they hurt badly. I may have a look later if I feel better. I hope I am not taking up too much of your time on this.
Regards
Jeremy

Good! Yes, that feature can be turned on and off inside the alarm case. I don't kow what you exactly wanted.
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7256
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There is a problem with this code, in your get_password case. Once you get the 5th number, your telling the program to put a zero in the arrays 5th place.

Yeah right, so what's the problem?
Logged


Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3768
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Edit: Removed
« Last Edit: January 29, 2013, 11:30:52 am by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Good! Yes, that feature can be turned on and off inside the alarm case. I don't kow what you exactly wanted.
Hi liudr. What I wanted is to attach a PIR sensor and have the value it senses for movement set the alarm buzzer off for say 30 seconds. The keypad needs the correct code to disarm the alarm buzzer and ignore the PIR sensing or turn off the PIR sensor using a transistor. If i use a PNP transistor setting a digital pin HIGH via a resistor to the base of the transistor would cut the power to the PIR sensor.
Code:
if (pointer==5)
      {
        buffer[pointer]=0;
        pointer=0;
        if (!strcmp(buffer,password))
        {
          Serial.println("Authenticated!");
          system_state=cleared;
          cleared_starting=millis();
II hope this is a help
Jeremy
          // Open a door or something
          digitalWrite (tripped,LOW);
          digitalWrite(alarmOff,HIGH);
        }
   
        }
       
        else
        {
          Serial.println("Unauthorized!");
          system_state=alarm;
          alarm_starting=millis();
          digitalWrite(alarm_pin,HIGH);
          digitalWrite(tripped,HIGH);
          digitalWrite (alarmOff,LOW);
        }
      }
    }[code]
[/code]
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7256
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yeah right, so what's the problem?
Well any number you put in for the 5th digit, will be changed to zero, thats not a problem?
You enter 12345, the code after that part sees 123450. I can't test it myself, but thats what I see from this.

Again I can't test it, but if it works then it works.
No. Reread the code. More importantly, get the C from ground up by the author I recommended you.
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7256
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't have a PIR sensor so I can't help with that. I recommend you read my code a few times, find out how it is able to sound alarm/LED for 30 seconds while monitoring keypad input. Then you can change it to do your PIR logic. I imagine if someone walks up to the PIR sensor, it sounds the alarm. then they type in the code to disarm it? It's all your call how to do this.
Logged


Central MN, USA
Offline Offline
Tesla Member
***
Karma: 74
Posts: 7256
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Yeah right, so what's the problem?
Well any number you put in for the 5th digit, will be changed to zero, thats not a problem?
You enter 12345, the code after that part sees 123450. I can't test it myself, but thats what I see from this.

Again I can't test it, but if it works then it works.
No. Reread the code. More importantly, get the C from ground up by the author I recommended you.
HazardsMind,

Sorry I issued the RTFM on this. I should have pointed out what the code does:

1) It fills the buffer with up to 5 characters from the keypad (password).
2) It then terminates the string in the buffer with \0
3) It then performs a string comparison with the password using strcmp(), which requires a zero terminated C-string, thus step 2.
4) It then decides whether to sound the alarm or to clear the alarm.

My step 2 was not necessary if I set the C-string's 5th location to zero in setup like buffer[5]=0 or have char buffer[10]="     ". But if I move the code to a different project, that line is very likely to be left out (forget to move) and this wouldn't work (buffer[5] is just a random value). The initial value of the buffer is also wasting 6 bytes of arduino SRAM, which is only 2K long.
Logged


Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3768
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, thank you for clarifying that. It just seemed wrong for some reason, and I have no way to test your code for myself. (At work) I understand everything you want to do, its just, it looked odd to add a zero to buffer[5] and then compare it. I thought that when you got the last key, that is was adding on an extra 0.
« Last Edit: January 29, 2013, 01:51:13 pm by HazardsMind » Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi and a big thank you to liudr. Here is the code I am now using. I have got over the keypad press turning the alarm off. I have added another state called disarmed and the system stops at that point until the tactile button is pressed. This is fine but the motion sensor is a different story. I have tried instead to use a digital pin to read the sensor as it gives a clear 1 if motion detected and a 0 if not. I have tried using it in different places in the sketch but it is causing problems with the key presses(it was doing the same with an analog input) Also I can use a tactile button to simulate the motion sensor because it keeps going high with me moving my hands around.
It is getting there but not quite. I will keep trying to get the sensor working though.
Jeremy
Code:
#include <phi_interfaces.h>

#define buttons_per_column 12 // Each analog pin has five buttons with resistors.
#define buttons_per_row 1 // There are two analog pins in use.

byte keypad_type=Analog_keypad;
char mapping[]={'1','2','3','4','5','6','7','8','9','*','0','#'}; // This is an analog keypad.
byte pins[]={0}; // The pin numbers are analog pin numbers.
int values[]={ 58, 96, 145, 226, 335, 447, 558, 680, 790, 890, 941, 970}; //These numbers need to increase monotonically.
phi_analog_keypads panel_keypad(mapping, pins, values, buttons_per_row, buttons_per_column);
multiple_button_input* pad1=&panel_keypad;

#define alarm_pin 13
enum States {get_password, alarm, cleared,dissarmed};
States system_state=get_password;

unsigned long alarm_starting, cleared_starting;
char password[]="11111";// easy password for testing
char buffer[20];
int pointer=0;

int led=11;// led to simulate alarm
int PIR=A5;// motion sensor cannot get to work so not used
int tripped;// value to hold pir
int pirPin=5;/*I was using this pin as pir gives a digital 1 or 0 cannot get it to work
in the sketch it just seems to stall the keypad
*/
int buttonPin=2;//small tactile button to turn system on
int buttonState;//state of the tactile button
void setup()
{
  Serial.begin(9600);
  pinMode(alarm_pin,OUTPUT);
  digitalWrite(alarm_pin,LOW);
 // digitalWrite(A0,HIGH); // DO this only if you are using phi-3 shield.
 pinMode(alarm_pin,OUTPUT);
  digitalWrite(alarm_pin,LOW);
  pinMode(led,OUTPUT);
  pinMode(pirPin,INPUT);
  pinMode(buttonPin,INPUT);
 
}

void loop()
{
  byte temp;
  switch (system_state)
  {
    case get_password:
     if  (millis()-alarm_starting>=10000)// if alarm is tripped this turns led off
     // I changed the time just for testing
        {
          digitalWrite(led,LOW);
        }
    // do get password stuff
    temp=panel_keypad.getKey(); // Use phi_keypads object to access the keypad
    if (temp!=NO_KEY)
    {
      Serial.write(temp);
      buffer[pointer++]=temp;
      if (pointer==5)
      {
        buffer[pointer]=0;
        pointer=0;
        if (!strcmp(buffer,password))
        {
          Serial.println("Authenticated!");
         system_state=dissarmed;//cleared;
         //dissarmed turns the system off
          cleared_starting=millis();
          digitalWrite(led,LOW);
          // Open a door or something
        }
       
        else
        {
          Serial.println("Unauthorized!");
          system_state=alarm;
          alarm_starting=millis();
          digitalWrite(alarm_pin,HIGH);
        }
      }
    }
    break;
   
   
    case dissarmed:
    digitalWrite(led,LOW);
    buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {    //system off until button pressed.
     system_state=get_password;
  }
    break;
   
   
    case alarm:
     digitalWrite(led,HIGH);
    if ((panel_keypad.getKey()!=NO_KEY)||(millis()-alarm_starting>=10000))
    {
      system_state=get_password;
      digitalWrite(alarm_pin,LOW);
    }
    break;
   
    case cleared:
    if ((!panel_keypad.getKey())||(millis()-cleared_starting>=10000))
    {
      system_state=get_password;
      // Close the door or something.
    }
    break;
  }
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry I should have added that the pir stays high for 6 seconds after activation
Jeremy
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Queens, New York
Offline Offline
Faraday Member
**
Karma: 106
Posts: 3768
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see that you declaired "pinMode(pirPin,INPUT);" but where in your code are you reading it, or have you not gotten to that part yet?
Logged

Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

UK Norfolk
Offline Offline
Newbie
*
Karma: 3
Posts: 48
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi HazardsMind. I did put the sensor in in various places in the get password case because i could only get a reading on it there. I found it caused problems with the keypad function.
The problem seems to be in the case get password part of the sketch where the program sits for the majority of the time waiting for a key press.I wherever the sensor is in the sketch the keypad is slow to respond and I noticed that when the alarm was triggered the led on pin 11 was very dim. I have tried giving the motion sensor its own power supply with the grounds connected but this makes no difference. I tried the led on a different pin with the same result. I was trying to work out how to have the sketch sit waiting in a different part lets call it case check_sensor but I am not able to do this. My programming skills have been learned over the last few months with some library books and the internet.
 I used to drive a Bus and my hobby is electronics and now I am learning to program my arduino.
The trouble with me is I always think     "How hard can it be?"
Learning to write code is like learning to speak Chinese with a Spanish phrase book. But I am picking it up slowly.
Jeremy
Logged

There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

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