Hi,
I'm creating something like a feedback system and i have problem with receiving data from slaves, here is my code.
Master:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
const byte numSlaves = 3;
const byte slaveAddresses[numSlaves][5] = {
{'R', 'x', 'A', 'A', 'A'},
{'R', 'x', 'A', 'A', 'B'},
{'R', 'x', 'A', 'A', 'C'}
};
const byte masterAddress[numSlaves][5] = {
{'T', 'X', 'a', 'a', 'a'},
{'T', 'X', 'a', 'a', 'b'},
{'T', 'X', 'a', 'a', 'c'}
};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataToSend[31] = "hello?";
int dataReceived[1];
bool newData = false;
bool rslt;
uint32_t period = 12000L;
uint32_t period2 = 3000L;
//============
void setup() {
Serial.begin(9600);
Serial.println("MasterSwapRoles Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
//radio.openReadingPipe(1, masterAddress);
radio.openReadingPipe(1, masterAddress[0]);
radio.openReadingPipe(2, masterAddress[1]);
radio.openReadingPipe(3, masterAddress[2]);
radio.setRetries(3, 5); // delay, count
pinMode(3, INPUT);
}
//=============
void loop() {
if (digitalRead(3) == HIGH) {
send();
for (uint32_t tStart = millis(); (millis() - tStart) < period;);
Serial.print("\nStart listening\n");
radio.startListening();
getData();
//showData();
}
}
//====================
void send() {
for (byte n = 0; n < numSlaves; n++) {
radio.stopListening();
radio.openWritingPipe(slaveAddresses[n]);
rslt = radio.write( &dataToSend, sizeof(dataToSend) );
//radio.startListening();
Serial.print("Wysłano: ");
Serial.print(dataToSend);
Serial.print(" to slave: ");
Serial.println(n + 1);
if (rslt) {
Serial.println(" Success");
}
else {
Serial.println(" Error");
}
}
}
//================
void getData() {
for (uint32_t tStart = millis(); (millis() - tStart) < period2;) {
for (byte n = 0; n < numSlaves; n++) {
if ( radio.available(&n + 1) ) {
radio.read( &dataReceived, sizeof(dataReceived) );
Serial.print("\nfrom slave: ");
Serial.println(n + 1);
newData = true;
showData();
}
}
}
Serial.print("------------------------------------------------\n");
}
//================
void showData() {
if (newData == true) {
Serial.print(" Received: ");
if (dataReceived[0] == 1) {
Serial.print("YES\n");
}
else if (dataReceived[0] == 2) {
Serial.print("NO\n");
}
else if (dataReceived[0] == 3) {
Serial.print("HOLDING\n");
}
else {
Serial.print("NO VOTE\n");
}
newData = false;
}
}
Slave1:
// SlaveSwapRoles
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'A'};
const byte masterAddress[5] = {'T', 'X', 'a', 'a', 'a'};
RF24 radio(CE_PIN, CSN_PIN);
char dataReceived[31];
int replyData[1];
bool newData = false;
bool rslt;
uint32_t period = 10000L;
void setup() {
Serial.begin(9600);
Serial.println("SlaveSwapRoles Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openWritingPipe(masterAddress);
radio.openReadingPipe(1, slaveAddress);
radio.setRetries(3, 5); // delay, count
radio.startListening();
pinMode(3, INPUT); //tak / yes
pinMode(4, INPUT); //nie / no
pinMode(5, INPUT); //wstrzymuje sie /holding
}
//====================
void loop() {
getData();
if (newData == true) {
showData();
vote();
send();
}
}
//====================
void send() {
if (newData == true) {
radio.stopListening();
rslt = radio.write( &replyData, sizeof(replyData) );
radio.startListening();
Serial.print("Wysłano: ");
Serial.print(replyData[0]);
if (rslt) {
Serial.println(" Success");
}
else {
Serial.println("Error");
}
Serial.println();
newData = false;
}
}
//================
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
//================
void showData() {
Serial.print("Received: ");
Serial.println(dataReceived);
}
//================
void vote() {
for (uint32_t tStart = millis(); (millis() - tStart) < period;) {
if (digitalRead(3) == HIGH) {
replyData[0] = 1;
}
else if (digitalRead(4) == HIGH) {
replyData[0] = 2;
}
else if (digitalRead(5) == HIGH) {
replyData[0] = 3;
}
}
}
Slave2:
// SlaveSwapRoles
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 7
#define CSN_PIN 8
const byte slaveAddress[5] = {'R', 'x', 'A', 'A', 'B'};
const byte masterAddress[5] = {'T', 'X', 'a', 'a', 'b'};
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
char dataReceived[31]; // must match dataToSend in master
int replyData[1]; // the two values to be sent to the master
bool newData = false;
bool rslt;
uint32_t period = 10000L;
void setup() {
Serial.begin(9600);
Serial.println("SlaveSwapRoles Starting");
radio.begin();
radio.setDataRate( RF24_250KBPS );
radio.openWritingPipe(masterAddress); // NB these are swapped compared to the master
radio.openReadingPipe(1, slaveAddress);
radio.setRetries(3, 5); // delay, count
radio.startListening();
pinMode(3, INPUT); //tak
pinMode(4, INPUT); //nie
pinMode(5, INPUT); //wstrzymuje sie
}
//====================
void loop() {
getData();
if (newData == true) {
showData();
vote();
send();
}
}
//====================
void send() {
if (newData == true) {
radio.stopListening();
rslt = radio.write( &replyData, sizeof(replyData) );
radio.startListening();
Serial.print("Wysłano: ");
Serial.print(replyData[0]);
if (rslt) {
Serial.println(" Powodzenie");
}
else {
Serial.println("Blad wysylania.");
}
Serial.println();
newData = false;
}
}
//================
void getData() {
if ( radio.available() ) {
radio.read( &dataReceived, sizeof(dataReceived) );
newData = true;
}
}
//================
void showData() {
Serial.print("Otrzymano: ");
Serial.println(dataReceived);
}
//================
void vote() {
for (uint32_t tStart = millis(); (millis() - tStart) < period;) {
if (digitalRead(3) == HIGH) {
replyData[0] = 1;
}
else if (digitalRead(4) == HIGH) {
replyData[0] = 2;
}
else if (digitalRead(5) == HIGH) {
replyData[0] = 3;
}
}
}
Master module should send a message to all slaves when button is clicked (this part is working), then It sould wait till voting is complete and read data that was send by slaves.
I can't collect data from all slaves, only one. Could you pleeease help me?
Also I've used code from Simple nRF24L01+Tutorial from second reply.