| 4 | /* |
| 5 | * Server 廣播發出訊號,[node][val] |
| 6 | * B1 : 當 B node 收到後,會閃 LED 燈 1 次並回傳 1 |
| 7 | * B2 : 當 B node 收到後,會閃 LED 燈 2 次並回傳 2 |
| 8 | * C1 : 當 C node 收到後,會閃 LED 燈 1 次並回傳 1 |
| 9 | * 當 Server 收到 node A 或 node B 回應後,再指定下個工作予其執行。 |
| 10 | */ |
| 11 | #include <NewSoftSerial.h> |
| 12 | #define node_A_sh 13A200 |
| 13 | #define node_A_sl 403D0190 |
| 14 | #define node_B_sl 403D018A |
| 15 | #define node_C_sl 403D01D0 |
| 16 | |
| 17 | // set pin 9 as RX |
| 18 | uint8_t ssRX = 9; |
| 19 | // set pin 10 as TX |
| 20 | uint8_t ssTX = 10; |
| 21 | // enable soft serial nss |
| 22 | NewSoftSerial nss(ssRX, ssTX); |
| 23 | |
| 24 | int data; |
| 25 | int nodeACount=1; |
| 26 | int nodeBCount=1; |
| 27 | |
| 28 | void setup() |
| 29 | { |
| 30 | Serial.begin(9600); |
| 31 | // start soft serial |
| 32 | nss.begin(9600); |
| 33 | nss.print("A"); |
| 34 | nss.print(byte(nodeACount)); |
| 35 | nss.print("B"); |
| 36 | nss.print(byte(nodeBCount)); |
| 37 | } |
| 38 | |
| 39 | |
| 40 | void loop() |
| 41 | { |
| 42 | if(nss.available()) |
| 43 | { |
| 44 | data = nss.read(); |
| 45 | switch(data) |
| 46 | { |
| 47 | case 'A': |
| 48 | nodeACount++; |
| 49 | nodeACount %= 6; |
| 50 | Serial.print("nodeACount = "); |
| 51 | Serial.println(nodeACount); |
| 52 | nss.print("A"); |
| 53 | nss.print(byte(nodeACount)); |
| 54 | break; |
| 55 | case 'B': |
| 56 | nodeBCount++; |
| 57 | nodeBCount %= 6; |
| 58 | Serial.print("nodeBCount = "); |
| 59 | Serial.println(nodeBCount); |
| 60 | nss.print("B"); |
| 61 | nss.print(byte(nodeBCount)); |
| 62 | break; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | }}} |
| 68 | |