wiki:wade/embedded/arduino/example/example_8_2_Xbee_ClientB_broadcast

回到 Arduino

/*
 * Client 從廣播接收訊號,[node][val]
 * B1 : 當 B node 收到後,會閃 LED 燈 1 次並回傳 1
 * B2 : 當 B node 收到後,會閃 LED 燈 2 次並回傳 2
 * C1 : 當 C node 收到後,會閃 LED 燈 1 次並回傳 1
 */
#include <NewSoftSerial.h> 
#define node_A_sh 13A200
#define node_A_sl 403D0190
#define node_B_sl 403D018A
#define node_C_sl 403D01D0

// set pin 9 as RX 
uint8_t ssRX = 9;
// set pin 10 as TX
uint8_t ssTX = 10;
// enable soft serial nss
NewSoftSerial nss(ssRX, ssTX);

void setup()
{
  pinMode(13, OUTPUT);
  Serial.begin(9600);
  // start soft serial
  nss.begin(9600);
}

int data;
void loop()
{
  if(nss.available())
  {
    data = nss.read();
    switch(data)
    {
      case 'A':
        while(1)
        {
          if(nss.available())
          {
            nss.read();
            break;
          }
        }
        break;
      case 'B':
              while (1)
        {
          if(nss.available())
          {
            data = nss.read();
            Serial.println("I receive ");
            Serial.println(data, HEX);
            for (int i = 1; i <= data; i++)
            {
              digitalWrite(13, HIGH);
              delay(200);
              digitalWrite(13, LOW);
              delay(200);
            }
            nss.print("B");
            delay(1000);
            break;
          }
        }
        break;
    }
  }
}

Last modified 15 years ago Last modified on Mar 5, 2010, 3:16:32 PM