hello,
currently using mfrc522 component connect smartsystem developing. have shortened code as possible , looks this:
this code works when use in new project. reads , prints uid of card. problem when "copy paste" other project, stops working after 1 use. can read 1 card, doesn't work anymore. project has keypad system. keypad system works letting first create code, using code unlock , lock unit. want do, user have option of using rfid-card unlock/lock (which easy enough), normal keypad. code troubling me below:
i using arduino mega, , keypad not connected it.
currently using mfrc522 component connect smartsystem developing. have shortened code as possible , looks this:
code: [select]
#include <spi.h>
#include <mfrc522.h>
#define ss_pin 53
#define rst_pin 5
mfrc522 mfrc522(ss_pin, rst_pin); // create mfrc522 instance.
void setup() {
serial.begin(9600); // initialize serial communications pc
spi.begin(); // init spi bus
mfrc522.pcd_init(); // init mfrc522 card
}
void loop() {
// new cards, , select 1 if present
if ( ! mfrc522.picc_isnewcardpresent() || ! mfrc522.picc_readcardserial() ) {
delay(50);
return;
}
// dump uid
serial.print(f("card uid:"));
(byte = 0; < mfrc522.uid.size; i++) {
serial.print(mfrc522.uid.uidbyte[i] < 0x10 ? " 0" : " ");
serial.print(mfrc522.uid.uidbyte[i], dec);
}
serial.println();
}this code works when use in new project. reads , prints uid of card. problem when "copy paste" other project, stops working after 1 use. can read 1 card, doesn't work anymore. project has keypad system. keypad system works letting first create code, using code unlock , lock unit. want do, user have option of using rfid-card unlock/lock (which easy enough), normal keypad. code troubling me below:
code: [select]
#include <keypad.h>
#include <spi.h>
#include <mfrc522.h>
//set-up mfrc-chippen
#define ss_pin 53
#define rst_pin 5
mfrc522 mfrc522(ss_pin, rst_pin);
const byte numrows = 4;
const byte numcols = 4;
char keymap[numrows][numcols]=
{
{'1', '2', '3', 'a'},
{'4', '5', '6', 'b'},
{'7', '8', '9', 'c'},
{'*', '0', '#', 'd'}
};
//code shows the keypad connections arduino terminals
byte rowpins[numrows] = {9,8,7,6}; //rows 0 3
byte colpins[numcols]= {5,4,3,2}; //columns 0 3
//initializes instance of keypad class
keypad mykeypad= keypad(makekeymap(keymap), rowpins, colpins, numrows, numcols);
void setup() {
serial.begin(9600);
spi.begin(); // init spi bus
mfrc522.pcd_init(); // init mfrc522 card
}
void loop() {
// new cards, , select 1 if present
if ( mfrc522.picc_isnewcardpresent() || mfrc522.picc_readcardserial() ) {
serial.print(f("card uid:"));
for (byte = 0; < mfrc522.uid.size; i++) {
serial.print(mfrc522.uid.uidbyte[i] < 0x10 ? " 0" : " ");
serial.print(mfrc522.uid.uidbyte[i], hex);
}
serial.println();
}
char keypressed = mykeypad.getkey();
static string passwordchosen = "";
static string keycodegiven = "";
static bool passsetup = false;
static bool alarmuser = false;
string chipuid = "bla bla";
if(!passsetup){
if(keypressed == '#'){
serial.print("chosen:" + passwordchosen);
passsetup = true;
}
else if(keypressed == '*'){
passwordchosen = "";
serial.print("reset pw");
}
else if(keypressed == 'a'){
// nothing
}
else if(keypressed == 'b'){
// nothing
}
else if(keypressed == 'c'){
// nothing
}
else if(keypressed == 'd'){
// nothing
}
else if (keypressed != no_key){
passwordchosen += string(keypressed);
serial.print("pw:" + passwordchosen);
}
}
else if (passsetup){
if(keypressed == 'a'){
serial.println("next page");
serial.println(keycodegiven);
}
else if(keypressed == 'b'){
serial.println("previous page");
serial.println(keycodegiven);
}
else if(keypressed == 'c'){
// nothing
}
else if(keypressed == 'd'){
// nothing
}
else if(keypressed == '*')
{
serial.println("reset");
keycodegiven = "";
serial.println(keycodegiven);
}
else if(keypressed == '#')
{
serial.println("enter");
if(keycodegiven == passwordchosen && alarmuser == false){
serial.println("correct pw, may pass!");
alarmuser = true;
keycodegiven = "";
}
else if(keycodegiven == passwordchosen && alarmuser == true){
serial.println("correct pw, locked!");
alarmuser = false;
keycodegiven = "";
}
else{
serial.println("error, wrong password!");
keycodegiven = "";
}
}
else if (keypressed != no_key)
{
serial.println(keypressed);
keycodegiven += string(keypressed);
serial.println(keycodegiven);
}
}
}i using arduino mega, , keypad not connected it.
you're using pin 5 both rst_pin , 1 of keypad pins... don't that.
also, using pin 50-something chip select? mega you're using, right? (if not, pin doesn't exist - compiler not able warn in case, either.)
also, using pin 50-something chip select? mega you're using, right? (if not, pin doesn't exist - compiler not able warn in case, either.)
Arduino Forum > Using Arduino > Programming Questions > MFRC522 code will not work in one project, but does in another
arduino
Comments
Post a Comment