课程需要,打算用Arduino uno做一个宿舍智能锁。可以多种方式解锁:4*4矩阵键盘,读卡器(RC522),指纹识别等,开锁动力是25KG舵机。我先编写了一个程序,实现了4*4矩阵键盘解锁。又写了一个程序(大部分是借鉴的CSDN上面的),实现了RC522读卡解释。然后把两个程序合并在一起,就出错了。。
编译,上传都没问题,键盘也没问题,就是读卡时有问题,只能开一次,实在不知道错哪里了,求教各位大神,代码如下:
#include<RFID.h>
#include<SPI.h>
#include <Keypad.h>
#include <EEPROM.h>
#include <Servo.h>
String RC;
RFID rfid(10,5); //10 - 读卡器CS引脚、5 - 读卡器RST引脚
unsigned char RC_size;
unsigned char blockAddr; //选择操作的块地址0~63
unsigned char i,tmp;
unsigned char status;
unsigned char str[MAX_LEN]; ////MAX_LEN为16,数组最大长度
Servo myservo;
char * card[]={"d6c7f0f716","60f1d4ad6"};
char * name[]={"zhao yan chen","dada"};
String RFID_readcardnum() {
rfid.readCardSerial();
String stringserNum=String(rfid.serNum[0], HEX)+String(rfid.serNum[1], HEX)+String(rfid.serNum[2], HEX)+String(rfid.serNum[3], HEX)+String(rfid.serNum[4], HEX);
//选卡,返回卡容量(锁定卡片,防止多次读写)
rfid.selectTag(rfid.serNum);
return stringserNum;
}
//4字节卡序列号,第5字节为校验字节
//新扇区A密码,16个扇区,每个扇区密码6Byte
unsigned char sectorNewKeyA[16][16] = {
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xff,0x07,0x80,0x69, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},};
unsigned char serNum[5];
//写卡数据
//原扇区A密码,16个扇区,每个扇区密码6Byte
unsigned char sectorKeyA[16][16] = {
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},};
int pos = 0;
char* secretCode = "1234";
int position = 0;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {10,11,12,13};
byte colPins[cols] = {2,3,4};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 6;
int greenPin = 7;
int servoPin = 8;
void setup()
{
RC = "";
Serial.begin(9600);
SPI.begin();
rfid.init();
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
myservo.attach(servoPin);
myservo.write(0);
loadCode();
flash();
lock();
//Serial.begin(9600);
while(!Serial);
Serial.print("Code is: "); Serial.println(secretCode);
Serial.println("Change code: cNNNN");
Serial.println("Unloack: u");
Serial.println("Lock: l");
}
void loop()
{
if ( rfid.isCard() )
{
RC = RFID_readcardnum();
//Serial.println(RC);
for (int i = (1); i <= (sizeof(card)/sizeof(card[0])); i = i + (1))
{
if (RC == card[(int)(i - 1)])
{
Serial.println(String("Hello !") + String(name[(int)(i - 1)]));
for(pos = 0;pos<180;pos+=1)
{
myservo.write(pos);
delay(15);
}
for(pos = 180;pos>0;pos-=1)
{
myservo.write(pos);
delay(15);
}
}
}
}
else if (Serial.available())
{
char c = Serial.read();
if (c == 'u')
{
unlock();
}
if (c == 'l')
{
lock();
}
if (c == 'c')
{
getNewCode();
}
}
else
{
char key = keypad.getKey();
if (key == '#')
{
lock();
}
if (key == secretCode[position])
{
position ++;
}
else if (key != 0)
{
lock();
}
if (position == 4)
{
unlock();
}
delay(100);
}
}
void lock()
{
position = 0;
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
Serial.println("LOCKED");
}
void unlock()
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
for(pos = 0; pos < 180; pos += 1) // 从0度到180度运动
{ // 每次步进一度
myservo.write(pos); // 指定舵机转向的角度
delay(15); // 等待15ms让舵机到达指定位置
}
delay(2000);
for(pos = 180; pos>=1; pos-=1) //从180度到0度运动
{
myservo.write(pos); // 指定舵机转向的角度
delay(15); // 等待15ms让舵机到达指定位置
}
Serial.println("UN-LOCKED");
lock();
}
void getNewCode()
{
for (int s = 0; s < 4; s++ )
{
char ch = Serial.read();
secretCode[s] = ch;
}
saveCode();
flash();flash();
Serial.print("Code changed to: "); Serial.println(secretCode);
}
void loadCode()
{
if (EEPROM.read(0) == 1)
{
secretCode[0] = EEPROM.read(1);
secretCode[1] = EEPROM.read(2);
secretCode[2] = EEPROM.read(3);
secretCode[3] = EEPROM.read(4);
}
}
void saveCode()
{
EEPROM.write(1, secretCode[0]);
EEPROM.write(2, secretCode[1]);
EEPROM.write(3, secretCode[2]);
EEPROM.write(4, secretCode[3]);
EEPROM.write(0, 1);
}
void flash()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, HIGH);
delay(500);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
}
|