Comparing onewire 4x4 keypad password

Good evening guys,
Hope every member of this great forum would be doing good. I'm a newbie to Arduino programing and I need help. Scenario is, i want to use one pin of Arduino for 4x4 keypad which and i succeeded, now i stuck on how to compare the password which is taken by the 4x4 keypad. Following is the code of my project. Any help, advice and suggestions would be highly appreciated. Thank You.

void setup() {
  Serial.begin(9600);  /* Define baud rate for serial communication */

}

void loop() {
  
 keys();


}
void keys()
{

  int adc_val;
  adc_val = analogRead(A1); /* Read input from keypad */

  if (adc_val>235 && adc_val<245)
  {
    Serial.print("Key Pressed : ");
    Serial.println("0");
    delay(100);
  }
  else if ( adc_val>1005 && adc_val<1015)
  {
    Serial.print("Key Pressed : ");
    Serial.println("1");
    delay(100);
  }
  else if ( adc_val>840  && adc_val<850)
  {
    Serial.print("Key Pressed : ");
    Serial.println("2");
    delay(100);
  }
  else if ( adc_val>690  && adc_val<700)
  {
    Serial.print("Key Pressed : ");
    Serial.println("3");
    delay(100);
  }
  else if ( adc_val>500  && adc_val<515)
  {
    Serial.print("Key Pressed : ");
    Serial.println("4");
    delay(100);
  }
  else if ( adc_val>450  && adc_val<465)
  {
    Serial.print("Key Pressed : ");
    Serial.println("5");
    delay(100);
  }
  else if ( adc_val>400 && adc_val<420)
  {
    Serial.print("Key Pressed : ");
    Serial.println("6");
    delay(100);
  }
  else if ( adc_val>335  && adc_val<350)
  {
    Serial.print("Key Pressed : ");
    Serial.println("7");
    delay(100);
  }
  else if ( adc_val>301  && adc_val<325)
  {
    Serial.print("Key Pressed : ");
    Serial.println("8");
    delay(100);
  }
  else if ( adc_val>275  && adc_val<300)
  {
    Serial.print("Key Pressed : ");
    Serial.println("9");
    delay(100);
  }
  else if ( adc_val>635  && adc_val<645)
  {
    Serial.print("Key Pressed : ");
    Serial.println("A");
    delay(100);
  }
  else if ( adc_val>380  && adc_val<400)
  {
    Serial.print("Key Pressed : ");
    Serial.println("B");
    delay(100);
  }
  else if ( adc_val>908  && adc_val<68)
  {
    Serial.print("Key Pressed : ");
    Serial.println("C");
    delay(100);
  }
  else if ( adc_val>876  && adc_val<62)
  {
    Serial.print("Key Pressed : ");
    Serial.println("D");
    delay(100);
  }
  else if ( adc_val>248  && adc_val<265)
  {
    Serial.print("Key Pressed : ");
    Serial.println("*");
    delay(100);
  }
  else if( adc_val>215  && adc_val<234)
  {
    Serial.print("Key Pressed : ");
    Serial.println("#");
    delay(100);
  }
  else
  {
    
  }
  delay(100);
}

As your characters are received, place them into a character string array.

When you detect a terminator character, check your assembled array against a password array.

Maybe add a timeout function where if the characters do not arrive within a certain period, cancel the attempt and flash an error LED.

1 Like

Appreciate your reply, like i said im not good at c++ a little help how i can make an array would be great.

Read these discussions:

If you can understand what’s being said, try using an array in your sketch.

If you run into problems we can help.

1 Like

How is the analog input ever going to be simultaneously greater than 908 and less than 68?!?

1 Like

The first suggestion that comes to my mind is to refactor that huge chain of else if into some manageable function instead of hard-coding everything over and over... 16 times! If you look around, you may even find a library for your particular piece of hardware, to take the bulk of the work off your sketch.

Library or no library, however, a function that reads the keypress and returns a char should be the core of the program. Then, you take that char and you print it out, or put it into an array, or do whatever you want with it. I would also get rid of those delay(100). Are you sure you need them the way they are?

1 Like

not using that button at the moment ...

all i need i solution and little help thats it.

I've found the codes with the keypad library (#include <Keypad.h>) and its working but i want to use one analog pin of arduino to attiny85 and i have Zero experience of c++ programming

They are not the same thing, though.

If the library is working, use it as a temporary foundation to develop the second stage of your program (i.e. validating the password). When this second stage is up and running, you can refactor the keypad-handling code to suit your hardware needs. Make sense?

1 Like

well i guess so umm but im not sure, would u please kind enough to guide me how can i store pressed value in array as int.

Use a char, not an int. What you are after is an ascii value here. Try to work in small increments. For instance, start by saving 3 consecutive chars into a fixed-size array and print the result to serial as a whole string. Then add a timeout that will start the recording over if too much time passes after the first or second key press and put out an error message to serial.

P.S. Remember to declare something like char code[4] to make space for the terminating null char.

1 Like

went above my stupid head :smiley:

Here's an example I wrote got the Keypad library. You may be able to adapt it to your analog keypad:

#include <Keypad.h>

// Combination is "08675309"
// Use any value up to 4 billion

// NOTE: 08675309 is not a valid number and will
// not compile.  The leading zero means 'octal
// constant' and the digits 8 and 9 are not valid
// octal digits.
//
// To get leading zeroes, set CombinationLength
// higher than the number of digits

const unsigned long Combination = 8675309;
const byte CombinationLength = 8;

const byte ROWS = 4; // set four rows
const byte COLS = 4; // set four columns
const char keys[ROWS][COLS] =   // Define the keymap
{
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};

