Rc boat receiver issues (pls help!)

I have been working on a 3d printed rc boat using two dc motors and a nrf24lo1 module. I can't seem to get the code to work and it is getting very frustrating.

here is the transmitter code:



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


RF24 radio(7, 8);

byte addresses[][6] = {"1Node","2Node"};


int buttonPinL = 3;
int buttonPinR = 4;

int motorL;
int motorR;

void setup() {

  Serial.begin(9600);
  Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
  
  
  radio.begin();                    

  radio.setPALevel(RF24_PA_LOW);  

  radio.setDataRate(RF24_2MBPS);  

  radio.setChannel(124);            

  radio.openWritingPipe(addresses[1]);       
  radio.openReadingPipe(1, addresses[0]);    
  
}

void loop() {

//*********************************************************

if(digitalRead(buttonPinL)==HIGH)
  {
  byte motorL = 1;
  while(digitalRead(buttonPinL)==HIGH);
  }
  else
  {
    byte motorL = 0;
  }

//************************************************************

  if(digitalRead(buttonPinR)==HIGH)
  {
  byte motorR = 1;
  while(digitalRead(buttonPinR)==HIGH);
  }
  else
  {
    byte motorR = 0;
  }
  
//***************************************************************
  
  uint8_t data[2]; 
  
  data[0] = motorL;  

   
  data[1] = motorR;

  (radio.write( data, sizeof data));

  
  Serial.print("Sent: ");
  Serial.print(motorL);
  Serial.print(' ');
  Serial.println(motorR);
  
}

and here is the receiver code:



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

RF24 radio(7, 8);

byte addresses[][6] = {"1Node", "2Node"};

int leftMotor = 3;
int rightMotor = 4; 

void setup()
{
  Serial.begin(9600);
  Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");

  radio.begin();                   

  radio.setPALevel(RF24_PA_LOW);   

  radio.setDataRate(RF24_2MBPS); 

  radio.setChannel(124);         

  radio.openReadingPipe(1, addresses[1]);  

  radio.startListening();        

pinMode(leftMotor, OUTPUT);
pinMode(rightMotor, OUTPUT);
  
}

void loop()
{
  
  unsigned char data[2];

 
  if (radio.available())
  {
    radio.read(data, sizeof data);
    int motorL = data[0];
    int motorR = data[1];

    Serial.print("Recieved: ");
    Serial.print(motorL);
    Serial.print(' ');
    Serial.println(motorR);

//*********************************************************

if ((motorL)==1)
{
  digitalWrite(leftMotor, HIGH);
  while((motorL)==1);
}
else{

digitalWrite(leftMotor, LOW);

}

//*********************************************************

if ((motorR)==1)
{
  digitalWrite(rightMotor, HIGH);
  while((motorR)==1);
}
else{

digitalWrite(rightMotor, LOW);

}

//*********************************************************

}
}

and here are some photos of the setup:


thanks in advance!

jethro

Are you going to give us any clues as to what does not work ?

Those while loops in the receiver will hang your code forever once either is invoked.

the motors won't spin

ok, I have removed them but it still dosen't work

A schematic/circuit diagram would be more useful. If you're trying to drive those motors direct from digital pins that's never going to work and will probably damage the Arduino.

If not, how are you driving them? Do they work when you write a simple test code that just writes directly to pins 3 and 4?

Steve

Did you first establish that you can communicate between the two NRFs with simple sketches you didn’t write?

Next see if you can spin the motors the way you want to automate, using a simple program that just does that. Exercise the motor code.

If you can’t make the two parts work with simple test programs, you will have a greater deal of difficulty making the whole system work…

a7

int leftMotor = 3;

