Go Down

Topic: Home Alarm with a 3 wire 4x3 analog keypad nearly done but need some help please (Read 9262 times) previous topic - next topic

HazardsMind

This has one too many values.
Code: [Select]
int values[]={0, 58, 96, 145, 228, 335, 447, 598, 720, 809, 890, 941, 970};
Get rid of the first 0, to make it match,
Code: [Select]
char mapping[]={'1','2','3','4','5','6','7','8','9','*','0','#'};
My GitHub:
https://github.com/AndrewMascolo?tab=repositories

jezza103

Hi HazardsMind. Thank you. Well done it compiles now and works. I have a few buttons on my keypad that give inconsistent readings though. Take button 4 for example it returns anything from 220-229 so I tried to adapt the "values" array to
Code: [Select]

int values[12][3] = {{1, 50, 65}, // button 1
                       {2, 90, 110}, // button 2
                     {3, 137, 160}, // button 3
                     {4, 200, 245}, // button 4
                     {5, 300, 350}, // button 5
                     {6, 420, 470}, // button 6
                     {7, 500, 620}, // button 7
                     {8, 700, 745}, // button 8
                     {9, 780, 820}, // button 9
                     {10, 844, 900}, // button *
                     {11, 930, 955}, // button 0
                     {12, 956, 999}}; // button #

The trouble is it didn't work so I am back to square 1 on this but I have learned quite a bit over the last few days.I will have to try again on this.
I have a 7 pin 4x3 keypad which is working fine on my project but, I just need to get this to work now,if nothing else just to show that it can be done. It is a wonder that more of you guys are not using a keypad like this to free up those digital pins. unless of course you are all using "Mega's"
Regards
Jeremy
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

liudr

Jeremy,

Just use the center value between the max and min for each button in that sample code (sorry about the zero) and then look at the range of these values. There are 12 (effectively 13 values with the 0) so you are cutting the whole 1024 range into small sub-ranges. In the phi_interfaces library, there is a factor to decide the threshold of a match. Change that to maybe 20 so you will get more hits. The 2D array will not work with my library.

Open phi_interfaces.cpp and look for this line:       if (diff<10)

Change 10 to 20. I should have defined a sensitivity=10 but didn't.

jezza103

Yes It works now. Thanks Liudr. Right now for the password part. I think I am getting there. It has taken me ages to get this idea working. I knew it could be done I just couldn't figure it out on my own.
No problem about the "0" I should have spotted that myself.
Have you made your own keypad yet?
Jeremy
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

liudr

I might be able to find you some code to enter password but if you want some simplicity, get my phi-panel serial lcd backpack with matrix keypad connection.

jezza103

Hi Liudr I would appreciate some code if you have some for the password. But as for the  phi-panel serial lcd backpack. I would love one but I have no funds at the moment because I am on what you call welfare since my injury.  I will make do with what I have in the way of hardware. my alarm when it is finally done will not have a display just some indicator led's. I will have to do something about my keypad though it is just 12 buttons soldered onto a piece of protoboard. But that may just confuse a Burglar.
Regards
Jeremy
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

jezza103

Have you ever felt that you are in a real life
void loop(){
try this;
did it work y/n;
n;
try that;
did it work y/n
n;
}
Sorry but it is driving me nuts. Maybe I could learn to make cakes ^_^
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

liudr

I found some time today and wrote this code for you:

Code: [Select]
#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;

char password[]="13531";
char buffer[20];
int pointer=0;

void setup()
{
  Serial.begin(9600);
  Serial.println("Phi_interfaces library analog keypad test code: password");
  digitalWrite(A0,HIGH); // DO this only if you are using phi-3 shield.
}

void loop()
{
  byte temp;
  Serial.println("Enter 5-digit password:");
  while(1)
  {
    temp=panel_keypad.getKey(); // Use phi_keypads object to access the keypad
  //  byte temp=pad1->getKey(); // Use the generic multiple_button_interface to access the same 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!");
        }
       
        else
        {
          Serial.println("Unauthorized!");
        }
        return;
      }
    }
  }
}


It works on my phi-3 shield (experimental) so if you modify it to work on your keypad it should work too.

jezza103

Hi Liudr. Thank you so much for the code. It works !!!!  I cannot believe that after all this time I have a working analog keypad with a password function. It is coming together now and I am going to add a bit to it to get my PIR sensor working and buzzer and I will be there.
Thanks again
Jeremy
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

jezza103

I have been at it all evening trying to get my PIR sensor to work. The only way I have found is to do away with the while statement. I had to turn off the Serial.println('Enter 5 digit password') line but I can not get the siren pin to go high, once switched on the sensor goes high before settling down.
The sensor once tripped stays HIGH for five seconds before it goes LOW again. I need the siren to wait for that amount of time before going off or it will trip as soon as you power on the system.
And I thought getting the keypad done was the hard bit. I think I may have bitten off more than I can chew here. I am not sure how to get the siren to wait but maybe the answer is using the millis function. I am not sure if that will influence the code I am using? Should i look at the blink without delay sketch or just use a switch case with a delay to wait for the sensor;
I think I will sleep on it as it is 1:30 am
Jeremy
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

liudr


Hi Liudr. Thank you so much for the code. It works !!!!  I cannot believe that after all this time I have a working analog keypad with a password function. It is coming together now and I am going to add a bit to it to get my PIR sensor working and buzzer and I will be there.
Thanks again
Jeremy


Jeremy,

You are welcome. I will include this example in my next library release.

liudr

Yeah, the blink without delay example will help you multitask instead of just reading the keypad. Give it a try.

jezza103

Hi again. My brain aches and I still cannot get this to work. I have tried this way and that but nothing seem to work. The thing is that I know too little about programming to understand what I am doing wrong. This evening my computer crashed and I lost what I have been doing all day(I was an idiot and didn't save it) but then again it was not working anyway. If I use the millis function to time the amount of time the alarm sounds when it is triggered the keypad will not respond until the time millis function has completed. I had the siren sounding for 30 seconds and in that time the keypad will not function.
Regards
Jeremy
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

liudr

Read about state machines. If you use delay, you won't be able to anything while delaying. If you want to use the keypad while the alarm is sounding for 30 seconds, you can't use delay, use state machine and timers instead.

I didn't check on an arduino but it compiles.

Code: [Select]
#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 10
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()
{
}

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();
          tone(alarm_pin,550);
        }
      }
    }
    break;
   
    case alarm:
    if ((!panel_keypad.getKey())||(millis()-alarm_starting>=30000))
    {
      system_state=get_password;
      noTone(alarm_pin);
    }
    break;
   
    case cleared:
    if ((!panel_keypad.getKey())||(millis()-cleared_starting>=30000))
    {
      system_state=get_password;
      // Close the door or something.
    }
    break;
  }
}

jezza103

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
There are times when electronics projects are like Women. Just when you think everything is fine the blow up in your face :-0

Go Up