Problem with stoping RX nrf24 from listening

Hi everyone, didn't know if you see my post that is why I mentioned you.
@groundFungus
@Robin2
@sherzaad
@KrisKasprzak
@6v6gt

I'm using NRF24 module as RX and TX. My sketch is to control 2 dc motors via analog output through a 2 axis joystick. Everything is normal, it means I can control the speed and the direction of motors, but the problem starts when I cut the TX power off.
When I cut the TX module power off, the RX module remains ON and it sustains the last received analog signals before turning off the TX module. Subsequently the motor is still turning until I reconnect the TX power supply again. This can be so dangerous for my project cause I lift heavy weights and when the TX module is off the Rx outputs should be zero(Low) rapidly.

help if possible.
Regards.
Shahryar

Tx code:

#include <RF24.h>
#include <nRF24L01.h>
#include <SPI.h>
RF24 radio(9, 8);
const byte rxAddr[6] = "00001";
int DATA[2];

void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.openWritingPipe(rxAddr);
}
void loop()
{
 
  DATA[0] = analogRead(A0);
  DATA[1] = analogRead(A1);
  Serial.print("X:"); Serial.println(DATA[0]);
  Serial.print("Y:"); Serial.println(DATA[1]);
  radio.write(DATA, sizeof(DATA));
  //delay(1);
 
}

Rx:

#include <RF24.h>
#include <nRF24L01.h>
#include <SPI.h>
RF24 radio(9, 8);
const byte rxAddr[6] = "00001";
int DATA[2];

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.openReadingPipe(0, rxAddr);
  radio.startListening();
}

void loop() {
  if (radio.available()){
  radio.read(DATA, sizeof(DATA));
  
  
  if (DATA[0]>570 && 1023>=DATA[0]){
 int i=map(DATA[0], 570, 1023, 0, 255);
 analogWrite(5, i);
 analogWrite(3, 0);
 }
 if (DATA[0]<450 && DATA[0]>=0){
 int i=map(DATA[0], 450, 0, 0, 255);
 analogWrite(5, 0);
 analogWrite(3, i);
 }

if (DATA[0]>450 && DATA[0]<570){
 int i=map(DATA[0], 451, 569, 0, 0);
 analogWrite(5, 0);
 analogWrite(3, 0);
}
Serial.print("X:"); Serial.println(DATA[0]);
Serial.print("Y:"); Serial.println(DATA[1]);
  
  
}
}

You have to change the protocol between the transmitter and the receiver.
The transmitter has to continuously (say every 50ms) send a message to the receiver. The receiver acts on that message (which may be exactly the same instruction as in the previous message. If the receiver fails to get an expected message, say after 100ms, it should stop everything.

1 Like

the way you have stuctured your code, you Tx your value and the Rx reads and retains it.

if you want a 'fail-safe' cut off in case of Tx failure you can either

  1. periodically tx the same message and set a rx timout on your receiver. ie if it fails to receive anytihng after x seconds, shut down.

  2. the other way similar 1 is to set a specific 'heartbeat' message and if that fails to rx, shutdown.

hope the helps...

1 Like

Is there any function or command even tutorial to do that or I should change the cod by myself?
@sherzaad

Do you know any function inside its library?

You have to code it yourself but it should be a simple change to your existing code.
In the RX part, simply record the last transmission time. Keep testing it and if it gets too old, say > 100ms, then stop everything.
Post also your TX code.

1 Like

Thanks for responding
@6v6gt

Hi guys
I tried many codes but nothing works.
Do you have any idea what code gonna work?
One important point is that my radio.available() in receiver side is always 0, but every thing is normal and works. I know nrf_received_pwr() measures the receiving power but don't know how can I put it in my code!
@sherzaad
@6v6gt
@groundFungus
Regards
Shahryar

You were also asked to post the TX code.

Here is TX code:

#include <RF24.h>
#include <nRF24L01.h>
#include <SPI.h>
RF24 radio(9, 8);
const byte rxAddr[6] = "00001";
int DATA[2];

void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.openWritingPipe(rxAddr);
}
void loop()
{
 
  DATA[0] = analogRead(A0);
  DATA[1] = analogRead(A1);
  Serial.print("X:"); Serial.println(DATA[0]);
  Serial.print("Y:"); Serial.println(DATA[1]);
  radio.write(DATA, sizeof(DATA));
  //delay(1);
 
}

