The following sketch is for a entry exit system.
The password should be set in serial monitor, then entry via a 44 matrix keyboard.
when setting the passcode serial monitor reports for example passcode set to 1234
I then enter 1234* on the matrix keyboard only to be told wrong even though it then reports the passcode is 1234*.
I can not see where I am going wrong, any help!
//*
// || Simple Password Entry Using Matrix Keypad
//* is to validate password
//# is to reset password attempt
/////////////////////////////////////////////////////////////////
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
String inputString = ""; // a string to hold incoming data
String inString = "";
Password password = Password(inString.toInt());
boolean stringComplete = false; // whether the string is complete
const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 5, 4, 3};//connect to the column pinouts of the keypad
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop() {
keypad.getKey();
}
//take care of some special events
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
Serial.print(eKey);
switch (eKey) {
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}
void checkPassword() {
if (password.evaluate()) {
Serial.println("Success");
//Add code to run if it works
password.reset();
} else {
Serial.println("Wrong");
Serial.println(inString.toInt());
//add code to run if it did not work
}
{
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string,
// then the string's value:
if (inChar == '*') {// If carriage return
Serial.print("PassCode:");
Serial.println(inString.toInt());
stringComplete = true;
if (inChar == 'C') {
inString = "";
}
if (stringComplete) {
Serial.print("Pin Set To : ");
Serial.println(inString.toInt());
password.reset();
password.set(inString.toInt());
Password password = Password(inString.toInt());
inString = "";
}
}
}
}
}
Call me thick, I am really finding it difficult to get my head round a char array.
How do I set up a char array to expect 6 digits starting # and ending * "#1234*"
//* is to validate password
//# is to reset password attempt
/////////////////////////////////////////////////////////////////
#include <Password.h> //http://www.arduino.cc/playground/uploads/Code/Password.zip
#include <Keypad.h> //http://www.arduino.cc/playground/uploads/Code/Keypad.zip
String inputString = ""; // a string to hold incoming data
String inString = "";
Password password = Password(inString.toCharArray());
boolean stringComplete = false; // whether the string is complete
const byte ROWS = 4; // Four rows
const byte COLS = 4; // columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {10, 9, 8, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 5, 4, 3};//connect to the column pinouts of the keypad
// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup() {
Serial.begin(9600);
keypad.addEventListener(keypadEvent); //add an event listener for this keypad
}
void loop() {
keypad.getKey();
}
//take care of some special events
void keypadEvent(KeypadEvent eKey) {
switch (keypad.getState()) {
case PRESSED:
Serial.print(eKey);
switch (eKey) {
case '*': checkPassword(); break;
case '#': password.reset(); break;
default: password.append(eKey);
}
}
}
void checkPassword() {
if (password.evaluate()) {
Serial.println("Success");
//Add code to run if it works
password.reset();
} else {
Serial.println("Wrong");
Serial.println(inString.toCharArray());
//add code to run if it did not work
}
{
// Read serial input:
while (Serial.available() > 0) {
int inChar = Serial.read();
if (isDigit(inChar)) {
// convert the incoming byte to a char
// and add it to the string:
inString += (char)inChar;
}
// if you get a newline, print the string,
// then the string's value:
if (inChar == '#') {
inString = "";
if (inChar == '*') { // If carriage return
stringComplete = true;
}
if (stringComplete) {
Serial.print("Pin Set To : ");
Serial.println(inString.toCharArray());
password.reset();
password.set(inString.toCharArray());
Password password = Password(inString.toCharArray());
//inString = "";
}
}
}
}
}
[/*
||
|| @file Password.cpp
|| @version 1.2
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| 4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.com
|| Now v1.2 Arduino IDE v1.0 With BAckwards compatibility
||
|| @description
|| | Handle passwords easily
|| #
||
|| @license
|| | This library is free software; you can redistribute it and/or
|| | modify it under the terms of the GNU Lesser General Public
|| | License as published by the Free Software Foundation; version
|| | 2.1 of the License.
|| |
|| | This library is distributed in the hope that it will be useful,
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|| | Lesser General Public License for more details.
|| |
|| | You should have received a copy of the GNU Lesser General Public
|| | License along with this library; if not, write to the Free Software
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|| #
||
*/
#include "Password.h"
//construct object in memory, set all variables
Password::Password(char* pass){
set( pass );
reset();
}
//set the password
void Password::set(char* pass){
target = pass;
}
//evaluate a string, is it equal to the password?
bool Password::is(char* pass){
byte i=0;
while (*pass && i<MAX_PASSWORD_LENGTH){
guess[i] = pass[i];
i++;
}
return evaluate();
}
//append a char to the guessed password
bool Password::append(char character){
if (currentIndex+1==MAX_PASSWORD_LENGTH){
return false;
}else{
guess[currentIndex++] = character;
guess[currentIndex] = STRING_TERMINATOR; //ensure a valid c string
}
return true;
}
//reset the guessed password, one can guess again
void Password::reset(){
currentIndex = 0;
guess[currentIndex] = STRING_TERMINATOR;
}
//is the current guessed password equal to the target password?
bool Password::evaluate(){
char pass = target[0];
char guessed = guess[0];
for (byte i=1; i<MAX_PASSWORD_LENGTH; i++){
//check if guessed char is equal to the password char
if (pass==STRING_TERMINATOR && guessed==STRING_TERMINATOR){
return true; //both strings ended and all previous characters are equal
}else if (pass!=guessed || pass==STRING_TERMINATOR || guessed==STRING_TERMINATOR){
return false; //difference OR end of string has been reached
}
//read next char
pass = target[i];
guessed = guess[i];
}
return false; //a 'true' condition has not been met
}
//set password using operator =
Password &Password::operator=(char* pass){
set( pass );
return *this;
}
//test password using ==
bool Password::operator==(char* pass){
return is( pass );
}
//test password using !=
bool Password::operator!=(char* pass){
return !is( pass );
}
//append to currently guessed password using operator <<
Password &Password::operator<<(char character){
append( character );
return *this;
}
/*
|| @changelog
|| | 2009-06-17 - Alexander Brevig : Added assignment operator =, equality operators == != and insertion operator <<
|| | 2009-06-17 - Alexander Brevig : Initial Release
|| #
*//code]
and
Password.h
[code]/*
||
|| @file Password.h
|| @version 1.2
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| 4/5/2012 Updates Nathan Sobieck: Nathan@Sobisource.com
|| Now v1.2 Arduino IDE v1.0 With BAckwards compatibility
||
||
||
|| @description
|| | Handle passwords easily
|| #
||
|| @license
|| | This library is free software; you can redistribute it and/or
|| | modify it under the terms of the GNU Lesser General Public
|| | License as published by the Free Software Foundation; version
|| | 2.1 of the License.
|| |
|| | This library is distributed in the hope that it will be useful,
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|| | Lesser General Public License for more details.
|| |
|| | You should have received a copy of the GNU Lesser General Public
|| | License along with this library; if not, write to the Free Software
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|| #
||
*/
#ifndef PASSWORD_H
#define PASSWORD_H
// Arduino versioning.
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h" // for digitalRead, digitalWrite, etc
#else
#include "WProgram.h"
#endif
#define MAX_PASSWORD_LENGTH 20
#define STRING_TERMINATOR '\0'
class Password {
public:
Password(char* pass);
void set(char* pass);
bool is(char* pass);
bool append(char character);
void reset();
bool evaluate();
//char* getPassword();
//char* getGuess();
//operators
Password &operator=(char* pass);
bool operator==(char* pass);
bool operator!=(char* pass);
Password &operator<<(char character);
private:
char* target;
char guess[ MAX_PASSWORD_LENGTH ];
byte currentIndex;
};
#endif
/*
|| @changelog
|| | 1.1 2009-06-17 - Alexander Brevig : Added assignment operator =, equality operators == != and insertion operator <<
|| | 1.0 2009-06-17 - Alexander Brevig : Initial Release
|| #
*/
How do I set up a char array to expect 6 digits starting # and ending * "#1234*"
char someName[8];
What you expect to store in the array is completely irrelevant.
String inputString = ""; // a string to hold incoming data
String inString = "";
Password password = Password(inString.toCharArray());
Your current password is an empty string. Not very secure.
void loop() {
keypad.getKey();
}
Why do you bother reading the key that is pressed when you don't give a shit what key is pressed?
Reading serial data in the function to check the password is silly. You need to do that in setup() or loop().
The password library expects a char array that contains the valid password. If you change the password, the password library does NOT make a copy of the new password. It simply says "hey, the new password is over here". "Over here" being the current string wrapped by the String class is a REALLY bad idea.