Advertisement
Coder_228

Untitled

Jan 19th, 2024
2,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | Software | 0 0
  1. /*
  2.  * See documentation at https://nRF24.github.io/RF24
  3.  * See License information at root directory of this library
  4.  * Author: Brendan Doherty (2bndy5)
  5.  */
  6.  
  7. /**
  8.  * A simple example of sending data from 1 nRF24L01 transceiver to another.
  9.  *
  10.  * This example was written to be used on 2 devices acting as "nodes".
  11.  * Use the Serial Monitor to change each node's behavior.
  12.  */
  13. #include <SPI.h>
  14. #include "printf.h"
  15. #include "RF24.h"
  16.  
  17. #define CE_PIN D4
  18. #define CSN_PIN D2
  19. // instantiate an object for the nRF24L01 transceiver
  20. RF24 radio(CE_PIN, CSN_PIN);
  21.  
  22. // Let these addresses be used for the pair
  23. uint8_t address[][6] = { "1Node", "2Node" };
  24. // It is very helpful to think of an address as a path instead of as
  25. // an identifying device destination
  26.  
  27. // to use different addresses on a pair of radios, we need a variable to
  28. // uniquely identify which address this radio will use to transmit
  29. bool radioNumber = 1;  // 0 uses address[0] to transmit, 1 uses address[1] to transmit
  30.  
  31. // Used to control whether this node is sending or receiving
  32. bool role = false;  // true = TX role, false = RX role
  33.  
  34. // For this example, we'll be using a payload containing
  35. // a single float number that will be incremented
  36. // on every successful transmission
  37. float payload = 0.0;
  38.  
  39. void setup() {
  40.  
  41.   Serial.begin(115200);
  42.   while (!Serial) {
  43.     // some boards need to wait to ensure access to serial over USB
  44.   }
  45.  
  46.   // initialize the transceiver on the SPI bus
  47.   if (!radio.begin()) {
  48.     Serial.println(F("radio hardware is not responding!!"));
  49.     while (1) {}  // hold in infinite loop
  50.   }
  51.  
  52.   // print example's introductory prompt
  53.   Serial.println(F("RF24/examples/GettingStarted"));
  54.  
  55.   // To set the radioNumber via the Serial monitor on startup
  56.   Serial.println(F("Which radio is this? Enter '0' or '1'. Defaults to '0'"));
  57.   while (!Serial.available()) {
  58.     // wait for user input
  59.   }
  60.   char input = Serial.parseInt();
  61.   radioNumber = input == 1;
  62.   Serial.print(F("radioNumber = "));
  63.   Serial.println((int)radioNumber);
  64.  
  65.   // role variable is hardcoded to RX behavior, inform the user of this
  66.   Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  67.  
  68.   // Set the PA Level low to try preventing power supply related problems
  69.   // because these examples are likely run with nodes in close proximity to
  70.   // each other.
  71.   radio.setChannel(0x76);  // 108 (base 10) or 0x6C (base 16) - 2.476GHz
  72.   radio.setAutoAck(false);  
  73.   radio.setDataRate(RF24_1MBPS);
  74.   radio.setPALevel(RF24_PA_LOW);  // RF24_PA_MAX is default.
  75.  
  76.   // save on transmission time by setting the radio to only transmit the
  77.   // number of bytes we need to transmit a float
  78.   radio.setPayloadSize(sizeof(payload));  // float datatype occupies 4 bytes
  79.  
  80.   // set the TX address of the RX node into the TX pipe
  81.   radio.openWritingPipe(address[radioNumber]);  // always uses pipe 0
  82.  
  83.   // set the RX address of the TX node into a RX pipe
  84.   radio.openReadingPipe(1, address[!radioNumber]);  // using pipe 1
  85.  
  86.   // additional setup specific to the node's role
  87.   if (role) {
  88.     radio.stopListening();  // put radio in TX mode
  89.   } else {
  90.     radio.startListening();  // put radio in RX mode
  91.   }
  92.  
  93.   // For debugging info
  94.   // printf_begin();             // needed only once for printing details
  95.   // radio.printDetails();       // (smaller) function that prints raw register values
  96.    radio.printPrettyDetails(); // (larger) function that prints human readable data
  97.  
  98. }  // setup
  99.  
  100. void loop() {
  101.  
  102.   if (role) {
  103.     // This device is a TX node
  104.  
  105.     unsigned long start_timer = micros();                // start the timer
  106.     bool report = radio.write(&payload, sizeof(float));  // transmit & save the report
  107.     unsigned long end_timer = micros();                  // end the timer
  108.  
  109.     if (report) {
  110.       Serial.print(F("Transmission successful! "));  // payload was delivered
  111.       Serial.print(F("Time to transmit = "));
  112.       Serial.print(end_timer - start_timer);  // print the timer result
  113.       Serial.print(F(" us. Sent: "));
  114.       Serial.println(payload);  // print payload sent
  115.       payload += 0.01;          // increment float payload
  116.     } else {
  117.       Serial.println(F("Transmission failed or timed out"));  // payload was not delivered
  118.     }
  119.  
  120.     // to make this example readable in the serial monitor
  121.     delay(1000);  // slow transmissions down by 1 second
  122.  
  123.   } else {
  124.     // This device is a RX node
  125.  
  126.     uint8_t pipe;
  127.     if (radio.available(&pipe)) {              // is there a payload? get the pipe number that recieved it
  128.       uint8_t bytes = radio.getPayloadSize();  // get the size of the payload
  129.       radio.read(&payload, bytes);             // fetch payload from FIFO
  130.       Serial.print(F("Received "));
  131.       Serial.print(bytes);  // print the size of the payload
  132.       Serial.print(F(" bytes on pipe "));
  133.       Serial.print(pipe);  // print the pipe number
  134.       Serial.print(F(": "));
  135.       Serial.println(payload);  // print the payload's value
  136.     }
  137.   }  // role
  138.  
  139.   if (Serial.available()) {
  140.     // change the role via the serial monitor
  141.  
  142.     char c = toupper(Serial.read());
  143.     if (c == 'T' && !role) {
  144.       // Become the TX node
  145.  
  146.       role = true;
  147.       Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
  148.       radio.stopListening();
  149.  
  150.     } else if (c == 'R' && role) {
  151.       // Become the RX node
  152.  
  153.       role = false;
  154.       Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
  155.       radio.startListening();
  156.     }
  157.   }
  158.  
  159. }  // loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement