0

I have 2 arduino unos that are talking to each other with a couple NRF240L01 antennas. I have used these before and never had an issue but with this project two of my radio.write commands are causing the code to freeze. They are about 2/3 way down the code. When my code hits these write commands it just stop and doesn't do anything else. I have no idea whats causing this. Im only using a tiny percent of my memory so I don't think its that. On a side note I also have 2 hypersonic sensors reading data as well.

//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);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.