Go Down

Topic: NRF24L01 and Servo!! (Read 56 times) previous topic - next topic

DimitrisTzam

Hi I have an arduino uno, an arduino nano, two nrf24l01, one pot and a servo.
I want to control the servo wireless via pot and nrf24l01 but something is wrong with my code. Please help me..
Thanks

Transmitter

Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
#define JOYSTICK_X A0


const uint64_t pipe = 0xE8E8F0F0E1LL;

RF24 radio(CE_PIN, CSN_PIN);
int joystick[1];

void setup()
{
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(pipe);
}

void loop()
{
joystick[0] = analogRead(JOYSTICK_X);
radio.write( joystick, sizeof(joystick) );
}


Receiver:

Code: [Select]

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
#define CE_PIN 9
#define CSN_PIN 10
int val;
Servo myservo;
const uint64_t pipe = 0xE8E8F0F0E1LL;

RF24 radio(CE_PIN, CSN_PIN);

int joystick[1];

void setup()
{
 myservo.attach(2);
Serial.begin(9600);
delay(1000);
Serial.println("Nrf24L01 Receiver Starting");
radio.begin();
radio.openReadingPipe(1,pipe);
radio.startListening();;
}

void loop()
{
if ( radio.available() )
{
bool done = false;
while (!done)
{
done = radio.read( joystick, sizeof(joystick) );
Serial.print("X = ");
Serial.print(joystick[0]);
Serial.print("\n");
val = joystick[0];
val = map(val, 0, 1023, 0, 179);
         
         
myservo.write(val); 
}
}
else
{
Serial.println("NO radio available");
}

}

jremington

What is wrong with your code?

DimitrisTzam

Code is right but I had arduino nano as receiver and servo need lot of mah as a resalt it lags
thanks for your reply :)

jremington

Use a separate power supply for the servo and connect the grounds together.

Go Up