Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.09 KB | None | 0 0
  1. #include <SPI.h>
  2. #include "nRF24L01.h"
  3. #include "RF24.h"
  4. #include "printf.h"
  5.  
  6. RF24 radio(9,10);
  7. // Pipes for reading/writing { Controller, Head, Body }
  8. const uint64_t pipes[3] = { 0xF0F0F0F0D2LL, 0xF0F0F0F0C3LL, 0xF0F0F0F0B4LL };
  9.  
  10. typedef struct{
  11.   int forward;
  12.   int lean;
  13.   int swivel;
  14.   int nod;
  15.   int look;
  16.   int sound;
  17.   long timestamp;
  18. }
  19. tD;
  20. tD transmit_data;
  21.  
  22.  
  23. void setup(void)
  24. {
  25.     Serial.begin(115200);
  26.     Serial.println("BaBy-8 Controller");
  27.  
  28.     radio.begin();
  29.     radio.setPALevel(RF24_PA_LOW);
  30.     radio.setRetries(15, 15);
  31.    
  32.     radio.openWritingPipe(pipes[0]);
  33.     radio.openReadingPipe(1, pipes[1]); // Head
  34.     //radio.openReadingPipe(2, pipes[2]); // Body
  35.     radio.startListening();
  36.     //radio.printDetails();
  37. }
  38.  
  39. void loop(void)
  40. {
  41.     // First, stop listening so we can talk.
  42.     radio.stopListening();
  43.  
  44.     // Take the time, and send it.  This will block until complete
  45.     unsigned long time = millis();
  46.     Serial.print("Now sending ");
  47.     transmit_data.forward = 10;
  48.     transmit_data.lean = 20;
  49.     transmit_data.swivel = 30;
  50.     transmit_data.nod = 40;
  51.     transmit_data.look = 50;
  52.     transmit_data.sound = 60;
  53.     transmit_data.timestamp = millis();
  54.     bool ok = radio.write( &transmit_data, sizeof(transmit_data) );
  55.    
  56.     if (ok)
  57.         Serial.print("ok...");
  58.     else
  59.         Serial.println("failed!");
  60.  
  61.     // Now, continue listening
  62.     radio.startListening();
  63.    
  64.     bool timeout = false;
  65.     uint8_t pipe_num;
  66.     while ( ! radio.available(&pipe_num) && ! timeout )
  67.         if (millis() - transmit_data.timestamp > 250 )
  68.             timeout = true;
  69.  
  70.     // Wait here until we get a response, or timeout (250ms)
  71.     unsigned long started_waiting_at = millis();
  72.     unsigned long got_time;
  73.    
  74.     if ( timeout ) {
  75.         Serial.println("Failed, response timed out.");
  76.     } else {
  77.         radio.read( &got_time, sizeof(unsigned long) );
  78.         Serial.print(pipe_num);
  79.         Serial.print(" responded in ");
  80.         Serial.print(millis()-got_time);
  81.     }
  82.     Serial.println("");
  83.    
  84.     delay(250);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement