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!