zockerlein

Untitled

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