Arduino Pro Mini as flight controller and receiver help!

Is there anyone that can help with how to make an Arduino pro mini a receiver and flight controller. I managed to control so that the drone goes up and down using a home-built Arduino transmitter but I have no idea how to get it to go forward, backward, left, and right by tilting using a gyro-521. Any help will be very much appreciated!

My code that made it go upwards and downwards.

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

RF24 radio(3, 2); // CE, CSN
const byte address[6] = "00001";
unsigned long lastReceiveTime = 0; 
unsigned long currentTime = 0; 
Servo esc;
Servo esc2;
Servo esc3;
Servo esc4;

// Max size of this struct is 32 bytes - NRF24L01 buffer limit
struct Received_data {
  byte j1PotX;
  byte j1PotY;
  byte j2PotX;
  byte j2PotY;
  byte pot1;
  byte pot2;
};

Received_data received_data; //Create a variable with the above structure

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(110); //set the channel
  radio.openReadingPipe(0, address);
  radio.setPALevel(RF24_PA_MIN);
  radio.startListening();
  esc.attach(10);
  esc2.attach(4);
  esc3.attach(5);
  esc4.attach(6);
}
void loop() {
  if (radio.available()) {
    while (radio.available()) {
      int angleV = 0; 
      radio.read(&angleV, sizeof(angleV));
      Serial.println(angleV);
      esc.write(angleV);
      esc2.write(angleV);
      esc3.write(angleV);
      esc4.write(angleV);
      }
  }
}

and the transmitter works like this.

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

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

float elapsedTime, currentTime, previousTime;

struct Data_Package {
  byte j1PotX;
  byte j1PotY;
  byte j2PotX;
  byte j2PotY;
  byte pot1;
  byte pot2;
};

Data_Package data; // Create a variable with above structure 

void setup() {
  Serial.begin(9600);
  radio.begin();
  radio.setDataRate(RF24_250KBPS);
  radio.setChannel(110); //set the channel
  radio.openWritingPipe(address);
  radio.setPALevel(RF24_PA_MIN);
  radio.stopListening();

  data.j1PotX = 127;
  data.j1PotY = 127;
  data.j2PotX = 127;
  data.j2PotY = 127;
  data.pot1 = 1;
  data.pot2 = 1;  
}

void loop() {
  data.j1PotX = map(analogRead(A1), 0, 1023, 0, 255); // Convert the analog read value from 0 to 1023 into a BYTE from 0 to 255
  data.j1PotY = map(analogRead(A0), 0, 1023, 0, 255);
  data.j2PotX = map(analogRead(A2), 0, 1023, 0, 255);
  data.j2PotY = map(analogRead(A3), 0, 1023, 0, 255);
  data.pot1 = map(analogRead(A7), 0, 1023, 0, 180);
  data.pot2 = map(analogRead(A6), 0, 1023, 0, 180);

  int potValue = analogRead(A7);
  int angleValue = map(potValue, 0, 1023, 0, 255);
  radio.write(&angleValue, sizeof(angleValue));
}

I know right now I'm not using the data package but this was just to test that I could control anything.

Thanks in advance!

Why not transmit the entire record of X, Y and height each time? You only send one parameter and I suppose You use that for height control.

Well right now I actually didnt use the gy-521 because I hadnt souldered it on yet I just used inputs from my transmitter to controll the power on the escs. But I need to use the gyro to fly it and control it safely but I have no idea how to do it.

That is outside my comfort zone, how to combine X, Y and up/down with a gyro.
I would guess that doing some filtering, make a dead band near the joy stick center, and some calculations, scale X and Y into reasonable, safely usable numbers, should be done in the transmitter. This gives the receiver loop less work and it can control the flight.
As a start I suggest transmitting of "all" parameters, and receiving them. Then debug, verify that the data are received correctly.
Putting the new data in action is a later step.

Thank you very much that is what I will do and when or if I do run into to problems I will return with a new post with a more precise question. <3

Or continue this rather well presented topic....

Yes :slight_smile:

You state “ I managed to control so that the drone goes up and down…”

Please describe the aircraft or vehicle that you accomplished this with. I cannot interpret your claim in any way that makes it plausible.

If you just say it goes up and down and you can control that, then I will ask you to put a video of that somewhere so we can all see it!

a7

@alto777
Read the code and You will see that all OP is trying is just that....

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