nRF24L01 servos controlled by buttons

I’m working on a project with 2 servos and 2 buttons. If the left button is held, it moves the servo to 180 and if released it’ll travel back to 0. The right button does the same except moves the right servo from 180 to 0 if held and back to 180 if released.

I want to make this wireless with an nRF24l01 and cant figure out how to convert my original code.

I’m new to programming so I apologize if its hard to read.

Original Code:

#include <Servo.h> 
int button=0;
int buttonR=0;
Servo leftservo;
Servo rightservo;
int pos = 0;
int posR = 180;
void setup() 
{ 
  leftservo.attach(9);
  rightservo.attach(10);
  pinMode(2, INPUT);
  pinMode(3, INPUT);
} 

void loop() 
{ 
  button=digitalRead(2);
  if(button==HIGH)
  {  
   for(pos; pos < 180&&button==HIGH; pos +=2)
   {
     leftservo.write(pos);
     button=digitalRead(2);
   } 
  }
  if(button==LOW)
  {  
    for(pos; pos>=0&&button==LOW; pos-=2)
   {                                
     leftservo.write(pos);
     button=digitalRead(2);
   } 
  }
  buttonR=digitalRead(3);
  if(buttonR==HIGH)
  {
    for(posR; posR>0&&buttonR==HIGH; posR-=2)
    {
      rightservo.write(posR);
      buttonR=digitalRead(3);
    }
  }
  if(buttonR==LOW)
  {
    for(posR; posR <= 180&&buttonR==LOW; posR +=2)
    {
      rightservo.write(posR);
      buttonR=digitalRead(3);
    }
  }
}

Transmitter attempt:

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

RF24 radio(7, 8); //CSN, CE
const byte address[6] = "00001";

boolean button = 0;
boolean buttonR = 0;

void setup()
{
  radio.begin();
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();

  pinMode(2, INPUT);
  pinMode(3, INPUT);
}

void loop()
{
  button = digitalRead(2);
  radio.write(&button, sizeof(button));
  buttonR = digitalRead(3);
  radio.write(&buttonR, sizeof(button));
}

Receiver attempt:

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

int pos = 0;
int posR = 180;
Servo leftservo;
Servo rightservo;
boolean button = 0;
boolean buttonR = 0;

RF24 radio(7, 8); //CSN, CE
const byte address[6] = "00001";

void setup()
{
  radio.begin();
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  leftservo.attach(9);
  rightservo.attach(10);
  radio.startListening();
}

void loop()
{
  while(radio.available());
  {
    radio.read(&button, sizeof(button));
    if(button == HIGH)
    {
      for(pos; pos < 180&&button==HIGH; pos +=2)
      {
        leftservo.write(pos);
      }
    }
    if(button==LOW)
    {
      for(pos; pos>=0&&button==LOW; pos-=2)
      {
        leftservo.write(pos);
      }
    }
    radio.read(&buttonR, sizeof(buttonR));
    if(buttonR == HIGH)
    {
      for(posR; posR>0&&buttonR==HIGH; posR-=2)
      {
        rightservo.write(posR);
      }
    }
    if(buttonR==LOW)
    {
      for(posR; posR <= 180&&buttonR==LOW; posR +=2)
      {
        rightservo.write(posR);
      }
    }
  }
}

Thanks in advance for your help it is much appreciated. :slight_smile:

I suggest that you use Robin2's simple rf24 tutorial to get the radios working then add the servo code.