How to control two servo motor each with a potentiometer controlled wirlessly through NRF24L01?

Thread Starter

shrayanshsharma

Joined Aug 1, 2022
1
I was trying to control pan tilt wirelessly using NRF24L01+ , two potentiometer and arduino uno. I was using this code:-

Transmitter
Code:
#include <SPI.h>
#include <RF24.h>

RF24 radio(9, 10); // CE, CSN
const byte addresses[] = {"1Node"};
struct dataStruct {
  int angleValue_1;       
  int angleValue_2;
  } myData;

void setup() {
  radio.begin();
  radio.setChannel(83);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(addresses[0]);
  radio.stopListening();
}

void loop() {
  int potValue_1 = analogRead(A0);
  int potValue_2 = analogRead(A1);
  myData.angleValue_1 = map(potValue_1, 0, 1023, 0, 180);
  myData.angleValue_2 = map(potValue_2, 0, 1023, 0, 180);
  radio.write( &myData, sizeof(myData));
}
Receiver
Code:
#include <SPI.h>
#include <RF24.h>
#include <Servo.h>

RF24 radio(9, 10); // CE, CSN
const byte addresses[] = "1Node";
const byte pipe = 1;
Servo myServo_1;
Servo myServo_2;
struct dataStruct {
  int angleValue_1;        
  int angleValue_2;
  } myData;

void setup() {
  myServo_1.attach(7);
  myServo_2.attach(8);
  radio.begin();
  radio.setChannel(83);
  radio.setDataRate(RF24_250KBPS);
  radio.setPALevel(RF24_PA_LOW);
  radio.openReadingPipe(pipe, addresses[0]);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    while (radio.available()) {
      radio.read( &myData, sizeof(myData) );
      myServo_1.write(myData.angleValue_1);
      myServo_2.write(myData.angleValue_2);
      }
    }
}
The problem what I am facing with this code is only one servo motor is controlled that too with both potentiometer instead of one potentiometer for each servo. Please help me for fixing this issue.
 

KeithWalker

Joined Jul 10, 2017
3,050
In the data you are transmitting, you do not identify which data belongs to which servo.
I suggest that separate the data you are transmitting into two consecutive packages. Include a servo identifier in the data for each servo.
At the receiving end, as you get each package, read the identifier and direct the data to the correct servo.
 
Top