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