How to control the motors by potentiometer one on one with nRF24L01?

Thread Starter

jacksletter2002

Joined Feb 24, 2018
2
Hello everyone!
I need some help.
I had tried to control two brushless motor by two potentiometer, and had succeeded.
Now, I want to sent the signal of potentiometer by nRF24L01.
The wiring schemes and codes like the photos.
The problem is that when I turn on the "potentiometer 1", the "motor 1" doesn't move.
When I turn on the "potentiometer 2", the "motor 2" doesn't move too.
when I turn on the "potentiometer 1" and "potentiometer 2" simultaneously, the "motor 1" doesn't move, but the "motor 2" is move.
I want to control the motors one on one.
How can I fix it?




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

RF24 radio(7, 8); // CE, CSN
const byte addresses[] = "1Node";

void setup() {
  radio.begin();
  radio.setChannel(83);
  radio.openWritingPipe(addresses);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();
}

void loop() {
  delay(5);
  int potValue1 = analogRead(A1);
  int angleValue1 = map(potValue1, 0, 1023, 90, 180);
  radio.write(&angleValue1, sizeof(angleValue1));
  int potValue2 = analogRead(A2);
  int angleValue2 = map(potValue2, 0, 1023, 90, 180);
  radio.write(&angleValue2, sizeof(angleValue2));
}

Receiver
Code:
#include <SPI.h>
#include <RF24.h>
#include <Servo.h>

RF24 radio(7, 8); // CE, CSN
const byte addresses[] = "1Node";
const byte pipe = 1;
Servo myServo1;
Servo myServo2;

void setup() {
  myServo1.attach(10);
  myServo2.attach(11);
  radio.begin();
  radio.setChannel(83);
  radio.setPALevel(RF24_PA_MIN);
  radio.openReadingPipe(pipe, addresses);
  radio.startListening();
}

void loop() {
  if (radio.available()) {
    int angleValue1 = 0;
    radio.read(&angleValue1, sizeof(angleValue1));
    myServo1.write(angleValue1);
    int angleValue2 = 0;
    radio.read(&angleValue2, sizeof(angleValue2));
    myServo2.write(angleValue2);
    }
}
 
Last edited:

Mark Hughes

Joined Jun 14, 2016
409
@Jackson Mwangili,
Thanks for joining the site! Also, that for providing such thorough information. My initial thought in the comment section was that you were driving the motors with the servo signal -- I can see from your diagrams that I was wrong about that. You're diagram indicates that you're passing it through a Battery Eliminator Circuit. Can you upload the datasheet for that or a part number for reference?
Next -- do you have access to an oscilloscope? I'd like to confirm that there is a pulsed signal coming out of pins 9 & 10 of your receiver arduino. (BTW -- your receiver diagram uses pins 9&10, but your receiver code uses pins 10&11).
Also -- put some debug statements in your code so you can keep track of values to see where the breakdown is occuring. Use print statements all over the dang place.
For example:
Code:
void loop() {
  if (radio.available()) {
    int angleValue1 = 0; // I'd move this into the setup function
    radio.read(&angleValue1, sizeof(angleValue1));
   print("angleValue1=");println(angleValue1);
    myServo1.write(angleValue1);
    int angleValue2 = 0; // Move to setup function
    radio.read(&angleValue2, sizeof(angleValue2));
    myServo2.write(angleValue2);
print("angleValue2=");println(angleValue2);
    }
}
The reason for the debug statements is twofold -- first I'd like to know that the pointers are working correctly. Second, I'd like to know that the values are being passed from the transmitter arduino to the receiver arduino correctly.

My last bit of advice, regardless of what the project is, is always double-check wiring and connections. Most problems can be traced back to that.

@Raymond Genovese , @ericgibbs -- do you have any other thoughts?
Best,
Mark

@DickCappels -- should we keep this thread here or move it to Arduino so it gets a few more views?
 

Thread Starter

jacksletter2002

Joined Feb 24, 2018
2
Hello @Mark Hughes
I think I have solved the problem.
Somebody suggested me that I should send the values in single package at the same time. I rewrite the code, which add a "struct" to combined two values into one message. Now, I can control the motors by potentiometer one on one.

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

RF24 radio(7, 8); // 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(A1);
  int potValue_2 = analogRead(A2);
  myData.angleValue_1 = map(potValue_1, 0, 1023, 90, 180);
  myData.angleValue_2 = map(potValue_2, 0, 1023, 90, 180);
  radio.write( &myData, sizeof(myData));
}
Receive
Code:
#include <SPI.h>
#include <RF24.h>
#include <Servo.h>

RF24 radio(7, 8); // 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(5);
  myServo_2.attach(6);
  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);
      }
    }
}
 
Top