RF circuit stops my ultrasonic sensor data

I am working on a remote control car with an ultrasonic sensor to stop the car at a distance of — I have a code that works for driving the car. I also have a code that gives me a distance with the sensor. When I put them together I will only randomly receive position data with the RF controller and I only get 0 distance with the ultrasonic sensor. Sometimes the serial print will print position data as distance. I am kind of new to Arduino but I am having a hard time getting them to work together. Any advice?

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

#include <SPI.h>      //SPI library for communication with the nRF24L01+
#include "RF24.h"  //The main library of the nRF24L01+
#include "Wire.h"

RF24 radio (7, 8); // CE,CSN

//Create a pipe addresses for  communication
const uint64_t pipe = 0xE8E8F0F0E1LL;

struct package
{
  int X = 1;
  int Y = 1;
};

typedef struct package Package;
Package pos;

int buttonState;
int spinnerState = false ;
int motora_1 = 3;
int motora_2 = 4;
int motorb_1 = 5;
int motorb_2 = 6;
int spinner = 2;

// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;

void setup()
{
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600);
  /* radio.begin();                    //Start the nRF24 communicate
    radio.openReadingPipe(1, pipe);   //Sets the address of the transmitter to which the program will receive data.
    radio.startListening();
    pinMode(motora_1, OUTPUT);
    pinMode(motora_2, OUTPUT);
    pinMode(motorb_1, OUTPUT);
    pinMode(motorb_2, OUTPUT);
    pinMode(spinner, OUTPUT);
    delay(100);
  */

}

void loop()
{
  //  if (radio.available()){
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.println(distance);
  //}
}
/*if (radio.available()) {
  radio.read(&pos, sizeof(pos));
  radio.read(&buttonState, sizeof(buttonState));
  Serial.print("X:");
  Serial.print(pos.X);
  Serial.print("      Y");
  Serial.print(pos.Y);
  }

  int xAxis = pos.X;
  int yAxis = pos.Y;
  int forward = map(yAxis, 570, 1024, 0, 255);
  int backward = map(yAxis, 480, 0, 0, 255);
  int right = map(xAxis, 570, 1024, 0, 255);
  int left = map(xAxis, 480, 0, 0, 255);

  if (distance < 19)  {     //stop
  // Set Motor A stop
  analogWrite(motora_1, 0);
  analogWrite(motora_2, 0);
  // Set Motor B stop
  analogWrite(motorb_1, 0);
  analogWrite(motorb_2, 0);
  }
  else if (yAxis > 570) {          // Move Forward
  // Set Motor A forward
  analogWrite(motora_1, forward);
  analogWrite(motora_2, 0);
  // Set Motor B forward
  analogWrite(motorb_1, forward);
  analogWrite(motorb_2, 0);
  } else if (yAxis < 480) {                     //Move Backward
  // Set Motor A backward
  analogWrite(motora_1, 0);
  analogWrite(motora_2, backward);
  // Set Motor B backward
  analogWrite(motorb_1, 0);
  analogWrite(motorb_2, backward);
  } else if (xAxis > 570) {                     // Move Right
  // Set Motor A forward
  analogWrite(motora_1, 0);
  analogWrite(motora_2, right);
  // Set Motor B stop
  analogWrite(motorb_1, 0);
  analogWrite(motorb_2, 0);
  } else if (xAxis < 480) {                     // Move Left
  // Set Motor A stop
  analogWrite(motora_1, 0);
  analogWrite(motora_2, 0);
  // Set Motor B foward
  analogWrite(motorb_1, 0);
  analogWrite(motorb_2, left);
  } else  {     //stop
  // Set Motor A stop
  analogWrite(motora_1, 0);
  analogWrite(motora_2, 0);
  // Set Motor B stop
  analogWrite(motorb_1, 0);
  analogWrite(motorb_2, 0);
  }


  }
*/

Read the topics telling “How to use forum” etc. there are 3-4 different ones.