so using your RX and TX code you posted, you just need to add in a 'Timeout' code using millis() for example. ie something like this:
(Compiles, NOT tested!)
TX:

#include <RF24.h>
#include <nRF24L01.h>
#include <SPI.h>
RF24 radio(9, 8);
const byte rxAddr[6] = "00001";
int DATA[2];

void setup()
{
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.openWritingPipe(rxAddr);
}
void loop()
{
 
  DATA[0] = analogRead(A0);
  DATA[1] = analogRead(A1);
  Serial.print("X:"); Serial.println(DATA[0]);
  Serial.print("Y:"); Serial.println(DATA[1]);
  radio.write(DATA, sizeof(DATA));
  //delay(1);
 
}

RX:

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

RF24 radio(9, 8);
const byte rxAddr[6] = "00001";
const int TIMEOUT = 100; //100ms
int DATA[2];
uint32_t oldtime;

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setRetries(15, 15);
  radio.openReadingPipe(0, rxAddr);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    radio.read(DATA, sizeof(DATA));


    if (DATA[0] > 570 && 1023 >= DATA[0]) {
      int i = map(DATA[0], 570, 1023, 0, 255);
      analogWrite(5, i);
      analogWrite(3, 0);
    }
    if (DATA[0] < 450 && DATA[0] >= 0) {
      int i = map(DATA[0], 450, 0, 0, 255);
      analogWrite(5, 0);
      analogWrite(3, i);
    }

    if (DATA[0] > 450 && DATA[0] < 570) {
      int i = map(DATA[0], 451, 569, 0, 0);
      analogWrite(5, 0);
      analogWrite(3, 0);
    }
    Serial.print("X:"); Serial.println(DATA[0]);
    Serial.print("Y:"); Serial.println(DATA[1]);

    oldtime = millis(); //reset time out

  }

  //COMM failure time detection
  if (millis() - oldtime > TIMEOUT) {
    //Reset_IO();
    //write what you want to do in case of Comm failure here
  }
}

Hope that helps....

1 Like

Thanks a lot.
I'll try it and inform you the result

Why do you want to use the very high timeout?

Why do you use the atrocious baud rate?

Why is the radio.stopListening(); call missing in the transmitter's setup?
https://nrf24.github.io/RF24/classRF24.html#a6f144d73fc447c8ac2d1a4166210fd88

Why are you not interested in the result of your transmission?

Why do you send with such a high speed?

Thanks for responding and the link you've shared.

I just put this line here for increase number of retries of sending data. Should I decrease it?

I use this buad rate only for monitoring the value of A0, A1 and sometimes radio.available on the RX side.
Besides, I don't know why radio.available is zero (Low) always?

I thought the modules are on TX mode by default that's why I refused to put radio.StopListening() there.

Sorry I couldn't get this part! How could I get the results of my transmission?

I didn't choose any speed intentionaly, certainly I'll change it if its necessary.

You did not even understand the question, maybe you should really read the documentation.
https://nrf24.github.io/RF24/classRF24.html#a4c6d3959c8320e64568395f4ef507aef

That is no excuse to use a baud rate from the last century,
and you misuse it to moderate the packet rate on the tx side.

It is not in a functioning setup.

So you know better than the documentation? I doubt that.

Again, read the documentation.
https://nrf24.github.io/RF24/classRF24.html#a23bfe6502d74bb5bbccb3a7f2ba2b5ea

What speed would be appropriate?
Would ten times a second be enough?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.