Servo Control over Router using NRF24

Hi, I want to control the servo remotely with the joystick. But there will be some differences. There will be a circuit set up with arduino and nrf24 that acts as a router between the transmitter and receiver circuits to control the servom. simply; Instead of transferring the data from my joystick module directly to the transmitter and the servo, I will establish a relay communication system and communicate through it. I worked on it and my coordinator, router and endpoint codes were as I mentioned in the appendix. Do you think there is an error in these codes? in any simple digital signal or analog signal conversion parts? In general, can I achieve this with this code logic with nrf24?
---Joystick-Arduino Code---

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
RF24 radio(9, 10); // CSN, CE
const byte address[6] = "00001";

int x_key = A1;                                               
int y_key = A0;                                               
int x_pos;
int y_pos;

void setup() {
  
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
  pinMode (x_key, INPUT) ;                     
  pinMode (y_key, INPUT) ;
}
void loop() {
  x_pos = analogRead (x_key) ;  
  y_pos = analogRead (y_key) ;   
  radio.write(&x_pos, sizeof(x_pos));
  delay(100);
}

-- Router Code ( Only Arduino and Nrf24 circuit)--

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int x_pos ;
RF24 radio(9, 10); // CE, CSN
const byte addresses [][6] = {"00001", "00002"};  //Setting the two addresses. One for transmitting and one for receiving

void setup() {
  
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(addresses[1]);     //Setting the address at which we will send the data
  radio.openReadingPipe(1, addresses[0]);  //Setting the address at which we will receive the data
  radio.setPALevel(RF24_PA_MIN); //You can set it as minimum or maximum depending on the distance between the transmitter and receiver.  
}
void loop() {
  delay(100);
  radio.startListening();            //This sets the module as receiver
   if (radio.available()){           //Looking for incoming data
    int x_pos ;
    radio.read(&x_pos, sizeof(x_pos));
    Serial.println(x_pos);
    x_pos = map(x_pos, 0, 1023, 0, 180);
   }
  radio.stopListening();             //This sets the module as transmitter
  radio.write(&x_pos, sizeof(x_pos));//Sending the data
}

-- Endpoint ( Servo-Arduino Circiut Code)--

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo servo;
int servo_pin = 6;
RF24 radio(9, 10); // CSN, CE
const byte address[6] = "00002";

void setup() {
  Serial.begin(9600);
  radio.begin();
  servo.attach (servo_pin ) ; 
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  }
void loop() {
  if (radio.available()) {
    int x_pos ;
    radio.read(&x_pos, sizeof(x_pos));
    Serial.println(x_pos);
    x_pos = map(x_pos, 0, 1023, 0, 180);
    if (x_pos>400 && x_pos<600)
    {
      
    }
    else{
    servo.write (x_pos) ;
    }
  }
}

Thanks a lot :slight_smile:

Why do you bother with y if you only send x?

In your router x_pos is local to the if, can’t use it outside that if and expect the value to be there… you’ll be using the global one. Read about variable scope

In the end point why do you map again?

oh, yes I added that even though I don't need the value of y, I will delete it from there, but when you say other things I am a little lacking, I have deficiencies in those parts because I have turned to the communication protocol, do I have a problem only at the endpoint ? I would be glad if you could make a little more explanation by manipulating the codes. finally, i can not understand what you want to say in "In your router x_pos is local to the if, can’t use it outside that if and expect the value to be there" .

Did you read about variable scope?

Look at the code, x_pos is declared twice, once as a global variable and once as a local variable within your if statement. That means the x_pos within the if is a different variable than the global one, although they have the same name.

Drop the int in front of x_pos in the if statement to use the global variable

—-
The other comment regards using this twice
x_pos = map(x_pos, 0, 1023, 0, 180);

I will consider them, but the main point I want to ask is; Your thoughts on communication codes? Can I provide triple communication with this kind of logic?

Yes if you fix the bugs :slight_smile:

I’m not sure what’s the point for the middle man - may be distance ? There are mesh libraries that would take care or routing messages between nodes to their destinations.

So basically, your design is to shoot a message out and never care if it ever gets to it's destination with no corruption, and never cares if the command is executed.
Paul

yes, actually a prototype project that I was considering for distance. I examined the mesh library, but I didn't need it because mine requires a simpler system. I just needed to transmit the received signal.

I want commands to be transferred to the recipient properly and intact. What additions do I have to make to achieve this?

You need to use a CRCC on each message and expect a reply from the end recipient that the message was received correctly or that message needs to be repeated if it was not received correctly. You also need to have a definite time period that you wait for the reply and possibly resend the last message when that time-out occurs. Keep track of the number of retries so an alarm can be raised when the limit is exceeded.

RF24 does a lot of this for you but just for one hop
What is missing is propagation back to the originator if the message cannot be sent by one of the middleman

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