Two Way nrf communication

I am currently making a two way morse code communication system, I have been able to get it working one way, but as soon as I try making it communicate back and forth I lose connection.
I have tried:
-Checking wiring (everything is correct)
-Adding delays into code
-Changing where it says radio.stopListening(); and radio.startListening();
-changing ce/csn pins

Here's my code

//Include Libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int pin = 5;
//create an RF24 object
RF24 radio(10, 8);  // CE, CSN
//address through which two modules communicate.
const byte address[6] = "00001";
//set variables for inputs
int bs1;
#define bp1 2

void setup() { 
Serial.begin(9600);
  radio.begin();
  
  //set the address
  radio.openWritingPipe(address);
    Serial.println("Begin sending");
            //Set module as transmitter
    radio.stopListening();
}

void loop() {
  //Read buttons
  bs1 = digitalRead(bp1);
  delay(500);
  Serial.println(bs1);
    radio.write(&bs1, sizeof(bs1));
      //Set module as reciever
    radio.startListening();
      if (radio.available())
  {
    //print to the serial monitor if connection is established
    Serial.println("Connection established");
    //decode data
    boolean bs1;
    radio.read(&bs1,sizeof(bs1));
    Serial.println("Button stat");
    Serial.println(bs1);
    delay(500); 
    if(bs1 == 0){
      digitalWrite(pin, HIGH);
      delay(500);
    }
    else{
      digitalWrite(pin, LOW);
    }
    }
else{
  Serial.println("NO CONNECTION");
  radio.stopListening();
}
}

Hmmmm. Where does your code switch from sending to waiting for something to receive? Remember, this it alternating between sending and receiving, not doing both at the same time. You likely need some code to tell the receiver to begin sending, now, while you wait for a message coming back.

Have a look at the Two-way transmission by swapping roles example in Robin2's simple rf24 tutorial.

Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code, in code tags, to make the code easier to read and follow.

For bidirectional communication without swapping roles,
Two-way transmission using the ackPayload concept
is a possible solution.

On most all tranceiver modules, they can only receive or transmit, not both at the same time.

Most a modulesalso take a finite amount of time to switch between transmit and receive.

And the reply takes a certain amount of time to actually transmit before it can be received.

So if you transmit something and immediatly check if something has been received, that is likley to fail.

AckPayload does exactly that, it directly attaches a packet inside the Ack for the transmission,
so it will be available directly after transmit.

When
I say "radio.stopListening();" I'm telling the nrf to start sending, and when I say "radio.startListening();" I'm telling it to start recieving.

Is there a way to send and receive on the same module. Do I have to add more delays or something?

Thank you soooooooooo much! I think this will work!

Of course there is, but you must be able to tell which unit to go first and then eventually tell the second unit it is your turn to send while the first listens.
Paul

Thanks!!!

I'm trying to do this with a boolean, if you have any better ideas please tell me!

From my understanding, the "reply Payload" must already be in the TX FIFO before even receiving the message from the transmitter, i.e. the reply can't be dependent on the content of message of the transmitter.

That does not contradict my statement.

It does not, but it is a caveat someone wanted to implement this has to be aware of

Absolutely.

If you think about the process, it's obvious, hidden in plain sight. :grinning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.