rozman50

NRF24L01 - MASTER

Feb 18th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.85 KB | None | 0 0
  1. // SimpleTx - the master or the transmitter
  2.  
  3. #include <SPI.h>
  4. #include <nRF24L01.h>
  5. #include <RF24.h>
  6. #define CE_PIN   9
  7. #define CSN_PIN 10
  8.  
  9. const byte slaveAddress[][5] = {{'R', 'x', 'A', 'A', 'A'}, {'R', 'x', 'A', 'A', 'B'},
  10.     {'R', 'x', 'A', 'A', 'C'}, {'R', 'x', 'A', 'A', 'D'}};
  11.  
  12. RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
  13.  
  14. char dataToSend[10] = "Message 0";
  15. char txNum = '0';
  16.  
  17. unsigned long currentMillis;
  18. unsigned long prevMillis;
  19. unsigned long txIntervalMillis = 1000; // send once per second
  20.  
  21.  
  22. void setup() {
  23.  
  24.     Serial.begin(9600);
  25.     Serial.println("SimpleTx Starting");
  26.     radio.begin();
  27.     radio.setDataRate( RF24_250KBPS );
  28.     radio.setRetries(3, 5); // delay, count
  29.     int numSlaves=4;
  30.  
  31.  
  32.     radio.enableDynamicPayloads() ;
  33.     radio.setAutoAck( true ) ;
  34. }
  35.  
  36. //====================
  37.  
  38. void loop() {
  39.     currentMillis = millis();
  40.     if (currentMillis - prevMillis >= txIntervalMillis) {
  41.     send();
  42.     prevMillis = millis();
  43.     }
  44. }
  45.  
  46. //====================
  47.  
  48. void send() {
  49.  
  50.     bool rslt;
  51.     for (byte n = 1; n < 3; n++) {
  52.         radio.openWritingPipe(slaveAddress[n]);
  53.         rslt = radio.write( &dataToSend, sizeof(dataToSend) );
  54.             // Always use sizeof() as it gives the size as the number of bytes.
  55.             // For example if dataToSend was an int sizeof() would correctly return 2
  56.  
  57.         Serial.print("Data Sent to Slave ");
  58.         Serial,println(n);
  59.         Serial.print(dataToSend);
  60.         if (rslt) {
  61.         Serial.print("  Acknowledge received from Slave ");
  62.         Serial.println(n);
  63.         updateMessage();
  64.         }
  65.     }
  66.     else {
  67.     Serial.println("  Tx failed");
  68.     }
  69. }
  70.  
  71. //================
  72.  
  73. void updateMessage() {
  74.     // so you can see that new data is being sent
  75.     txNum += 1;
  76.     if (txNum > '9') {
  77.     txNum = '0';
  78.     }
  79.     dataToSend[8] = txNum;
  80. }
Add Comment
Please, Sign In to add comment