Ein Roboter mit bürstenlosem Antrieb, differenzial und NRF24L01 Funk. Großflächig gebaut um ein großes Solarpanel aufzunehmen. https://gitlab.informatik.hs-fulda.de/fdai5253/roboter
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.7 KiB

  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. #include <SPI.h>
  7. #include "RF24.h"
  8. /****************** User Config ***************************/
  9. /*** Set this radio as radio number 0 or 1 ***/
  10. bool radioNumber = 0;
  11. /* Hardware configuration: Set up nRF24L01 radio on SPI bus plus pins 7 & 8 */
  12. RF24 radio(7,8);
  13. /**********************************************************/
  14. byte addresses[][6] = {"1Node","2Node"};
  15. // Used to control whether this node is sending or receiving
  16. bool role = 0;
  17. void setup() {
  18. Serial.begin(115200);
  19. Serial.println(F("RF24/examples/GettingStarted"));
  20. Serial.println(F("*** PRESS 'T' to begin transmitting to the other node"));
  21. radio.begin();
  22. // Set the PA Level low to prevent power supply related issues since this is a
  23. // getting_started sketch, and the likelihood of close proximity of the devices. RF24_PA_MAX is default.
  24. radio.setPALevel(RF24_PA_LOW);
  25. // Open a writing and reading pipe on each radio, with opposite addresses
  26. if(radioNumber){
  27. radio.openWritingPipe(addresses[1]);
  28. radio.openReadingPipe(1,addresses[0]);
  29. }else{
  30. radio.openWritingPipe(addresses[0]);
  31. radio.openReadingPipe(1,addresses[1]);
  32. }
  33. // Start the radio listening for data
  34. radio.startListening();
  35. }
  36. void loop() {
  37. /****************** Ping Out Role ***************************/
  38. if (role == 1) {
  39. radio.stopListening(); // First, stop listening so we can talk.
  40. Serial.println(F("Now sending"));
  41. unsigned long start_time = micros(); // Take the time, and send it. This will block until complete
  42. if (!radio.write( &start_time, sizeof(unsigned long) )){
  43. Serial.println(F("failed"));
  44. }
  45. radio.startListening(); // Now, continue listening
  46. unsigned long started_waiting_at = micros(); // Set up a timeout period, get the current microseconds
  47. boolean timeout = false; // Set up a variable to indicate if a response was received or not
  48. while ( ! radio.available() ){ // While nothing is received
  49. if (micros() - started_waiting_at > 200000 ){ // If waited longer than 200ms, indicate timeout and exit while loop
  50. timeout = true;
  51. break;
  52. }
  53. }
  54. if ( timeout ){ // Describe the results
  55. Serial.println(F("Failed, response timed out."));
  56. }else{
  57. unsigned long got_time; // Grab the response, compare, and send to debugging spew
  58. radio.read( &got_time, sizeof(unsigned long) );
  59. unsigned long end_time = micros();
  60. // Spew it
  61. Serial.print(F("Sent "));
  62. Serial.print(start_time);
  63. Serial.print(F(", Got response "));
  64. Serial.print(got_time);
  65. Serial.print(F(", Round-trip delay "));
  66. Serial.print(end_time-start_time);
  67. Serial.println(F(" microseconds"));
  68. }
  69. // Try again 1s later
  70. delay(1000);
  71. }
  72. /****************** Pong Back Role ***************************/
  73. if ( role == 0 )
  74. {
  75. unsigned long got_time;
  76. if( radio.available()){
  77. // Variable for the received timestamp
  78. while (radio.available()) { // While there is data ready
  79. radio.read( &got_time, sizeof(unsigned long) ); // Get the payload
  80. }
  81. radio.stopListening(); // First, stop listening so we can talk
  82. radio.write( &got_time, sizeof(unsigned long) ); // Send the final one back.
  83. radio.startListening(); // Now, resume listening so we catch the next packets.
  84. Serial.print(F("Sent response "));
  85. Serial.println(got_time);
  86. }
  87. }
  88. /****************** Change Roles via Serial Commands ***************************/
  89. if ( Serial.available() )
  90. {
  91. char c = toupper(Serial.read());
  92. if ( c == 'T' && role == 0 ){
  93. Serial.println(F("*** CHANGING TO TRANSMIT ROLE -- PRESS 'R' TO SWITCH BACK"));
  94. role = 1; // Become the primary transmitter (ping out)
  95. }else
  96. if ( c == 'R' && role == 1 ){
  97. Serial.println(F("*** CHANGING TO RECEIVE ROLE -- PRESS 'T' TO SWITCH BACK"));
  98. role = 0; // Become the primary receiver (pong back)
  99. radio.startListening();
  100. }
  101. }
  102. } // Loop