本帖最後由 z159357z2000 於 2012-3-19 14:34 編輯  
 
我正在做一個專題 
是想要用RFID收到TAG的訊息用XBEE傳給出來 
我是要把BUTTON變成RFID 
請問程式是要怎麼合併了? 
 
這是我的程式碼 
XBEE: 
const intbuttonPin = 12; 
//按鍵引腳數量 
 const int ledPin= 
13; 
// LED引腳數量 
 // variableswill change: int buttonState= 0; 
// 變量讀取按鍵狀態 
 longpreviousMillis = 0; 
// 將存儲LED被更新的最後一次 
 // the followvariables is a long because the time, measured in miliseconds, // will quicklybecome a bigger number than can be stored in an int. long interval =1000; 
// interval at which toblink (milliseconds) void setup() {  
// initialize the LED pin as an output:
  
pinMode(ledPin, OUTPUT); 
  
// initialize the pushbutton pin as an input:
  
pinMode(buttonPin, INPUT); 
  
Serial.begin(9600); 
 } void loop(){  
// read the state of the pushbutton value:
  
buttonState = digitalRead(buttonPin);
  
// check if the pushbutton is pressed.
  
// if it is, the buttonState is HIGH:
  
if (buttonState == HIGH) { 
  
// turn LED on: 
  
digitalWrite(ledPin, HIGH); 
  
Serial.println("apple");
  
delay(300); 
// so won't send msg too many times
  
} 
  
else {
  
// turn LED off:
  
digitalWrite(ledPin, LOW); 
  
}
  
// Simply to keep xbee alive
  
unsigned long currentMillis = millis();
  
if(currentMillis - previousMillis >interval) {
  
// save the last time you blinked the LED 
  
previousMillis = currentMillis; 
  
Serial.println("sync");
  
}
 }  
 
RFID: 
int val =0; char code[10]; int bytesread =0;  
void setup() {  
Serial.begin(2400);// 將Serial RX pin(digital 0) 設為2400bps pinMode(2,OUTPUT);// 將digital pin 2設為輸出,以便傳送訊號給RFID reader }  
 voidloop() {  digitalWrite(0,LOW);                 // 
啟動RFID reader  if(Serial.available()> 0) {          // 
假如reader有資料讀入 
    if((val = Serial.read()) == 10) {   // 
檢查標頭是否為10      bytesread = 0;      while(bytesread<10){                     if( Serial.available() > 0) {          val = Serial.read();          if((val == 10)||(val == 13)) { //假如讀到表示標頭的10或結束的13                       break;                      // 
停止讀取 
          }          code[bytesread] = val;         //紀錄讀入的值 
          bytesread++;                  //準備讀取下一個位元 
        }      }      if(bytesread == 10){             // 
如果十個號碼皆讀取成功 
        Serial.print("TAG code is: ");          Serial.println(code);           // 
顯示TAG號碼 
      }      bytesread = 0;      digitalWrite(0,HIGH);            // 
關閉RFID reader      delay(500);                         }  } }  |