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.
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?
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
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?
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.
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()
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();
}
}
}