Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Project: nRF905 AVR/Arduino Library/Driver
- * Author: Zak Kemble, contact@zakkemble.co.uk
- * Copyright: (C) 2014 by Zak Kemble
- * License: GNU GPL v3 (see License.txt)
- */
- /*
- * Wireless serial link
- *
- * 7 -> CE
- * 8 -> PWR
- * 9 -> TXE
- * 2 -> CD
- * 3 -> DR
- * 10 -> CSN
- * 12 -> SO
- * 11 -> SI
- * 13 -> SCK
- */
- #include <nRF905.h>
- #include <SPI.h>
- #define RXADDR 0x00 // Address of this device (4 bytes / long data type)
- #define TXADDR 0x00 // Address of device to send to (4 bytes / long data type)
- void setup()
- {
- // Start up
- nRF905_init();
- // Set address of this device
- nRF905_setRXAddress(RXADDR);
- // Put into receive mode
- nRF905_receive();
- Serial.begin(9600);
- Serial.println("Ready");
- }
- void loop()
- {
- byte dataSize;
- while((dataSize = Serial.available()))
- {
- // Make sure we don't try to send more than max payload size
- if(dataSize > NRF905_MAX_PAYLOAD - 1)
- dataSize = NRF905_MAX_PAYLOAD - 1;
- // Copy data from serial to payload buffer
- byte data[dataSize + 1];
- for(byte i=0;i<dataSize;i++)
- data[i] = Serial.read();
- data[dataSize] = 0x00; // NULL char at the end
- // Set address of device to send to
- nRF905_setTXAddress(TXADDR);
- // Set payload data
- nRF905_setData(data, dataSize);
- // Send payload (send fails if other transmissions are going on, keep trying until success)
- while(!nRF905_send());
- }
- // Put into receive mode
- nRF905_receive();
- // Make buffer for reply
- byte buffer[NRF905_MAX_PAYLOAD];
- bool success;
- // Wait for data, cancel if new serial data available
- while(!(success = nRF905_getData(buffer, sizeof(buffer))) && !Serial.available());
- if(success)
- Serial.println((char*)buffer);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement