Wireless controlling servos with potentiometer using nrf24l01

Hallo , i am trying to control servo motor by potentiometer wireless using nrf24l01

if value of potentiometer get in range (0-400) servo turn left and if value if potentiometer get in range

(700-1000) servo turn right

please help . and thanks :slight_smile:

Here is a good tutorial on the use of the rf24 radios.

How to read a potentiometer.

The if statement reference.
The else statement reference.

thanks so much . i want the servo turn right if potentiometer was in range (700-100) and turn left when potentiometer in range (0-400) .

my code for rx

/***********************************************

  • LED Connections *
  • Anode -> VCC *
  • Cathode -> GND *
  • NRF 24L01 Connection *
  • VCC -> 3.3 V *
  • GND -> GND *
  • SCK -> 13 *
  • MISO -> 12 *
  • MOSI -> 11 *
  • CSN -> 7 *
  • CE -> 8 *
    ***********************************************/

//// Adding Libraries

#include <SPI.h> // to handle the communication interface with the modem
#include <nRF24L01.h> // to handle this particular modem driver
#include <RF24.h> // the library which helps us to control the radio modem
#include <Servo.h>

Servo myservo;
RF24 radio(7,8); // Creating instance 'radio' ( CE , CSN ) CE -> D7 | CSN -> D8

const byte Address[6] = " 00009 " ; // Address from which data to be received

void setup() {
// put your setup code here, to run once:
myservo.attach(3);
Serial.begin(9600); // Setting baudrate of Serial Port to 9600

radio.begin(); // Activate the modem

radio.openReadingPipe(1, Address); // Sets the address of receiver from which program will receive the data

// radio.startListening(); // Setting modem in Receiver mode
}

void loop() {
// put your main code here, to run repeatedly:

radio.startListening();

if (radio.available())
{
while (radio.available()) // Loop until receiving valid data
{

int rx_data = 0 ; // Variable to store received data

radio.read(&rx_data, sizeof(rx_data)); // Read the received data and store in ' rx_data '

Serial.print("Received Data : ");

Serial.println(rx_data); // Print received value on Serial Monitor
myservo.write (rx_data);

// analogWrite(led_pin , rx_data); // Write received value to PWM pin 3 to which LED is connected

}
}

else
{
Serial.println("Not Receiving !!!"); // If not receiving valid data print " Not Receiving !!! " on Serial Monitor
}
///// END OF LOOP //////

code tx :

/***********************************************

  • Potentiometer Connections *
  • PIN 1 -> VCC *
  • PIN 2 -> Analog input 0 i.e. A0 *
  • PIN 3 -> GND *
  • NRF 24L01 Connection *
  • VCC -> 3.3 V *
  • GND -> GND *
  • SCK -> 13 *
  • MISO -> 12 *
  • MOSI -> 11 *
  • CSN -> 7 *
  • CE -> 8 *
    ***********************************************/

//// Adding Libraries

#include <SPI.h> // to handle the communication interface with the modem
#include <nRF24L01.h> // to handle this particular modem driver
#include <RF24.h> // the library which helps us to control the radio modem

#define pot_pin A0 // Variable pin of POT is to be connected to analog pin 0 i.e. A0

RF24 radio(7,8); // Creating instance 'radio' ( CE , CSN ) CE -> D7 | CSN -> D8

const byte Address[6] = " 00009 " ; // Address to which data to be transmitted

//int data ; // Creating variable to store and send data

int value = 0 ; // Creating variable to store analog input on pin A0

void setup() {
// put your setup code here, to run once:

Serial.begin(9600);

pinMode(pot_pin,INPUT); // Setting A0 (POT pin) as input

radio.begin (); // Activate the modem

radio.openWritingPipe (Address); // Sets the address of transmitter to which program will send the data

// radio.stopListening (); // Setting modem in transmission mode

}

void loop() {
// put your main code here, to run repeatedly:

radio.stopListening (); // Setting modem in transmission mode

value = analogRead(pot_pin); // Reading analog value at pin A0 and storing in varible 'value'

int data = map( value , 0 , 1023 , 0 , 180 ); // Convering the 10 bit value to 8 bit

radio.write(&data, sizeof(data)); // Sending data over NRF 24L01

Serial.print("Transmitting Data : ");

Serial.println(data); // Printing POT value on serial monitor

}

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask a good question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

So send the raw output of the pot analog input value ( 0 to 1023) instead of the mapped value (0 to 180).
In the receiver use a series of if statements to decode the pot value (rx_data).

if(rx_data > 0 && rx_data < 399)
{
    //turn left
}
else if(rx_data > 400 && rx_data < 699)
{
     // go striaght
}
else  // anything greater than 700
{
    // turn right   
}

logic and (&&).

but when send signal by rf24 . i receive output from 0-255 and i can not compare this value

Did you make changes to your code? If so please post the new version so that we can keep up.

Read the how to use this forum-please read sticky to see how to properly post code and some advice on how to ask a good question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

@Warawreh

DO NOT CROSS POST.
Other posts DELETED

Please read the link you were given by GroundFungus before posting any further.
Continued cross posting may result in loss of forum privileges.

BOB.