nRF24L01s - I need coding help for wireless servo control.

Hello-
I found these nRF24L01s and they are cheap and do what I want, wirelessly.

I’m not a coder and a beginner. I need to have 2 potentiometers control 2 servos wirelessly. This sketch does that, but I don’t know how to tweak the serialprint to something that actually controls a servo.

I know this is probably a simple tweak of code, but I need some help.

TRANSMIT

/* YourDuinoStarter Example: nRF24L01 Transmit Joystick values
 - WHAT IT DOES: Reads Analog values on A0, A1 and transmits
   them over a nRF24L01 Radio Link to another transceiver.
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   - 
   Analog Joystick or two 10K potentiometers:
   GND to Arduino GND
   VCC to Arduino +5V
   X Pot to Arduino A0
   Y Pot to Arduino A1
   
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10
#define JOYSTICK_X A0
#define JOYSTICK_Y A1

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[2];  // 2 element array holding Joystick readings

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  radio.begin();
  radio.openWritingPipe(pipe);
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  joystick[0] = analogRead(JOYSTICK_X);
  joystick[1] = analogRead(JOYSTICK_Y);
  
  radio.write( joystick, sizeof(joystick) );

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

//NONE
//*********( THE END )***********

RECEIVE

/* YourDuinoStarter Example: nRF24L01 Receive Joystick values

 - WHAT IT DOES: Receives data from another transceiver with
   2 Analog values from a Joystick or 2 Potentiometers
   Displays received values on Serial Monitor
 - SEE the comments after "//" on each line below
 - CONNECTIONS: nRF24L01 Modules See:
 http://arduino-info.wikispaces.com/Nrf24L01-2.4GHz-HowTo
   1 - GND
   2 - VCC 3.3V !!! NOT 5V
   3 - CE to Arduino pin 9
   4 - CSN to Arduino pin 10
   5 - SCK to Arduino pin 13
   6 - MOSI to Arduino pin 11
   7 - MISO to Arduino pin 12
   8 - UNUSED
   
 - V1.00 11/26/13
   Based on examples at http://www.bajdi.com/
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10

// NOTE: the "LL" at the end of the constant is "LongLong" type
const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe


/*-----( Declare objects )-----*/
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
/*-----( Declare Variables )-----*/
int joystick[2];  // 2 element array holding Joystick readings

void setup()   /****** SETUP: RUNS ONCE ******/
{
  Serial.begin(9600);
  delay(1000);
  Serial.println("Nrf24L01 Receiver Starting");
  radio.begin();
  radio.openReadingPipe(1,pipe);
  radio.startListening();;
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  if ( radio.available() )
  {
    // Read the data payload until we've received everything
    bool done = false;
    while (!done)
    {
      // Fetch the data payload
      done = radio.read( joystick, sizeof(joystick) );
      Serial.print("X = ");
      Serial.print(joystick[0]);
      Serial.print(" Y = ");      
      Serial.println(joystick[1]);
    }
  }
  else
  {    
      Serial.println("No radio available");
  }

}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

//NONE

Hi stevex

Sounds like you have the two potentiometer values being received correctly via the nRF24L01s? Next step, I would suggest, is to focus on the servos, without the nRF24L01s initially. Get two servos working reliably and then add the nRF24L01s back in.

Try the ServoMoodIndicator example program in the Arduino IDE (it's in the starter kit section). This controls one servo position from one potentiometer. It shows how to define a servo in a program and map from the range of values you get from analogRead() to the range of positions you can send to a servo.

Once you have one servo working, try extending the program to handle two servos. You will need to define two servos (on different pins) and two potentiometers.

You'll need a separate power supply for the servos (but with GND connected to Arduino GND). Servos can draw high current which can cause problems to the Arduino.

Once you have two servos working, you can add parts of that program to your nRF24L01 receive program. Instead of using the analogRead() results to control the servo, your program will use the joystick[0] and joystick[1] values you are already receiving.

Regards

Ray

omg. I think I got it. Thank you.

I will post my final sketch when I get it fully ironed out.

yay.

y…es posible controlar 4 servos con potenciometros con este modulo… yo no puedo hacerlo ya realize los codigos de TX y RX y solo funciona para dos!!! tal vez mi codigo este malo… alguien podria ayudarme a solucionarlo? gracias dejo los dos codigod para que los vean.

TX

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

RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int msg[1];
int potpin_1 = A0;
int val_1;
int potpin_2 = A1;
int val_2;
int potpin_3= A2;
int val_3;
int potpin_4= A3;
int val_4;

void setup(void)
{
radio.begin();
radio.openWritingPipe(pipe);
Serial.begin(9600);
}
void loop()
{
val_1 = analogRead(potpin_1);
val_1 = map(val_1, 0, 1023, 0, 127);
msg[0] = val_1;
radio.write(msg, 1);
Serial.print(“Valor Val_1:”);
Serial.println(val_1);
delay(300);

val_2 = analogRead(potpin_2);
val_2 = map(val_2, 0, 1023, 128, 255);
msg[0] = val_2;
radio.write(msg, 1);
Serial.print(“Valor Val_2:”);
Serial.println(val_2);
delay(300);

val_3 = analogRead(potpin_3);
val_3 = map(val_3, 0, 1023, 0, 127);
msg[0] = val_3;
radio.write(msg, 1);
Serial.print(“Valor Val_3:”);
Serial.println(val_3);
delay(300);

val_4 = analogRead(potpin_4);
val_4 = map(val_4, 0, 1023, 0, 127);
msg[0] = val_4;
radio.write(msg, 1);
Serial.print(“Valor Val_4:”);
Serial.println(val_4);
delay(300);
}

RX

#include <SPI.h>
#include <RH_RF24.h>

#include <Servo.h>
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

RF24 radio(9,10);
const uint64_t pipe = 0xE8E8F0F0E1LL;
int msg[1];
int data;
int pos1;
int pos2;
int pos3;
int pos4;
void setup()
{
servo1.attach(3);
servo2.attach(7);
servo3.attach(5);
servo4.attach(6);
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();
}
void loop()
{
if (radio.available())radio.read(msg, 1);
if (msg[0] <128 && msg[0] >-1)data = msg[0], pos1 = map(data, 10, 254, 7, 177),servo1.write(pos1);
if (msg[0] >127 && msg[0] <255)data = msg[0], pos2 = map(data, 10, 254, 9, 177),servo2.write(pos2);
if (msg[0] >127 && msg[0] <255)data = msg[0], pos3 = map(data, 10, 254, 9, 177),servo3.write(pos3);
if (msg[0] >127 && msg[0] <255)data = msg[0], pos4 = map(data, 10, 254, 9, 177),servo4.write(pos4);
}