Advertisement
Guest User

Edgar C.

a guest
Oct 6th, 2013
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.74 KB | None | 0 0
  1. #include <nRF905.h>
  2. /*
  3.  * Project: nRF905 AVR/Arduino Library/Driver
  4.  * Author: Zak Kemble, me@zakkemble.co.uk
  5.  * Copyright: (C) 2013 by Zak Kemble
  6.  * License: GNU GPL v3 (see License.txt)
  7.  */
  8.  
  9. /*
  10.  * Wait for data and reply.
  11.  * Output power is set to the lowest setting, receive sensitivity is
  12.  * lowered.
  13.  *
  14.  * 7 -> CE
  15.  * 8 -> PWR
  16.  * 9 -> TXE
  17.  * 2 -> CD
  18.  * 3 -> DR
  19.  * 10 -> CSN
  20.  * 12 -> SO
  21.  * 11 -> SI
  22.  * 13 -> SCK
  23.  */
  24.  
  25. #include <nRF905.h>
  26. #include <SPI.h>
  27.  
  28. #define RXADDR 0x586F2E10 // Address of this device (4 bytes / long data type)
  29. #define TXADDR 0xFE4CA6E5 // Address of device to send to (4 bytes / long data type)
  30.  
  31. void setup()
  32. {
  33.   // Start up
  34.   nRF905_init();
  35.  
  36.   // Set address of this device
  37.   nRF905_setRXAddress(RXADDR);
  38.  
  39.   // Lowest transmit level -10db
  40.   nRF905_setTransmitPower(NRF905_PWR_n10);
  41.  
  42.   // Reduce receive sensitivity to save a few mA
  43.   nRF905_setLowRxPower(NRF905_LOW_RX_ENABLE);
  44.  
  45.   // Put into receive mode
  46.   nRF905_receive();
  47.  
  48.   Serial.begin(9600);
  49.  
  50.   Serial.println("Server started");
  51. }
  52.  
  53. void loop()
  54. {
  55.   Serial.println("Waiting for data...");
  56.  
  57.   // Make buffer for data
  58.   byte buffer[NRF905_MAX_PAYLOAD];
  59.  
  60.   // Wait for data
  61.   while(!nRF905_getData(buffer, sizeof(buffer)));
  62.  
  63.   for (byte i=0; i<sizeof(buffer); i++){
  64.     Serial.print((char)buffer[i]);
  65.     Serial.print("-");
  66.   }
  67.   Serial.println();
  68.  
  69.   // Set address of device to send to
  70.   nRF905_setTXAddress(TXADDR);
  71.  
  72.   // Set payload data (reply with data received)
  73.   nRF905_setData(buffer, sizeof(buffer));
  74.  
  75.   // Send payload (send fails if other transmissions are going on, keep trying until success)
  76.   while(!nRF905_send());
  77.  
  78.   // Put back into receive mode
  79.   nRF905_receive();
  80.  
  81.   Serial.println("Got data");
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement