Making arduino transmitter for drone using nRF24L01

I am struggling to write code to send the value of the potentiometer on the transmitter, over to the receiver. I don't know whether it matters when I map the value. The code works with a potentiometer and drone motor with no wireless transmission.

#include<Servo.h>
Servo esc;

void setup()
{
  esc.attach(12);
  
}

void loop()
{
  int val;
  int mappedval;
  val=analogRead(A0);
  mappedval = map(val, 560, 950, 1300, 2000);
  esc.write(mappedval);
}

Here is my code for the transmitter


#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int val;

RF24 radio(9, 10); // CE, CSN

const uint64_t pipe = 0xE8E8F0F0E1LL;

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

void loop() {
  val = map(analogRead(A0), 560, 950, 1300, 2000);
  radio.write(val, sizeof(val));
}

And receiver

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
Servo esc;
int val;
RF24 radio(9, 10); // CE, CSN

const uint64_t pipe = 0xE8E8F0F0E1LL;

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.openReadingPipe(1, pipe);
  radio.startListening();
  esc.attach(5);
}

void loop() {
  if (radio.available()) {
    radio.read(val, sizeof(val));
    Serial.println(val);
    esc.write(val);
  }
}

For the nRF24L01's work, I used Robins guide and ensured they worked there first. When running the code I get nothing in the serial monitor for the receiver and the motor does nothing.
Thanks for the help.

You made a bunch of changes that broke both programs, though.

This certainly won't work, because it lacks the "address of" operator, &. I did not look further.

  radio.write(val, sizeof(val));

When modifying someone else's programs, make changes one at a time, and make sure that the program continues to work before moving on.

You are sending at full speed, which will make it very hard for the receiver to follow.

I guess your current code sends above 1000 packets a second,
if any of your processes involves a PWM signal,
it is not useful to restart the PWM more often than a full cycle would take.

Take a peek at how iforce2D did it....few years old but works ....
Quite a few videos in the build....
https://www.youtube.com/watch?v=YPtxHw3DWrg

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.