Guest User

Untitled

a guest
Jan 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. /*
  2. * Getting Started example sketch for nRF24L01+ radios
  3. * This is a very basic example of how to send data from one node to another
  4. * Updated: Dec 2014 by TMRh20
  5. */
  6.  
  7. #include <SPI.h>
  8. #include "RF24.h"
  9.  
  10. /****************** User Config ***************************/
  11. /*** Set this radio as radio number 0 or 1 ***/
  12. bool radioNumber = 1;
  13.  
  14. /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
  15. RF24 radio(7,8);
  16. /**********************************************************/
  17.  
  18. byte addresses[][6] = {"1Node","2Node"};
  19.  
  20. // Used to control whether this node is sending or receiving
  21. bool role = 0;
  22.  
  23. void setup() {
  24. Serial.begin(115200);
  25. Serial.println(F("RF24/examples/GettingStarted"));
  26. Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  27.  
  28. radio.begin();
  29.  
  30. // Set the PA Level low to prevent power supply related issues since this is a
  31. // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  32. radio.setPALevel(RF24_PA_LOW);
  33.  
  34. // Open a writing and reading pipe on each radio, with opposite addresses
  35. if(radioNumber){
  36. radio.openWritingPipe(addresses[1]);
  37. radio.openReadingPipe(1,addresses[0]);
  38. }else{
  39. radio.openWritingPipe(addresses[0]);
  40. radio.openReadingPipe(1,addresses[1]);
  41. }
  42.  
  43. // Start the radio listening for data
  44. radio.startListening();
  45. }
  46.  
  47. void loop() {
  48.  
  49.  
  50. /****************** Ping Out Role ***************************/
  51. if (role == 1) {
  52.  
  53. radio.stopListening(); // First, stop listening so we can talk.
  54.  
  55.  
  56. Serial.println(F("Now sending"));
  57.  
  58. unsigned long start_time = micros(); // Take the time, and send it. This will block until complete
  59. if (!radio.write( &start_time, sizeof(unsigned long) )){
  60. Serial.println(F("failed"));
  61. }
  62.  
  63. radio.startListening(); // Now, continue listening
  64.  
  65. unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds
  66. boolean timeout = false; // Set up a variable to indicate if a response was received or not
  67.  
  68. while ( ! radio.available() ){ // While nothing is received
  69. if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
  70. timeout = true;
  71. break;
  72. }
  73. }
  74.  
  75. if ( timeout ){ // Describe the results
  76. Serial.println(F("Failed, response timed out."));
  77. }else{
  78. unsigned long got_time; // Grab the response, compare, and send to debugging spew
  79. radio.read( &got_time, sizeof(unsigned long) );
  80. unsigned long end_time = micros();
  81.  
  82. // Spew it
  83. Serial.print(F("Sent "));
  84. Serial.print(start_time);
  85. Serial.print(F(", Got response "));
  86. Serial.print(got_time);
  87. Serial.print(F(", Round-trip delay "));
  88. Serial.print(end_time-start_time);
  89. Serial.println(F(" microseconds"));
  90. }
  91.  
  92. // Try again 1s later
  93. delay(1000);
  94. }
  95.  
  96.  
  97.  
  98. /****************** Pong Back Role ***************************/
  99.  
  100. if ( role == 0 )
  101. {
  102. unsigned long got_time;
  103.  
  104. if( radio.available()){
  105. // Variable for the received timestamp
  106. while (radio.available()) { // While there is data ready
  107. radio.read( &got_time, sizeof(unsigned long) ); // Get the payload
  108. }
  109.  
  110. radio.stopListening(); // First, stop listening so we can talk
  111. radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
  112. radio.startListening(); // Now, resume listening so we catch the next packets.
  113. Serial.print(F("Sent response "));
  114. Serial.println(got_time);
  115. }
  116. }
  117.  
  118.  
  119.  
  120.  
  121. /****************** Change Roles via Serial Commands ***************************/
  122.  
  123. if ( Serial.available() )
  124. {
  125. char c = toupper(Serial.read());
  126. if ( c == 'T' && role == 0 ){
  127. Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
  128. role = 1; // Become the primary transmitter (ping out)
  129.  
  130. }else
  131. if ( c == 'R' && role == 1 ){
  132. Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
  133. role = 0; // Become the primary receiver (pong back)
  134. radio.startListening();
  135.  
  136. }
  137. }
  138.  
  139.  
  140. } // Loop
Add Comment
Please, Sign In to add comment