写了一个用RFID板刷卡开门的程序 然后再按碰撞传感器把门关上 但是接上电之后识别卡之后舵机旋转了90度之后会旋转回0度 所以按碰撞传感器也没有反应 请问这个应该怎么改才能刷卡之后停留在90度不自动转回 只有按下碰撞传感器才回回到0度?
- #include <SPI.h>
- #include <MFRC522.h>
- #include <Servo.h>
- #define id_0 0x09
- #define id_1 0x2F
- #define id_2 0x7E
- #define id_3 0xE7
- #define SS_PIN 53
- #define RST_PIN 49
- MFRC522 rfid(SS_PIN,RST_PIN);
- MFRC522::MIFARE_Key key;
- Servo servo;
- int crash = 7;
- int cnt = 0;
- int value = 0;
- void setup() {
- Serial.begin(9600);
- SPI.begin();
- rfid.PCD_Init();
- servo.attach(6);
- pinMode(crash,INPUT);
- for(byte i = 0;i < 6 ; i++){
- key.keyByte[i]=0xff;
- }
- Serial.println(F("Using the following key:"));
- printHex(key.keyByte,MFRC522::MF_KEY_SIZE);
- }
- void loop() {
-
- if(cnt == 1){
- servo.write(90);
- }else{
- servo.write(0);
- }
- delay(2000);
-
- if(digitalRead(crash)== 0){
- cnt=0;
- }
- if(! rfid.PICC_IsNewCardPresent())
- return;
- if(! rfid.PICC_ReadCardSerial())
- return;
- Serial.print("카드 ID :\n");
- printHex(rfid.uid.uidByte,rfid.uid.size);
- Serial.println("\n");
- if(rfid.uid.uidByte[0] == id_0 ||
- rfid.uid.uidByte[1] == id_1 ||
- rfid.uid.uidByte[2] == id_2 ||
- rfid.uid.uidByte[3] == id_3){
- cnt = 1;
- }
- }
- void printHex(byte *buffer,byte bufferSize){
- for(byte i = 0;i < bufferSize; i++){
- Serial.print(buffer[i] < 0X10 ? "0":" ");
- Serial.print(buffer[i],HEX);
- }
- }
复制代码
|