RF coding with the NRF24L01

Hey everyone. I'm working on a RC car with Arduino Nano. I'm using the Mega as a receiver, and the dc motor is connected to it. I'm unable to get the dc motor to work, after changing the code around, following YouTube videos, and some articles. Would anyone be of any assistance :grin:?

Transmitter

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

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

const byte motor_A_left = 5;  // PWM motor pin IN2 motor A                 
const byte motor_A_right = 6;  // PWM motor pin IN1 motor A
const byte top_Button = 2;  // button for counter clockwise motor A
const byte bottom_Button = 3;  // button for clockwise motor A
const byte motor_B_left = 9; // PWM motor pin IN2 motor B
const byte motor_B_right = A1; // PWM motor pin IN1 motor B
const byte left_Button = 7;  // button for clockwise motor B
const byte right_Button = 4;  // button for counter clockwise motor B
const byte PotPin = A0;
const byte MotorB_speed = 30;

int topButton = digitalRead(2);
int bottomButton = digitalRead(3);
int leftButton = digitalRead(7);
int rightButton = digitalRead(4);



void setup() {
      // put your setup code here, to run once:
      pinMode (10,OUTPUT); pinMode(3,OUTPUT); pinMode(2,OUTPUT);
      radio.begin();
      radio.openWritingPipe(address[1]);
      radio.openReadingPipe(1, address[0]);
      radio.setPALevel(RF24_PA_MIN);
      Serial.begin(9600);
      
      Serial.println("checking if chip connected");
      bool check = radio.isChipConnected();
      Serial.print("check-");
      Serial.println(check);
  
      
      radio.setPALevel(RF24_PA_MIN);
      pinMode(5, OUTPUT);
      pinMode(6, OUTPUT);
      pinMode(2, INPUT);
      pinMode(3, INPUT);
      pinMode(9, OUTPUT);
      pinMode(A1, OUTPUT);
      pinMode(7, INPUT);
      pinMode(4, INPUT);
      
      
      
}

void loop() {
   // put your main code here, to run repeatedly:
  radio.stopListening();
  int Pot = analogRead(A0); // Speed from 0 to 255
  Pot /= 4;
  Serial.println(Pot);

  // Motor A:
  if (topButton == LOW && bottomButton == HIGH)
  {
    analogWrite(motor_A_left, Pot);
    digitalWrite(motor_A_right, LOW);
  }
  else if (topButton == HIGH && bottomButton == LOW)
  {
    digitalWrite(motor_A_left, LOW);
    analogWrite(motor_A_right, Pot);
  }
  else
  {
    // Brake
    digitalWrite(5, HIGH);
    digitalWrite(6, HIGH);
  }

  // Motor B
  if (leftButton == LOW && rightButton == HIGH)
  {
    //For Clockwise motion
    analogWrite(motor_B_left, MotorB_speed);
    digitalWrite(motor_B_right, LOW);
  }
  else if (leftButton == HIGH && rightButton == LOW)
  {
    //For Anti-Clockwise motion
    digitalWrite(motor_B_left, LOW) ;
    analogWrite(motor_B_right,MotorB_speed );
  }
  else
  {
    //For brake
    digitalWrite(motor_B_left, HIGH) ;
    digitalWrite(motor_B_right, HIGH) ;
  }



      int message[4];
      
      message[0] = digitalRead(topButton);
      message[1] = digitalRead(bottomButton);
      message[2] = digitalRead(leftButton);
      message[3] = digitalRead(rightButton);
      //int i;
      //for (i = 0; i < 4; i = i + 1) {
      //      Serial.print(message[i]);
      //}
      Serial.println("Transmitted");
      radio.write(&message,sizeof(message));
      delay(100);       //You can play with this number
}

Does the receiver get the data that you send or is the problem with using it to control the car ?

Receiver

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

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

const byte address[][6] = {"00001", "00002"};

void setup() {
      //put your setup code here, to run once:
  pinMode (10,INPUT); pinMode(3,INPUT); pinMode(2,INPUT);
  radio.begin();
  radio.openWritingPipe(address[0]);
  radio.openReadingPipe(1, address[1]);
  radio.setPALevel(RF24_PA_MIN);
  Serial.begin(9600);
  
}

void loop() {
radio.startListening();
  if (radio.available()) {
    char text[32] = "";
    radio.read(&text, sizeof(text));
    Serial.println(text);
  }
}

All I'm seeing in the serial monitor is "Transmitted, and whatever number I turn my potentiometer to. Also, if I choose Nano on one screen, it automatically makes the other screen Nano, even though it's a Mega.

The transmit and the load led's are communicating on the Nano, I presume from the blinking. I thought maybe the receive would be blinking on the Mega, but it is not.

So nothing prints on the Mega ?

Try the examples and test sketch in here
Simple nRF24L01+ 2.4GHz transceiver demo
They are known to work

No sir. So with the demo's, I just open two new windows and run them right? I'll just do it in the morning. Thanks sir :grin:.

I have my nrf24l01 connected directly to my Mega, as well as the dc motor driver.

Open 2 instances of the IDE (not File/New) so that you can use a different board and COM port in each

Oh so you're saying open the same file twice?

What do you mean different board? I only have the two.

No. Open 2 separate instances of the Arduino IDE. In each, run the CheckConnection sketch from post #30 (link back in post #6) on both of your boards to make sure that you have the correct values for CE_PIN and CSN_PIN.

Post the output of the IDE serial monitor for both Arduinos if you are not sure if the connection is working.

Then load the example sketches from post #2 (again, link back in post #6). Load SimpleTx into one of your Arduinos (in one IDE instance), and SimpleRx into the other one (in the other IDE instance).

Make any necessary changes for your particular setups for the values of CE_PIN and CSN_PIN in both of the sketches.

Check that the 2 setups can communicate.

Report back if you need help.

Oh, so open another Arduino IDE, with the one I'm working on still open? That's what I'm starting to do now.

Also, when I go to tools, like I've mentioned before, I can't select different Arduinos for my boards. They both say the same board type.

You should be able to go to Tool->Board and select a different board on the second IDE.

The boards can be of the same type or different. By opening a separate instance of the IDE for each you are able to set the COM port and board type for each, if different, which yours are

Remember I said explicitly

Have you got 2 instances, one for the Tx and one for the Rx working now ?

Sorry to respond so late, something happened to my account so I'm a Newbie now :grin:. Which limits my replies. I can't make anymore replies for another 4 hours. I think my badge upgraded, so I'm trying with this post now :grin:.
Yeah, I can get a Tx, and Rx screen open now for two different Arduinos. However, my Tx, transmission is failing.

I've been browsing some forums, and saw something about a root port usb, so I switched the usb my Mega was on. Once I did that, the receiver started receiving data, but the transmitter continues to fail.

I did the two tests as mentioned, from the tutorial. It's shows the Tx failing. The first only showed the dynpd/feature as 0×00. I thought I saved my draft, before the technicality :grin:. It was a diagnosis page for how many 0x00's, and 0xff's I had.

I don't know if you saw my replies on having technicalities with responding :grin:. I tried to save my drafts, but they're not coming up. I did those two tests, and the first only had one 0x00, and no 0xff's. The 0x00 was on dynpd/feature. It says 0x00 twice. The second test says Tx failed, and I was able to set a different Arduino for each screen :grin:. I wasn't able to when I do my rc car though.

In order for the receiver to receive data, the transmitter has to be working.

Can you expand on:

Are you able to post the results of the CheckConnection sketch. This needs to be correct before continuing.