NRF24L01 RX-TX FOR MOTORS

I sketch below to a rc boat
RX

// It is necessary to install a library for the radio module nRF24
#include <Servo.h>
#include <SPI.h>
#include "RF24.h"
Servo servo1;

RF24 radio(9,10);
const uint64_t pipe = 0xF0F0F0F000LL; 
int msg[1]; 
int data; 
int pos;
int fspeed;           // forward speed 
int bspeed;           // backward speed
const int in1 = 5;    // direction pin 1
const int in2 = 6;    // direction pin 2
//const int ena = 5;    // PWM pin to change speed MOTOR DRIVER L298n
int reset;
void setup()
{
 
 pinMode(in1, OUTPUT);      // connection to L298n
 pinMode(in2, OUTPUT);      // connection to L298n
 //pinMode(ena, OUTPUT);      // connection to L298n
servo1.attach(3); 
delay(50);
//delay(5);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.setPALevel ( RF24_PA_MAX ) ;
//RF24_PA_MIN = -18dBm, RF24_PA_LOW = -12dBm, RF24_PA_HIGH = -6dBm, RF24_PA_MAX = 0dBm
radio.setDataRate(RF24_1MBPS);
//speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps. 
radio.startListening();



}

void loop()
{
 if (radio.available())
 radio.read(msg, 1);

 
 if (msg[0] >=0 && msg[0] <=127)data = msg[0], pos = map(data, 0, 127, 30, 150),servo1.write(pos);
 
 
 if (msg[0] >=128 && msg[0] <=189)data = msg[0], bspeed = map(data, 189, 128, 0, 255), backward(bspeed);
 
 if (msg[0] >=201 && msg[0] <=255)data = msg[0], fspeed = map(data, 255, 201, 255, 0), forward(fspeed);
 
 if (msg[0] >190 && msg[0] <200)data = msg[0], stop();
 
}

void stop()
{
 
 //analogWrite(ena, 0);
 analogWrite(in1, 0);
 analogWrite(in2, 0);
}

void forward(int fspeed)
{
 
 analogWrite(in1, fspeed);
 analogWrite(in2, 0);
 //analogWrite(ena, fspeed);
}

void backward(int bspeed)
{
 
 analogWrite(in1, 0);
 analogWrite(in2, bspeed);
 //analogWrite(ena, bspeed);
}

The problem is that when the boat comes out of the TX range it does not stop but it’s still going. That is, it performs the last command received at infinite.
I’d like to stop when it comes out of TX’s range!
Thank you to the person who helps me with the change of the sketch!

Below I put the sketch of TX

#include <SPI.h>
#include "RF24.h" 
RF24 radio(9,10);
const uint64_t pipe = 0xF0F0F0F000LL; 
int msg[1]; 
int potpin_1 = A0; 
int val_1; 
int potpin_2 = A1; 
int val_2; 

void setup(void){
radio.begin();
radio.openWritingPipe(pipe); 
radio.setPALevel ( RF24_PA_MAX ) ;
//RF24_PA_MIN = -18dBm, RF24_PA_LOW = -12dBm, RF24_PA_HIGH = -6dBm, RF24_PA_MAX = 0dBm
radio.setDataRate(RF24_1MBPS);
//speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps.
}
void loop() 
{
val_1 = analogRead(potpin_1),val_1 = map(val_1, 0, 1023, 0, 127),msg[0] = val_1,radio.write(msg, 1);
val_2 = analogRead(potpin_2),val_2 = map(val_2, 0, 1023, 128, 255),msg[0] = val_2,radio.write(msg, 1);

}

You should edit your post to use code-tags so it is readable; the code is hard to follow otherwise, and I completely missed your question the first time reading through.

I would recommend setting up a timer on the receiver to clock how long its been since a message was received and kills the motors in the event of a time-out.

In my radio control projects I make my Tx send a message a regular intervals even if the data does not change - 5 to 10 times per second seems to be sufficiently responsive. That way the Rx knows when to expect the next message and if nothing arrives for (say) 1 second it shuts off power to the motor.

...R
Simple nRF24L01+ Tutorial

If someone were to change my sketch in the sense I would be very grateful. Thank you!

marik2500:
If someone were to change my sketch in the sense I would be very grateful. Thank you!

That's your role. If you make changes and run into a problem we will try to help.

If you want someone to write a program for you please ask in the Gigs and Collaborations section of the Forum and be prepared to pay.

...R

Marik2500, are you familiar with the use of millis() instead of delay()? That will be your friend here.

Thanks, 'silly_cone,' you kindled a very good idea in my mind. If it does good, I will be very happy.

You're very welcome! :slight_smile:

Once you make a go at implementing that strategy, I'll be happy to give you additional feedback or pointers.