I posted a pair of demo programs here and following some comments I have changed them a little in the hope of making things a bit clearer.
These two programs are cut-down versions of a system I use for controlling model trains. The idea is that th Track Control program can control a number of tracks based on data sent from different hand controllers. The TrackControl program acts as the master and sends a message to each hand control in turn. When it receives a message the hand-control uses ackPayload to send data back to the master.
By using ackPayload there is no need for the master and slave to switch roles from transmitting (receiving) to receiving (transmtting).
In the example code the master just calls one slave and sends a couple of numbers. The slave sends a different number back. All the numbers are incremented so you can see that change is happening.
EDIT 28 Feb 2016 - there is a slightly revised version of the code in Reply #16
The master code
// TrackControl - the master or the transmitter
// http://tmrh20.github.io/RF24/
//~ - CONNECTIONS: nRF24L01 Modules See:
//~ http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
//~ 1 - GND
//~ 2 - VCC 3.3V !!! NOT 5V
//~ 3 - CE to Arduino pin 9
//~ 4 - CSN to Arduino pin 10
//~ 5 - SCK to Arduino pin 13
//~ 6 - MOSI to Arduino pin 11
//~ 7 - MISO to Arduino pin 12
//~ 8 - UNUSED
#include <SPI.h>
//~ #include <TMRh20nRF24L01.h>
//~ #include <TMRh20RF24.h>
#include <RF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
// NOTE: the "LL" at the end of the constant is "LongLong" type
// These are the IDs of each of the slaves
const uint64_t slaveID[2] = {0xE8E8F0F0E1LL, 0xE8E8F0F0E2LL} ;
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
int dataToSend[2];
unsigned long currentMillis;
unsigned long prevMillis;
unsigned long txIntervalMillis = 1000;
int txVal = 0;
int ackMessg[4];
byte ackMessgLen = 2;
void setup() {
Serial.begin(9600);
Serial.println("Track Control Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.enableAckPayload();
radio.setRetries(3,5); // delay, count
}
//====================
void loop() {
currentMillis = millis();
if (currentMillis - prevMillis >= txIntervalMillis) {
radio.openWritingPipe(slaveID[0]); // calls the first slave
// there could be a FOR loop to call several slaves in turn
dataToSend[0] = txVal; // this gets incremented so you can see that new data is being sent
txVal += 1;
dataToSend[1] = txVal;
txVal += 1;
bool rslt;
rslt = radio.write( dataToSend, sizeof(dataToSend) );
Serial.print("\nRSLT (1 = success) ");
Serial.println(rslt);
Serial.print("Data Sent ");
Serial.print(dataToSend[0]);
Serial.print(" ");
Serial.println(dataToSend[1]);
if ( radio.isAckPayloadAvailable() ) {
radio.read(ackMessg,ackMessgLen);
Serial.print("Acknowledge received: ");
Serial.println(ackMessg[0]);
}
prevMillis = millis();
}
}
The slave code
// HandController - the slave or the receiver
// http://tmrh20.github.io/RF24/
//~ - CONNECTIONS: nRF24L01 Modules See:
//~ http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
//~ 1 - GND
//~ 2 - VCC 3.3V !!! NOT 5V
//~ 3 - CE to Arduino pin 9
//~ 4 - CSN to Arduino pin 10
//~ 5 - SCK to Arduino pin 13
//~ 6 - MOSI to Arduino pin 11
//~ 7 - MISO to Arduino pin 12
//~ 8 - UNUSED
#include <SPI.h>
//~ #include <TMRh20nRF24L01.h>
//~ #include <TMRh20RF24.h>
#include <RF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t deviceID = 0xE8E8F0F0E1LL; // Define the ID for this slave
int valChange = 1;
RF24 radio(CE_PIN, CSN_PIN);
int dataReceived[2];
int ackData[2] = {12,0};
byte ackLen = 2;
void setup() {
Serial.begin(9600);
delay(1000);
Serial.println("Hand Controller Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openReadingPipe(1,deviceID);
radio.enableAckPayload();
radio.writeAckPayload(1, ackData, ackLen);
radio.startListening();
}
void loop() {
if ( radio.available() ) {
radio.read( dataReceived, sizeof(dataReceived) );
Serial.print("Data received Number0 ");
Serial.print(dataReceived[0]);
Serial.print(" Number1 ");
Serial.println(dataReceived[1]);
radio.writeAckPayload(1, ackData, ackLen);
ackData[0] += valChange; // this just increments so you can see that new data is being sent
}
}
NOTE that these programs use TMRh20's version of the RF24 library
...R