Advertisement
Guest User

Untitled

a guest
May 20th, 2019
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #include <SPI.h>
  2. #include "nRF24L01.h"
  3. #include "RF24.h"
  4. #include "printf.h"
  5.  
  6. int echoPin = 4;
  7. int trigPin = 3;
  8.  
  9. int mydata[3];
  10. int hisdata[3];
  11.  
  12. RF24 radio(9, 10);
  13.  
  14. typedef enum { role_ping_out = 1, role_pong_back } role_e;
  15.  
  16. const char* role_friendly_name[] = { "invalid", "Ping out", "Pong back" };
  17.  
  18. role_e role = role_pong_back;
  19.  
  20. int __ID__ = 0;
  21. int mount = 2;
  22. const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
  23.  
  24. uint64_t getPipe(int id = __ID__)
  25. {
  26. return pipes[id];
  27. }
  28.  
  29. void setup(void)
  30. {
  31. Serial.begin(9600);
  32. pinMode(trigPin, OUTPUT);
  33. pinMode(echoPin, INPUT);
  34. printf_begin();
  35. printf("ROLE: %s\n\r", role_friendly_name[role]);
  36. printf("*** PRESS 'T' to begin transmitting to the other node\n\r");
  37.  
  38. radio.openReadingPipe(1, getPipe());
  39.  
  40. radio.begin();
  41. radio.setRetries(15, 15);
  42. radio.startListening();
  43. radio.printDetails();
  44. }
  45.  
  46. int cm1 = 0;
  47.  
  48. void loop(void)
  49. {
  50. if (Serial.available()) {
  51. radio.openWritingPipe(getPipe(0));
  52. }
  53.  
  54. radio.startListening();
  55. int duration, cm;
  56. digitalWrite(trigPin, LOW);
  57. delayMicroseconds(2);
  58. digitalWrite(trigPin, HIGH);
  59. delayMicroseconds(10);
  60. digitalWrite(trigPin, LOW);
  61. duration = pulseIn(echoPin, HIGH);
  62. cm = duration / 58;
  63.  
  64. if ((cm <= 0.8 * cm1) || (cm >= 1.2 * cm1))
  65. role = role_ping_out;
  66. else
  67. role = role_pong_back;
  68.  
  69. if (role == role_ping_out) {
  70. cm1 = cm;
  71.  
  72. radio.stopListening();
  73.  
  74. mydata[0] = __ID__;
  75. mydata[1] = millis();
  76. mydata[2] = cm;
  77.  
  78.  
  79. for(int i = 0; i < mount; i++) {
  80. if (i != __ID__) {
  81. radio.openWritingPipe(getPipe(i));
  82.  
  83. unsigned long time = millis();
  84. printf("Now sending %u...", cm);
  85. bool ok = radio.write(&mydata, sizeof(mydata));
  86.  
  87. if (ok)
  88. printf("ok...");
  89. else
  90. printf("failed.\n\r");
  91.  
  92. radio.startListening();
  93.  
  94. unsigned long started_waiting_at = millis();
  95. bool timeout = false;
  96. while (!radio.available() && !timeout)
  97. if (millis() - started_waiting_at > 200)
  98. timeout = true;
  99.  
  100. if (timeout) {
  101. printf("Failed, response timed out.\n\r");
  102. }
  103. else {
  104. int got_time;
  105. radio.read(&mydata[2], sizeof(int));
  106. printf("Got response %u, round-trip delay: %u\n\r", mydata[2], mydata[2]);
  107. }
  108. }
  109. }
  110. }
  111.  
  112. delay(1000);
  113.  
  114. if (role == role_pong_back) {
  115. if (radio.available()) {
  116. int got_time;
  117. bool done = false;
  118. while (!done) {
  119. done = radio.read(&hisdata, sizeof(hisdata));
  120. printf("Got payload %u...", hisdata[2]);
  121. delay(20);
  122. }
  123.  
  124. radio.stopListening();
  125.  
  126. radio.write(&hisdata[2], sizeof(int));
  127. printf("Sent response.\n\r");
  128.  
  129. radio.startListening();
  130. }
  131. }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement