Advertisement
rozman50

BARELY WORKING - master

May 14th, 2018
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #include "Arduino.h"
  2. #include <SPI.h>
  3. #include <RF24.h>
  4.  
  5. // This is just the way the RF24 library works:
  6. // Hardware configuration: Set up nRF24L01 radio on SPI bus (pins 10, 11, 12, 13) plus pins 7 & 8
  7. RF24 radio(7, 8);
  8.  
  9. byte addresses[][6] = { "1Node", "2Node" };
  10.  
  11. char receivedData[32];
  12. char data1[32];
  13.  
  14. int j = 33;
  15.  
  16. // -----------------------------------------------------------------------------
  17. // SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP SETUP
  18. // -----------------------------------------------------------------------------
  19. void setup() {
  20. Serial.begin(9600);
  21. Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
  22.  
  23. // Initiate the radio object
  24. radio.begin();
  25.  
  26. // Set the transmit power to lowest available to prevent power supply related issues
  27. radio.setPALevel(RF24_PA_MIN);
  28.  
  29. // Set the speed of the transmission to the quickest available
  30. radio.setDataRate(RF24_2MBPS);
  31.  
  32. // Use a channel unlikely to be used by Wifi, Microwave ovens etc
  33. radio.setChannel(124);
  34.  
  35. // Open a writing and reading pipe on each radio, with opposite addresses
  36. radio.openWritingPipe(addresses[1]);
  37. radio.openReadingPipe(1, addresses[0]);
  38.  
  39. // Random number seeding (we're going to be sending a single random number)
  40. randomSeed(analogRead(A0));
  41. }
  42.  
  43. // -----------------------------------------------------------------------------
  44. // LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP LOOP
  45. // -----------------------------------------------------------------------------
  46. void loop() {
  47.  
  48. // Generate a single random character to transmit
  49. char data = 'a';
  50.  
  51.  
  52.  
  53. // Ensure we have stopped listening (even if we're not) or we won't be able to transmit
  54. radio.stopListening();
  55.  
  56. // Did we manage to SUCCESSFULLY transmit that (by getting an acknowledgement back from the other Arduino)?
  57. // Even we didn't we'll continue with the sketch, you never know, the radio fairies may help us
  58.  
  59. /*
  60. if (!radio.write(&data1, sizeof(data1))) {
  61. // Serial.println("No acknowledgement of transmission - receiving radio device connected?");
  62. }
  63. */
  64.  
  65. String neki = "datatosend";
  66.  
  67. neki.toCharArray(data1, 32);
  68.  
  69. if (!radio.write(&data1, sizeof(data1))) {
  70. // Serial.println("No acknowledgement of transmission - receiving radio device connected?");
  71. }
  72.  
  73. // Now listen for a response
  74. radio.startListening();
  75.  
  76. // But we won't listen for long, 200 milliseconds is enough
  77. unsigned long started_waiting_at = millis();
  78.  
  79. // Loop here until we get indication that some data is ready for us to read (or we time out)
  80.  
  81. while (!radio.available()) {
  82.  
  83. // Oh dear, no response received within our timescale
  84. if (millis() - started_waiting_at > 200) {
  85. Serial.println("No response received - timeout!");
  86. return;
  87. }
  88. }
  89. j++;
  90.  
  91.  
  92. // Now read the data that is waiting for us in the nRF24L01's buffer
  93. unsigned char dataRx;
  94. radio.read(&receivedData, sizeof(receivedData));
  95.  
  96. // Show user what we sent and what we got back
  97. Serial.print("Sent: ");
  98. Serial.print(j);
  99. Serial.print(", received: ");
  100. Serial.println(receivedData);
  101.  
  102. if (j > 129) j = 33;
  103.  
  104. // Try again 1s later
  105. delay(500);
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement