Advertisement
Guest User

Untitled

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