NRF240L01 antenna code locks up during if statement

I have been using the nrf240L01 antennas for a while now and have never ran into this issue. My code stops running when I do a radio.write line in an if statement. I have no idea why it locks up. Has anyone seen this before?

Code:

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

//define pins
const int trigPin = 2;
const int echoPin = 3;
const int trigPin2 = 4;
const int echoPin2 = 5;
int LEDPin = 9;
RF24 radio(7, 8);

//define variables
long duration;
long duration2;
int distance;
int distance2;
int Sensor1;
int Sensor2;
int InOut;
const byte address[6] = "00001";


void setup() {
  // put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(trigPin2, OUTPUT);
pinMode(echoPin2, INPUT);
pinMode(LEDPin, OUTPUT);
pinMode(A0, OUTPUT);
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();

digitalWrite(A0, HIGH);

}

void loop() { 
//Clear trig pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

//Set trig pin to state HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

//Reads echopin, return the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
delayMicroseconds(10);

//Read second HC
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);

//Calculate distance
distance2 = duration2*0.034/2;
distance = duration*0.034/2;

//Prints distance in Serial Monitor in cm
Serial.print("Distance2: ");
Serial.println(distance2);
Serial.print("Distance: ");
Serial.println(distance);

//delay(1000);

if (distance <= 20)
{
  for(int i=0; i<=5; i++)
  {
  //Attempt a second read for short
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);
  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = duration2*0.034/2;
  if (distance2 <= 20)
  {
    digitalWrite(LEDPin, HIGH);
    const char text[] = "ON"; //ERROR HERE
    radio.write(&text, sizeof(text)); //ERROR HERE
    Serial.println("HIT");
    delay(1000);
  }
  delay(50);
  }
}

if (distance2 <= 20)
{
  for(int i=0; i<=5; i++)
  {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;
  if (distance <=20 )
  {
    Serial.println("HIT RED");
    digitalWrite(A0, HIGH);
//    const char text[] = "ON";   //ERROR HERE
//    radio.write(&text, sizeof(text)); //ERROR HERE
    delay(1000);
  }
  delay(50);
  }
}



digitalWrite(LEDPin, LOW);
digitalWrite(A0, LOW);

}

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

This tutorial is very similar to the one I used.

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

...R

I have tested this without using any wireless code. Everything worked fine. The error only occurs once I add the radio.write commands. Other than that the Hypersonic sensors both work perfectly as expected.

cledfo11:
I have tested this without using any wireless code.

That is the exact opposite of the advice that I gave you and which you quoted in Reply #2

...R