Advertisement
Guest User

Untitled

a guest
Jan 9th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Project: nRF905 AVR/Arduino Library/Driver
  3.  * Author: Zak Kemble, contact@zakkemble.co.uk
  4.  * Copyright: (C) 2014 by Zak Kemble
  5.  * License: GNU GPL v3 (see License.txt)
  6.  */
  7.  
  8. /*
  9.  * Wireless serial link
  10.  *
  11.  * 7 -> CE
  12.  * 8 -> PWR
  13.  * 9 -> TXE
  14.  * 2 -> CD
  15.  * 3 -> DR
  16.  * 10 -> CSN
  17.  * 12 -> SO
  18.  * 11 -> SI
  19.  * 13 -> SCK
  20.  */
  21.  
  22. #include <nRF905.h>
  23. #include <SPI.h>
  24.  
  25. #define RXADDR 0x00 // Address of this device (4 bytes / long data type)
  26. #define TXADDR 0x00 // Address of device to send to (4 bytes / long data type)
  27.  
  28. void setup()
  29. {
  30.     // Start up
  31.     nRF905_init();
  32.  
  33.     // Set address of this device
  34.     nRF905_setRXAddress(RXADDR);
  35.  
  36.     // Put into receive mode
  37.     nRF905_receive();
  38.  
  39.     Serial.begin(9600);
  40.    
  41.     Serial.println("Ready");
  42. }
  43.  
  44. void loop()
  45. {
  46.     byte dataSize;
  47.     while((dataSize = Serial.available()))
  48.     {
  49.         // Make sure we don't try to send more than max payload size
  50.         if(dataSize > NRF905_MAX_PAYLOAD - 1)
  51.             dataSize = NRF905_MAX_PAYLOAD - 1;
  52.  
  53.         // Copy data from serial to payload buffer
  54.         byte data[dataSize + 1];
  55.         for(byte i=0;i<dataSize;i++)
  56.             data[i] = Serial.read();
  57.  
  58.         data[dataSize] = 0x00; // NULL char at the end
  59.  
  60.         // Set address of device to send to
  61.         nRF905_setTXAddress(TXADDR);
  62.  
  63.         // Set payload data
  64.         nRF905_setData(data, dataSize);
  65.  
  66.         // Send payload (send fails if other transmissions are going on, keep trying until success)
  67.         while(!nRF905_send());
  68.     }
  69.  
  70.     // Put into receive mode
  71.     nRF905_receive();
  72.  
  73.     // Make buffer for reply
  74.     byte buffer[NRF905_MAX_PAYLOAD];
  75.     bool success;
  76.  
  77.     // Wait for data, cancel if new serial data available
  78.     while(!(success = nRF905_getData(buffer, sizeof(buffer))) && !Serial.available());
  79.  
  80.     if(success)
  81.         Serial.println((char*)buffer);
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement