Sketch stop looping!!? NRF24L01 transmitter

Hello~ :slight_smile:

I am learning about NRF24L01 and I tried making a simple project that I could communicate two Arduinos, one as a Transmitter and the other as a Receiver, sending and receiving information at the same time.
Its basically two Arduinos(UNO) with a Led and a Pushbutton connected in both. If I press the pushbutton, the Led at the other Arduino will turn ON.

It is working, but the Transmitter suddenly stops looping(I think). The program stops completly about 5 or 10 seconds after a turn on.
Im using a 100miliseconds delay, I realized that the faster the sketch goes the faster it stops looping. If I use 10miliseconds delay it stops in about 2 seconds.

I can see by the Serial monitor that only the transmitter arduino stops looping, while the other arduino(receiver) works fine and keep waiting for a signal from the transmitter(that is frozen).
Before the transmitter stops(5 to 10 seconds after turn on), everything works fine.

--> Here is the TRANSMITTER CODE:

// TRANSMITTER

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define button 3
#define led 2

RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};

boolean buttonState = 0;
boolean recbutton = 0;

unsigned long timer = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  
  radio.begin();
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.openWritingPipe(addresses[1]); // 00002
  radio.openReadingPipe(1, addresses[0]); // 00001
  
  Serial.begin(9600);
}

void loop() {
  buttonState = digitalRead(button);
  
  radio.stopListening();
  if (!radio.write( &buttonState, sizeof(buttonState) )) {
    Serial.println("No acknowledgement of transmission - receiving radio device connected?");    
  } else{
    Serial.print("Sent button: ");
    Serial.println(buttonState);
  }

  delay(100);

  radio.startListening();
  timer = millis();
  while(!radio.available()){
    if(millis() - timer >= 200){
      Serial.println("No response received - timeout!");
      return;  
    }
  }

  radio.read(&recbutton, sizeof(recbutton));
  if(recbutton == 1){
    digitalWrite(led, LOW);
  } else{
    digitalWrite(led, HIGH);
  }
  Serial.print("Received Button: ");
  Serial.println(recbutton);

  delay(100);
}

--> Here is the Receiver code:

// RECEIVER

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

#define button 3
#define led 2

RF24 radio(7, 8); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};

boolean buttonState = 0;
boolean recbutton = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(led, OUTPUT);
  pinMode(button, INPUT_PULLUP);
  
  radio.begin();
  radio.setPALevel(RF24_PA_MIN);
  radio.setDataRate(RF24_2MBPS);
  radio.setChannel(124);
  radio.openWritingPipe(addresses[0]); // 00002
  radio.openReadingPipe(1, addresses[1]); // 00001
  radio.startListening();
  
  Serial.begin(9600);
}

void loop() {
  if(radio.available()){
    while(radio.available()){
      radio.read(&recbutton, sizeof(recbutton));
      Serial.print("Received Button: ");
      Serial.println(recbutton);
    }
    if(recbutton == 1){
      digitalWrite(led, LOW);
    } else{
      digitalWrite(led, HIGH);
    }

    delay(100);

    radio.stopListening();
    buttonState = digitalRead(button);
    if(radio.write(&buttonState, sizeof(buttonState))){
      Serial.print("Sent button state: ");
      Serial.println(buttonState);
    } else{
      Serial.println("Sent failed");
    }
    delay(100);

    
    radio.startListening(); 
  }
}

I tried to check the connections and the sketch, but everything looks fine, I really have no idea what is the problem.

Thank you!

Have a look at this Simple nRF24L01+ Tutorial.

Wireless problems can be very difficult to debug so get the wireless part working on its own before you start adding any other features.

The examples are as simple as I could make them and they have worked for other Forum members. If you get stuck it will be easier to help with code that I am familiar with. Start by getting the first example to work

...R

Ok, I will learn from there and try again.
Thank you!