tungcheuk2 发表于 2014-4-12 18:51:59

新手求幫助。RFID門禁問題

http://img04.taobaocdn.com/imgextra/i4/178252798/T2taj4XttaXXXXXXXX-178252798.jpg

我是這個模塊,現在的問題是還沒解決屋內解鎖問題
我已有按鈕,但不會編寫下一步,求幫助

以下是我的arduino code



#include <EEPROM.h>
#define EEPROM_write(address, p) {int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) EEPROM.write(address+i, pp);}
#define EEPROM_read(address, p){int i = 0; byte *pp = (byte*)&(p);for(; i < sizeof(p); i++) pp=EEPROM.read(address+i);}

char val; // variable to receive data from the serial port
const int ENPin = 2;   // 定義使能管腳
int ENState = 1;//初始化使能狀態為高電平
// lock 1
int PWMA = 2;
int LED = 3;

unsigned long ID = 0;                  unsigned long IDR = 0;
unsigned long ID1 = 0;          unsigned long IDR1 = 0;
unsigned long ID2 = 0;          unsigned long IDR2 = 0;
unsigned long ID3 = 0;                   unsigned long IDR3 = 0;
unsigned long ID4 = 0;                   unsigned long IDR4 = 0;
unsigned long ID5 = 4241884143;                   unsigned long IDR5 = 0;


void setup()
{
Serial.begin(9600);
pinMode(ENPin, INPUT);
pinMode(PWMA,OUTPUT);
pinMode(LED, OUTPUT);
EEPROM_write(0, ID);
EEPROM_write(10, ID1);
EEPROM_write(20, ID2);
EEPROM_write(30, ID3);
EEPROM_write(40, ID4);
EEPROM_write(50, ID5);

EEPROM_read(0, IDR);
Serial.print("ID card1: ");
Serial.println(IDR);
EEPROM_read(10, IDR1);
Serial.print("ID card2: ");
Serial.println(IDR1);
EEPROM_read(20, IDR2);
Serial.print("ID card3: ");
Serial.println(IDR2);
EEPROM_read(30, IDR3);
Serial.print("ID card4: ");
Serial.println(IDR3);
EEPROM_read(40, IDR4);
Serial.print("ID card5: ");
Serial.println(IDR4);
EEPROM_read(50, IDR5);
Serial.print("ID card6: ");
Serial.println(IDR5);

}

void loop()
{
for(ENState = digitalRead(ENPin) ; ENState == LOW && Serial.available() > 0; ) //判斷使能端是否為低電平,是且串口有輸入,則讀入卡號
{      
    ID = ID * 10 + int(Serial.read()-'0'); //讀出卡號
    delay(2);                        //加個小延時,讓串口有充裕的時間讀出資料
}
if(ID != 0)             //如果ID記憶體的值不為0,則輸出該卡號
{
   Serial.println(ID);
      if(ID == IDR) Serial.println("welcome! TimYiu");   //判断是否与EEPROM中的卡号相匹配
         if(IDR==ID)
         {
             digitalWrite(LED, HIGH);   // set the LED on
             delay(2000);            // wait for a second
             digitalWrite(LED, LOW);    // set the LED off
             delay(1000);            // wait for a second
         }
   else if(ID == IDR1) Serial.println("welcome! Thomes");
          if(IDR1==ID)
          {
             digitalWrite(LED, HIGH);   // set the LED on
             delay(2000);            // wait for a second
             digitalWrite(LED, LOW);    // set the LED off
             delay(1000);            // wait for a second
         }
   else if(ID == IDR2) Serial.println("welcome! Koei");
         if(IDR2==ID)
          {
             digitalWrite(LED, HIGH);   // set the LED on
             delay(2000);            // wait for a second
             digitalWrite(LED, LOW);    // set the LED off
             delay(1000);            // wait for a second
         }
   else if(ID == IDR3) Serial.println("welcome! Wai");
          if(IDR3==ID)
          {
             digitalWrite(LED, HIGH);   // set the LED on
             delay(2000);            // wait for a second
             digitalWrite(LED, LOW);    // set the LED off
             delay(1000);            // wait for a second
         }
   else if(ID == IDR4) Serial.println("welcome! Mother");
          if(IDR4==ID)
          {
             digitalWrite(LED, HIGH);   // set the LED on
             delay(2000);            // wait for a second
             digitalWrite(LED, LOW);    // set the LED off
             delay(1000);            // wait for a second
         }
   else if(ID == IDR5) Serial.println("welcome! Father");
         if(IDR5==ID)
          {
          leftForw();// turn to left         
         }
      else Serial.println("sorry,you are not allowed.");
         ID = 0;                  //清0
      }

            }

             void leftForw(){
             if( Serial.available() )       // if data is available to read
            {
             val = Serial.read();}
             digitalWrite(PWMA, HIGH);   // set the LED on
             delay(2000);            // wait for a second
             digitalWrite(PWMA, LOW);    // set the LED off
             //delay(1000);   
             }

tungcheuk2 发表于 2014-4-13 22:25:40

求指教。。。。
页: [1]
查看完整版本: 新手求幫助。RFID門禁問題