回到 Arduino
/*
* Server 廣播發出訊號,[node][val]
* B1 : 當 B node 收到後,會閃 LED 燈 1 次並回傳 1
* B2 : 當 B node 收到後,會閃 LED 燈 2 次並回傳 2
* C1 : 當 C node 收到後,會閃 LED 燈 1 次並回傳 1
* 當 Server 收到 node A 或 node B 回應後,再指定下個工作予其執行。
*/
#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);
int data;
int nodeACount=1;
int nodeBCount=1;
void setup()
{
Serial.begin(9600);
// start soft serial
nss.begin(9600);
nss.print("A");
nss.print(byte(nodeACount));
nss.print("B");
nss.print(byte(nodeBCount));
}
void loop()
{
if(nss.available())
{
data = nss.read();
switch(data)
{
case 'A':
nodeACount++;
nodeACount %= 6;
Serial.print("nodeACount = ");
Serial.println(nodeACount);
nss.print("A");
nss.print(byte(nodeACount));
break;
case 'B':
nodeBCount++;
nodeBCount %= 6;
Serial.print("nodeBCount = ");
Serial.println(nodeBCount);
nss.print("B");
nss.print(byte(nodeBCount));
break;
}
}
}