Use example code etc. and make a sketch exercising just the distance sensor. That way You will really know how to use that sensor.
It happends that differet periferals collides, uses the same timer etc. Let’s wonder about that later.

If you want help with a spcific issue, then give the forum a clue in the title of your post.

A lot of people will ignore a post which just says 'Help' and then expects everyone to read the post to work out what exactly you want help with .............................

With your sketch in an IDE window , pull down "Tools" and click on "AutoFormat". Then under "Edit" click on "copy for forum". Then paste in your post.

Thanks, I am new to using forums in general. But I reposted the coding up above. I noticed If I comment out the code as I did in the code above I will get distance data. However, if I put the radio.begin(); //Start the nRF24 communicate back into the code I stop getting distance data.

sensor code

/*
  Ultrasonic Sensor HC-SR04 and Arduino Tutorial

  by Dejan Nedelkovski,
  www.HowToMechatronics.com

*/
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
// defines variables
long duration;
int distance;
void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication
}
void loop() {
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance = duration * 0.034 / 2;
  // Prints the distance on the Serial Monitor
  Serial.print("Distance: ");

car code

#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>

#include <SPI.h>      //SPI library for communication with the nRF24L01+
#include "RF24.h"  //The main library of the nRF24L01+
#include "Wire.h"

RF24 radio (7, 8); // CE,CSN

//Create a pipe addresses for  communication
const uint64_t pipe = 0xE8E8F0F0E1LL;

struct package
{
  int X = 1;
  int Y = 1;
};

typedef struct package Package;
Package pos;

int buttonState;
int spinnerState = false ;
int motora_1 = 3;
int motora_2 = 4;
int motorb_1 = 5;
int motorb_2 = 6;
int spinner = 2;

void setup()
{
  Serial.begin(9600);
  radio.begin();                    //Start the nRF24 communicate
  radio.openReadingPipe(1, pipe);   //Sets the address of the transmitter to which the program will receive data.
  radio.startListening();
  pinMode(motora_1, OUTPUT);
  pinMode(motora_2, OUTPUT);
  pinMode(motorb_1, OUTPUT);
  pinMode(motorb_2, OUTPUT);
  pinMode(spinner, OUTPUT);
  digitalWrite(spinner, LOW);
  delay(100);
}

void loop()
{
  if (radio.available()) {
    radio.read(&pos, sizeof(pos));
    radio.read(&buttonState, sizeof(buttonState));
    Serial.print("X:");
    Serial.print(pos.X);
    Serial.print("      Y");
    Serial.print(pos.Y);
    Serial.print("      buttonState");
    Serial.println(buttonState);
  }

  int xAxis = pos.X;
  int yAxis = pos.Y;
  int forward = map(yAxis, 570, 1024, 0, 255);
  int backward = map(yAxis, 480, 0, 0, 255);
  int right = map(xAxis, 570, 1024, 0, 255);
  int left = map(xAxis, 480, 0, 0, 255);

  if (distance < 19)  {     //stop
    // Set Motor A stop
    analogWrite(motora_1, 0);
    analogWrite(motora_2, 0);
    // Set Motor B stop
    analogWrite(motorb_1, 0);
    analogWrite(motorb_2, 0);
  }
  else if (yAxis > 570) {          // Move Forward
    // Set Motor A forward
    analogWrite(motora_1, forward);
    analogWrite(motora_2, 0);
    // Set Motor B forward
    analogWrite(motorb_1, forward);
    analogWrite(motorb_2, 0);
  } else if (yAxis < 480) {                     //Move Backward
    // Set Motor A backward
    analogWrite(motora_1, 0);
    analogWrite(motora_2, backward);
    // Set Motor B backward
    analogWrite(motorb_1, 0);
    analogWrite(motorb_2, backward);
  } else if (xAxis > 570) {                     // Move Right
    // Set Motor A forward
    analogWrite(motora_1, 0);
    analogWrite(motora_2, right);
    // Set Motor B stop
    analogWrite(motorb_1, 0);
    analogWrite(motorb_2, 0);
  } else if (xAxis < 480) {                     // Move Left
    // Set Motor A stop
    analogWrite(motora_1, 0);
    analogWrite(motora_2, 0);
    // Set Motor B foward
    analogWrite(motorb_1, 0);
    analogWrite(motorb_2, left);
  } else  {     //stop
    // Set Motor A stop
    analogWrite(motora_1, 0);
    analogWrite(motora_2, 0);
    // Set Motor B stop
    analogWrite(motorb_1, 0);
    analogWrite(motorb_2, 0);
  }


}

clearmist:
Thanks, I am new to using forums in general. But I reposted the coding up above. I noticed If I comment out the code as I did in the code above I will get distance data. However, if I put the radio.begin(); //Start the nRF24 communicate back into the code I stop getting distance data.

Perhaps you can tell us how you are supplying the 3.3 volts to power the NRF24. If you are using the 3.3 volts from the Arduino, then there is your problem.

Paul

I am supplying the power to the RF circuit with the 3.3 v from the Arduino Uno and without the ultrasonic sensor it works. the ultrasonic sensor is getting its power from the 5 v from the Arduino Uno.
How is it a problem to run them like this?
Should I find a new powersource for the ultrasonic sensor?

The RF24 radios request more current than the 3.3V regulator on an Uno can manage. You can try putting a 10uF cap across the supply terminals of the radio. Personally, I like to give the radio its own 3.3V supply. See the power recommendations in Robin2's simple rf24 tutorial.

groundFungus:
The RF24 radios request more current than the 3.3V regulator on an Uno can manage. You can try putting a 10uF cap across the supply terminals of the radio. Personally, I like to give the radio its own 3.3V supply. See the power recommendations in Robin2's simple rf24 tutorial.

Ok, thanks for your help! It was confusing because I initially made this as a car only and it was working fine. And trying to upgrade it with the sensor made it stop working. Maybe it doesn't have the power for both the RF circuit plus the sound sensor.

So I made a voltage divider to make a 9 V down to a 5 V and ran power the 5V power to the sound sensor.

I have 3 sets of code, one for the sensor to verify it is working correctly (it works with the new stand-alone power)

one for the car (contains RF controller code too)

and one code I am trying to combine the two.

when I upload the sensor (it works fine), I upload the car (It works fine)

Now with a new power supply, the car code started working! in the combined code. However, the sound sensor still continually reads 0

So I made a voltage divider and ran a solo power to the 5V sound sensor.

Please show a schematic of that. Verbal circuit descriptions are never as clear as a schematic. You are not powering something with a voltage divider are you?

groundFungus:
Please show a schematic of that. Verbal circuit descriptions are never as clear as a schematic. You are not powering something with a voltage divider are you?

Yes I am powering the sound sensor with a 9-volt battery 510-ohm resister and a 680-ohm resistor

A schematic of the divider looks like this.

I am powering it like this because earlier in the thread I was told the board cannot support power for both my sound sensor and the RF circuit.

voltage divider.png

voltage divider.png

You can not power anything with a voltage divider. The load completely changes the divider. Measure the actual voltage at the sound sensor to see.

groundFungus:
You can not power anything with a voltage divider. The load completely changes the divider. Measure the actual voltage at the sound sensor to see.

Yes, I see that you are correct. However, I was doing this to find the problem when running the sound sensor program with the voltage divider I was still able to get good distance data. ( it worked fine unless very close to the sensor)

Then loading the combined code I saw improvement with position data from the controller's RF circuit. However, I wasn't receiving the very slight degraded distance data.

So currently I have a working sound sensor code and sensor.
a working car/controller code
(both independently powered)

when combined I have a working car/controller but the sound sensor reads 0

I notice when running just the sound sensor the serial monitor reads distances like 15+ times a second.
but when combined it only reads at the speed of the RF circuit 1 time a second ish.

the picture of both serial monitors looks like this