nRF24L01 send and receive data at the same time
hi i'm mattia,
i think topic interesting , useful because didn't find informations it.
i have 2 arduino uno , 2 nrf24l01, , want sand , receive data @ same time. have problems when receiver changes mode , have send response message
this sketch of sending arduino
this sketch of receiving arduino
i think topic interesting , useful because didn't find informations it.
i have 2 arduino uno , 2 nrf24l01, , want sand , receive data @ same time. have problems when receiver changes mode , have send response message
this sketch of sending arduino
code: [select]
#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 enabled
radio.enableackpayload(); // allow optional ack payloads
radio.setretries(0, 15); // smallest time between retries, max no. of retries
radio.setpayloadsize(1); // here sending 1-byte payloads test call-response speed
radio.openwritingpipe(txaddr); // both radios listen on same pipes default, , 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 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 sketch of receiving arduino
code: [select]
#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 enabled
radio.enableackpayload(); // allow optional ack payloads
radio.setretries(0, 15); // smallest time between retries, max no. of retries
radio.setpayloadsize(1); // here sending 1-byte payloads test call-response speed
//radio.openwritingpipe(rxaddr); // both radios listen on same pipes default, , 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 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 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;
}
please edit original post , correct reference nrf24l01 in title.
you can't send , receive @ same time - although can close together.
you need tell problem having. have managed communication working?
i got nrf24s working tutorial
i suggest use tmrh20 version of rf24 library - solves problems maniacbug version
the pair of programs in this link may useful.
...r
you can't send , receive @ same time - although can close together.
you need tell problem having. have managed communication working?
i got nrf24s working tutorial
i suggest use tmrh20 version of rf24 library - solves problems maniacbug version
the pair of programs in this link may useful.
...r
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > nRF24L01 send and receive data at the same time
arduino
Comments
Post a Comment