Advertisement
Guest User

Edgar C.

a guest
Oct 6th, 2013
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. /*
  2.  * Project: nRF905 AVR/Arduino Library/Driver
  3.  * Author: Zak Kemble, me@zakkemble.co.uk
  4.  * Copyright: (C) 2013 by Zak Kemble
  5.  * License: GNU GPL v3 (see License.txt)
  6.  */
  7.  
  8. /*
  9.  * Turn the nRF905 on to transmit some data, wait for a reply,
  10.  * turn off and wait for a second.
  11.  * Output power is set to the lowest setting, receive sensitivity is
  12.  * lowered and uses the power up/down feature of the nRF905.
  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 0xFE4CA6E5 // Address of this device (4 bytes / long data type)
  29. #define TXADDR 0x586F2E10 // Address of device to send to (4 bytes / long data type)
  30. #define TIMEOUT 1000 // 1 second ping timeout
  31.  
  32. // Just some random data to send
  33. //byte data[NRF905_MAX_PAYLOAD] = {
  34. //0x0A,
  35. //0x68,
  36. //0x45,
  37. //0xFA
  38. //};
  39. char data[NRF905_MAX_PAYLOAD] = "hi";
  40.  
  41. void setup()
  42. {
  43.   // Start up
  44.   nRF905_init();
  45.  
  46.   // Set address of this device
  47.   nRF905_setRXAddress(RXADDR);
  48.  
  49.   // Lowest transmit level -10db
  50.   nRF905_setTransmitPower(NRF905_PWR_n10);
  51.  
  52.   // Reduce receive sensitivity to save a few mA
  53.   nRF905_setLowRxPower(NRF905_LOW_RX_ENABLE);
  54.  
  55.   // Put into receive mode
  56.   nRF905_receive();
  57.  
  58.   Serial.begin(9600);
  59.  
  60.   Serial.println("Client started");
  61. }
  62.  
  63. void loop()
  64. {
  65.   // Turn on module
  66.   nRF905_powerUp();
  67.  
  68.   // Set address of device to send to
  69.   nRF905_setTXAddress(TXADDR);
  70.  
  71.   //Checking array to send
  72.   for (int i =0; i < NRF905_MAX_PAYLOAD; i++){
  73.     Serial.print((char)data[i]);
  74.     Serial.print("-");
  75.   }
  76.   Serial.println();
  77.  
  78.   // Set payload data
  79.   nRF905_setData((byte*)data, sizeof(data));
  80.  
  81.   // Send payload (send fails if other transmissions are going on, keep trying until success)
  82.   while(!nRF905_send());
  83.  
  84.   // Put into receive mode
  85.   nRF905_receive();
  86.  
  87.   // Make buffer for reply
  88.   byte buffer[NRF905_MAX_PAYLOAD];
  89.   bool success;
  90.  
  91.   // Wait for reply with timeout
  92.   unsigned long sendStartTime = millis();
  93.   while(!(success = nRF905_getData(buffer, sizeof(buffer))) && millis() - sendStartTime < TIMEOUT);
  94.  
  95.   if(success)
  96.     Serial.println("Got response");
  97.   else
  98.     Serial.println("Timed out");
  99.  
  100.   // Turn off module
  101.   nRF905_powerDown();
  102.  
  103.   delay(1000);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement