nrf24l01, bidirectional communication

Hi, I(ve been trying to use two nrf24l01 transceivers, in the way, that when I press a button connected to the first Arduino Nano, a diode connected to the second Arduino will light up and the other way round (when I press a button on the second Arduino, a diode on the first one will light up). But when I tried the codes, which you can se down below, the diode connected to the first Arduino was lightening constantly, no matter whether the button was pressed or not and the other diode wasn’t lightening at all.
Could someone help me, please?

first code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

int led1 = 5;
int button1 = 6;
boolean button_state1 = 0;
boolean button_state2 = 0;

RF24 radio(7,8);
const byte addresses[6] = {“00001”, “00002”};

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[1]);
radio.openReadingPipe(1, addresses[0]);
radio.setPALevel(RF24_PA_LOW);

pinMode(led1, OUTPUT);
pinMode(button1, INPUT_PULLUP);
}

void loop() {
// put your main code here, to run repeatedly:
delay(5);
radio.stopListening();
button_state1 = digitalRead(button1);
radio.write(&button_state1, sizeof(button_state1));

delay(5);
radio.startListening();

while (!radio.available()) {
radio.read(&button_state2, sizeof(button_state2));

if (button_state2 == LOW) {
digitalWrite(led1, HIGH);
}
else {
digitalWrite(led1, LOW);
}

}

}

second code:

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7,8);
const byte addresses[6] = {“00001”, “00002”};

int button2 = 2;
int led2 = 6;
boolean button_state1 = 0;
boolean button_state2 = 0;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(addresses[0]); //00001
radio.openReadingPipe(1, addresses[1]); //00002
radio.setPALevel(RF24_PA_LOW);

pinMode(button2, INPUT_PULLUP);
pinMode(led2, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
delay(5);
radio.startListening();

if (radio.available()) {
radio.read(&button_state1, sizeof(button_state1));

if (button_state1 == LOW) {
digitalWrite(led2, HIGH);
}
else {
digitalWrite(led2, LOW);
}
delay(5);

radio.stopListening();
button_state1 = digitalRead(button2);
radio.write(&button_state2, sizeof(button_state2));

}
}

Have a look at this Simple nRF24L01+ Tutorial.

The second and third examples illustrate two-way communication. I reckon the system in the second example is much the simplest.

...R

Thank you for sharing the link, but I already found the mistake.

strouhanka:
Thank you for sharing the link, but I already found the mistake.

Please explain what it was for the benefit of other readers.

...R

First of all I used the function while instead of the function if in one of the codes and the second problem was that I accidently wrote button_state1 instead of button_state2 in the final lines.