###### tags: `Arduino` `Arduino 實作` # NRF Nano接收端程式 ```cpp= #include<Streaming.h> #include <SPI.h> #include "RF24.h" RF24 rf24(9, 10); // CE腳, CSN腳 const byte addr[] = "1Node"; const byte pipe = 1; // 指定通道編號 const int dtpin[8] = {2,3,4,5,6,7,A0,A1}; //shift data to mega 2560 int dtread[4] = {0,0,0,0}; //nrf read int dtout[8] = {0,0,0,0,0,0,0,0}; //shift data void setup() { Serial.begin(9600); rf24.begin(); rf24.setChannel(83); // 設定頻道編號 rf24.setPALevel(RF24_PA_MAX); // 設定廣播功率 rf24.setDataRate(RF24_2MBPS); // 設定傳輸速率 rf24.openReadingPipe(pipe, addr); // 開啟通道和位址 rf24.startListening(); // 開始監聽無線廣播 rf24.printDetails(); // 顯示現在狀態 Serial.println("nRF24L01 ready!"); for(int i=0;i<8;i++){ pinMode(dtpin[i],OUTPUT); } } void loop() { if(rf24.available(&pipe)) { rf24.read(&dtread, sizeof(dtread)); control(dtread); } for(int i=0;i<4;i++){ Serial << dtread[i] << " "; // 顯示訊息內容 if(i==7)Serial << endl; } Serial << endl; delay(10); } void control(int a[4]) { if(a[0]>=200){ motR(1); }else if(a[0]<=-200){ motR(-1); }else{ motR(0); } //-------------------------------------------- if(a[2]>=200){ motL(1); }else if(a[2]<=-200){ motL(-1); }else{ motL(0); } } void motR(int a){ if(a==1){ dtout[0] = 1; dtout[1] = 0; dtout[2] = 1; dtout[3] = 0; }else if(a==0){ dtout[0] = 0; dtout[1] = 0; dtout[2] = 0; dtout[3] = 0; }else if(a==-1){ dtout[0] = 0; dtout[1] = 1; dtout[2] = 0; dtout[3] = 1; } } void motL(int a){ if(a==1){ dtout[4] = 1; dtout[5] = 0; dtout[6] = 1; dtout[7] = 0; }else if(a==0){ dtout[4] = 0; dtout[5] = 0; dtout[6] = 0; dtout[7] = 0; }else if(a==-1){ dtout[4] = 0; dtout[5] = 1; dtout[6] = 0; dtout[7] = 1; } } ```