byte rowPins[ROWS] = { 36, 34, 32, 30 };
byte colPins[COLS] = { 28, 26, 24, 22 };
// Create the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );

unsigned long InputValue = 0;
byte InputLength = 0;

void setup()
{
  Serial.begin(115200);
  pinMode(46, OUTPUT); // Set green LED as output
  pinMode(48, OUTPUT); // Set red LED as output
}

void loop()
{
  char key = keypad.getKey();

  switch (key)
  {
    case '*':  // Clear
      InputValue = 0;
      InputLength = 0;
      break;

    case '0'...'9':
      InputValue *= 10;
      InputLength++;
      InputValue += key - '0';
      break;

    case '#': // Enter
      if (InputLength == CombinationLength && InputValue == Combination)
      {
        Serial.println("Success, Come inside"); // If the password is correct
        digitalWrite(46, HIGH); // Turn on green LED
        delay(500); // Wait 5 seconds
        digitalWrite(46, LOW); // Turn off green LED
      }
      else
      {
        Serial.println("Access Denied"); // If the password is incorrect
        digitalWrite(48, HIGH); // Turn on red LED
        delay(500); // Wait 5 seconds
        digitalWrite(48, LOW); // Turn off red LED
      }
      InputValue = 0;
      InputLength = 0;
      break;

  }  // switch(key)
}  // loop()
1 Like

appreciate your codes but i need something that would work with my project as im not using keypad.h. I also have the following codes that works like a charm but with 8 pins of Arduino.



/*
 Author: Danny van den Brande. Arduinosensors.nl. BlueCore Tech.
 Hello world! This code is made to put a 
 access code/password protection on your dangerous machines in the garage/work place for example.
 To protect your kids from the danger that might lurk in your garage, without the code.
 power cannot be turned on. Hide it in a box with that you can lock on the wall
 and add a little safery to your garage. Of course it can be used to turn on/off anything.
 */
#include <Keypad.h> 
#include <Password.h> 
 
int relay1 = 2; 
int relay2 = 3; 
 


int noAccesled = 9; 
int AccesLed = 10; 
 
int noAcces = 1;
int passinput = 0;
 
long flashvarled = 0; 
long flashtimeled = 300;  
 
const byte ROWS = 4;
const byte COLS = 4;
 
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}};
byte rowPins[ROWS] = {
  A1};
byte colPins[COLS] = {
  A1};
 
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
Password password = Password("6240"); // change the access code here
 
void setup(){
  Serial.begin(9600);
  pinMode(relay1, OUTPUT);
  digitalWrite(relay1, 255);
  pinMode(relay2, OUTPUT);
  digitalWrite(relay2, 255);

  pinMode(noAccesled, OUTPUT);
  digitalWrite(noAccesled, 255);
  pinMode(AccesLed, OUTPUT);
  digitalWrite(AccesLed, 0);

}
 
void loop(){
  char key = keypad.getKey();
  if(noAcces){
    if(passinput){
      unsigned long currentvarled = millis();
      if(currentvarled - flashvarled > flashtimeled) {
        flashvarled = currentvarled;
        digitalWrite(noAccesled, !digitalRead(noAccesled));
      }
    }
    else{
      digitalWrite(noAccesled, 255);
    }
    digitalWrite(AccesLed, 0);
  }
  if (key != NO_KEY){
    Serial.println(key);
    password.append(key);
    passinput = 1;

    delay(100);

    if(key == '*'){
      password.reset();
      passinput = 0;
      noAcces = 1;
      digitalWrite(relay1, 1);
      digitalWrite(relay2, 1);

    }
    if(password.evaluate()) {
      noAcces = !noAcces; 
      password.reset();
      passinput = 0;
    }
    if(!noAcces) {
      passinput = 0;
      digitalWrite(noAccesled, 0);
      digitalWrite(AccesLed, 255);
      switch (key) {
        case 'A':
          digitalWrite(relay1, !digitalRead(relay1));
          break;
        case 'B':
          digitalWrite(relay2, !digitalRead(relay2));
          break;
    
          break;
      }
      password.reset();
    }
  }
}

how can i modify keypad library with my analog keypad sir ? a little elaboration is required please ..

And here is an 'AnalogKeypad' example sketch that might make you analog keypad work more like a matrix Keypad:

class AnalogKeypad
{
  public:

    AnalogKeypad(int ap, int keyCount, const char *keyChars, const int *keyValues) :
      AnalogPin(ap), KeyCount(keyCount), KeyChars(keyChars), KeyValues(keyValues)
    {
      lastChar = 0;
    };

    char getKey()
    {
      int value = analogRead(AnalogPin);

      if (value < KeyValues[0])
      {
        lastChar = 0;
        return 0;
      }

      for (int i = 1; i <= KeyCount; i++)
      {
        if (value < KeyValues[i])
        {
          if (KeyChars[i] != lastChar)
          {
            lastChar = KeyChars[i];
            return lastChar;
          }
        }
      }
      lastChar = 0;
      return 0;
    };


  private:
    const int AnalogPin;
    const int KeyCount;
    const char *KeyChars;
    const int *KeyValues;
    int lastChar;
};




const int KeypadPin = A0;
const int KeyCount = 16;
const int AnalogValues[KeyCount + 1] = {};
const char KeyChars[KeyCount + 1] = "123A456B789C*0#D";

AnalogKeypad keypad(KeypadPin, KeyCount, KeyChars, AnalogValues);

void setup()
{
  Serial.begin(115200);  /* Define baud rate for serial communication */
}

void loop()
{
  char key = keypad.getKey();
  if (key != 0)
    Serial.print(key);
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.