hi i'm mattia,
i think that this topic could be very interesting and useful because i didn't find too informations about it.
i have two arduino UNO and two nRF24l01, and i want that they could sand and receive data at the same time. but i have some problems when the receiver changes its mode and it have to send a response message
this is the sketch of the sending arduino
#include <Wire.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
#define IN 1
#define OUT 2
int role = OUT;
bool answer = false;
RF24 radio(7, 8); //nRF address
const byte txAddr[6] = "00001";
const byte rxAddr[6] = "00002";
int yellow = 3, red = 4;
int const x = 1;
int pin = 6, button;
char ans[5];
int anslen = sizeof(ans);
void setup() {
Serial.begin(9600);
radio.begin();
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.enableAckPayload(); // Allow optional ack payloads
radio.setRetries(0, 15); // Smallest time between retries, max no. of retries
radio.setPayloadSize(1); // Here we are sending 1-byte payloads to test the call-response speed
radio.openWritingPipe(txAddr); // Both radios listen on the same pipes by default, and switch when writing
//radio.openReadingPipe(1, rxAddr);
radio.startListening(); // Start listening
radio.printDetails();
//***Transmitting Data
/*radio.begin();
radio.openWritingPipe(rxAddr);
radio.openReadingPipe(0, rxAddr);
//radio.enableAckPayload();
radio.setRetries(3, 5);*/
pinMode(yellow, OUTPUT); //led giallo
pinMode(red, OUTPUT); //led rosso
pinMode(pin, INPUT_PULLUP); //pulsante
}
void loop () {
if (role == OUT) //role stabilisce se arduino trasmette o riceve
radio.stopListening(); // in qusto sta inviando
button = digitalRead(pin);
delay(200);
if (!button) { //quando viene premuto il pulsante
if (radio.write(&x, sizeof(x))) // viene semplicemente inviato X e viene fatto il controllo sull'effettivo invio con un if
Serial.println("inviato\n\n");
while ( !answer) { //answer è una variabile booleana che verifica se la risposta è arrivata
Serial.println(answer);
ledY();
if (role == OUT) // cambia role, in ricevente
change_mode(IN);
if (radio.available()) {
Serial.println("ricevuto");
radio.read(ans, anslen); //riceve il messaggio di ritorno dall'altro arduino
if (strcmp(ans, "OK") == 0) { // lo confronta con il messaggio che dovrebbe essere
ledR();
answer = true; //cambia la variabile per uscire dal while
}
}
}
answer = false; // ritorno alle condizioni iniziali
change_mode(OUT);
}
}
void change_mode(int x) { // funzione che scambia i ruoli fra ricevente e trasmettente
switch (x) {
case IN:
role = IN;
radio.openReadingPipe(1, rxAddr);
radio.startListening();
Serial.println ("sto ricevendo");
break;
case OUT:
role = OUT;
radio.stopListening();
radio.openWritingPipe(txAddr);
break;
}
return;
}
void ledY() {
digitalWrite(red, LOW);
digitalWrite(yellow, HIGH);
delay(300);
digitalWrite(yellow, LOW);
delay(300);
return;
}
void ledR() {
digitalWrite(yellow, LOW);
digitalWrite(red, HIGH);
delay(1500);
return;
}
this is the sketch of the receiving arduino
#include <Wire.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SPI.h>
#define IN 1
#define OUT 2
int role = IN;
char ackmex[] = "OK";
char answer[5];
RF24 radio(7, 8); //nRF address
const byte rxAddr[6] = "00001";
const byte txAddr[6] = "00002";
int yellow = 3, green = 4;
int const x = 1;
void setup() {
Serial.begin(9600);
pinMode(yellow, OUTPUT);
pinMode(green, OUTPUT);
radio.begin();
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.enableAckPayload(); // Allow optional ack payloads
radio.setRetries(0, 15); // Smallest time between retries, max no. of retries
radio.setPayloadSize(1); // Here we are sending 1-byte payloads to test the call-response speed
//radio.openWritingPipe(rxAddr); // Both radios listen on the same pipes by default, and switch when writing
radio.openReadingPipe(1, rxAddr);
radio.startListening(); // Start listening
radio.printDetails();
/*
radio.begin();
radio.openWritingPipe(rxAddr);
radio.openReadingPipe(1, rxAddr);
radio.setRetries(3, 5);
//radio.enableAckPayload();
//radio.writeAckPayload(0, ack, acklen);
Serial.println("sto ricevendo");
radio.startListening();
*/
}
void loop() {
digitalWrite(yellow, LOW);
digitalWrite(green, LOW);
if (radio.available()) { //si prepara a ricevere il messaggio
Serial.println("ricevuto");
ledG();
radio.read(answer, sizeof(answer)); //legge il valore
if (role == IN) //cambia il suo ruolo in trasmettente
change_mode(OUT);
Serial.println(ackmex);
radio.write(ackmex, sizeof(ackmex)); //invia il segnale di risposta
delay(100);
if (role == OUT)
change_mode(IN); //cambia il valore in ricevente, = condizioni iniziali
}
ledY();
}
void change_mode(int x) { //funzione per cambiare da ricevente a trasmettente
switch (x) {
case IN:
role = IN;
radio.openReadingPipe(1, rxAddr);
radio.startListening();
Serial.println("sto ricevendo");
break;
case OUT:
role = OUT;
radio.stopListening();
radio.openWritingPipe(txAddr);
Serial.println("sto trasmettndo");
break;
}
return;
}
void ledG() {
digitalWrite(yellow, LOW);
digitalWrite(green, HIGH);
delay(3000);
return;
}
void ledY() {
digitalWrite(green, LOW);
digitalWrite(yellow, HIGH);
delay(300);
digitalWrite(yellow, LOW);
delay(300);
return;
}