void setup() {
  // put your setup code here, to run once:
pinMode(leftMotor, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
digitalWrite(leftMotor, HIGH);
delay(1000);
digitalWrite(leftMotor, LOW);
delay(1000);
}

this is the code that I used to test the motors and they twitched a bit but didn't spin

should I use relays to trigger the motors or is there another way?

Yes you could use a relay to switch power to you motor.

You’d need to energize the relay, that cannot be powered directly by the Arduino.

You could purchase a relay module that would provide the additional circuit needed.

You could probably find a motor control module.

Or you could make your own circuit.

google

arduino dc motor control

and poke around to see all the ways ppl accomplish running motors with computers.

a7

If the motors only need to turn in one direction you could use MOSFETs. If they need to go forward/reverse then an H-bridge motor driver is what you need. There are thousands of examples of both online.

Steve

I found some mosfets and installed them correctly so the motors spin on a separate code but they dost spin with the radio code

Can you please post a copy of both your circuits, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

Have you confirmed that the data is being received by printing it ?

also I fixed some other bugs in the code but it still won't work

here is the new transmitter code :



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


RF24 radio(7, 8);

byte addresses[][6] = {"1Node","2Node"};


int buttonPinL = 3;
int buttonPinR = 4;

int motorL;
int motorR;

void setup() {

  Serial.begin(9600);
  Serial.println("THIS IS THE TRANSMITTER CODE - YOU NEED THE OTHER ARDIUNO TO SEND BACK A RESPONSE");
  
  
  radio.begin();                    

  radio.setPALevel(RF24_PA_LOW);  

  radio.setDataRate(RF24_2MBPS);  

  radio.setChannel(124);            

  radio.openWritingPipe(addresses[1]);       
  radio.openReadingPipe(1, addresses[0]);    

pinMode(buttonPinL, INPUT);
pinMode(buttonPinR, INPUT);
  
}

void loop() {

//*********************************************************

if(digitalRead(buttonPinL)==HIGH)
  {
  byte motorL = 1;
  while(digitalRead(buttonPinL)==HIGH);
  }
  else
  {
    byte motorL = 0;
  }

//************************************************************

  if(digitalRead(buttonPinR)==HIGH)
  {
  byte motorR = 1;
  while(digitalRead(buttonPinR)==HIGH);
  }
  else
  {
    byte motorR = 0;
  }
  
//***************************************************************
  
  uint8_t data[2]; 
  
  data[0] = motorL;  

   
  data[1] = motorR;

  (radio.write( data, sizeof data));

  
  Serial.print("Sent: ");
  Serial.print(motorL);
  Serial.print(' ');
  Serial.println(motorR);
  
}

and here is the new receiver code:

                                                                 


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

RF24 radio(7, 8);

byte addresses[][6] = {"1Node", "2Node"};

int leftMotor = 3;
int rightMotor = 4; 

void setup()
{
  Serial.begin(9600);
  Serial.println("THIS IS THE RECEIVER CODE - YOU NEED THE OTHER ARDUINO TO TRANSMIT");

  radio.begin();                   

  radio.setPALevel(RF24_PA_LOW);   

  radio.setDataRate(RF24_2MBPS); 

  radio.setChannel(124);         

  radio.openReadingPipe(1, addresses[1]);  

  radio.startListening();        

pinMode(leftMotor, OUTPUT);
pinMode(rightMotor, OUTPUT);
  
}

void loop()
{
  
  unsigned char data[2];

 
  if (radio.available())
  {
    radio.read(data, sizeof data);
    int motorL = data[0];
    int motorR = data[1];

    Serial.print("Recieved: ");
    Serial.print(motorL);
    Serial.print(' ');
    Serial.println(motorR);

//*********************************************************

if ((motorL)==1)
{
  digitalWrite(leftMotor, HIGH);

}
else{

digitalWrite(leftMotor, LOW);

}

//*********************************************************

if ((motorR)==1)
{
  digitalWrite(rightMotor, HIGH);

}
else{

digitalWrite(rightMotor, LOW);

}

//*********************************************************

}
}

ok I haven't checked the monitor but I can
do that

nothing is being printed so it seems nothing is being received

Hi,
Do you have a DMM?
If so can you please measure the battery voltage when you command a TX?

You appear to be using the NRF24l01 plus.
53CA2A4EDAF8C09B0423C7C64787EF0B
You would be best to keep ALL other wires away from the antenna, and point it up out of the box.
Any objects around the antennas will cause detuning of the antenna.

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

I do not have a dmm and when I suck the antenna out the box it still did not work

Hi,
Possibly time to get a cheap one with just the basics measurements.
Is that NRF with its own adapter PCB?
If so are you feeding it 5V or 3V3 at the input pins.

Tom.... :grinning: :+1: :coffee: :australia: