/* here you go..... */
/*
EDITED BY H49, THE ORIGNAL RECEIVER CODE IS MADE BY ELETRONOOBS
CHECK OUT HIS YOUTUBE CHANNEL FOR THE TRANSMITTER PART
PLEASE READ EACH AND EVERY COMMENTS FOR YOUR COMFORT
**FOR NRF24LO1
GND -> GND
Vcc -> 3.3V
CE -> D9
CSN -> D10
CLK -> D13
MOSI -> D11
MISO -> D12
**ESC -> D2
**SERVO -> D5,D6
*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h> //To create PWM signals we need this library
//Set the throw angle of servo,from 0 to 180 degree , this will allow to set the throws for elevator
#define servoUpRange 180
#define servoDownRange 0
//the below values are for the mixing, set it as per your requirement, this will allow to set the throws for ailerons
#define servoMaxRange 135
#define servoMidRange 67.5
#define servoMinRange 0
/*
*************DONT FORGET TO CHANGE THIS in the transmitter*******************
*/
const uint64_t pipeIn = 0xE8E8F0F0E1LL;//Remember that this code is the same as in the transmitter
RF24 radio(9, 10); //CSN = 9 and CE = 10 pins, you can set any digital pin
// The sizeof this struct should not exceed 32 bytes
struct Received_data {
byte ch1;
byte ch2;
byte ch3;
};
Received_data received_data;
Servo channel_1;
//left right servo is just for name, in case if you get reversed output just change the servo pins
Servo left; //left servo
Servo right; //right servo
// DO NOT CHANGE ANY VALUE HERE
word sensorValue1 = 0; // Read raw value for up/down motion for elevons
word sensorValue2 = 0; // Read raw value for roll, left/right motion for elevons
byte leftElevator = 0;
byte rightElevator = 0;
byte roll = 0;
byte leftServo = 0;
byte rightServo = 0;
byte rollLeft = 0;
byte rollRight = 0;
int ch1_value = 0;
int ch2_value = 0;
int ch3_value = 0;
void reset_the_Data()
{
// 'safe' values to use when NO radio input is detected
received_data.ch1 = 0; //Throttle (channel 1) to 0
received_data.ch2 = 127;
received_data.ch3 = 127;
}
/**************************************************/
void setup()
{
//Attach the servo signal on pins from D2 to D8
channel_1.attach(2);
left.attach(5); // Attach servo to PWM pin 9
right.attach(6); // Attach servo to PWM pin 6
//We reset the received values
reset_the_Data();
//Once again, begin and radio configuration
radio.begin();
radio.setAutoAck(false);
radio.setDataRate(RF24_250KBPS);
radio.openReadingPipe(1,pipeIn);
//We start the radio comunication
radio.startListening();
}
/**************************************************/
unsigned long lastRecvTime = 0;
//We create the function that will read the data each certain time
void receive_the_data()
{
while ( radio.available() ) {
radio.read(&received_data, sizeof(Received_data));
lastRecvTime = millis(); //Here we receive the data
}
}
/**************************************************/
void loop()
{
//Receive the radio data
receive_the_data();
//////////This small if will reset the data if signal is lost for 1 sec.
/////////////////////////////////////////////////////////////////////////
unsigned long now = millis();
if ( now - lastRecvTime > 1000 ) {
// signal lost?
reset_the_Data();
//Go up and change the initial values if you want depending on
//your aplications. Put 0 for throttle in case of drones so it won't
//fly away
}
ch1_value = map(received_data.ch1,0,255,1000,2000);
sensorValue1 = received_data.ch3; //
sensorValue2 = received_data.ch2; // Read raw value from 0 TO 255
//Create the PWM signals
channel_1.writeMicroseconds(ch1_value); // THIS SIGNAL IS FOR ESC
////////////////////HERE WE DO MATH OPRATION FOR SERVO MIXING//////////////////////////////////
leftElevator = map(sensorValue1, 0, 255, servoDownRange, servoUpRange); //
roll = map(sensorValue2, 0, 255, servoMinRange, servoMaxRange); // MAPPING THE RECEIVED VALUES TO OUR PRE DEFINE VALUES
rightElevator = map(sensorValue1, 0, 255, servoUpRange, servoDownRange); //
rollLeft = constrain(roll, servoMidRange, servoMaxRange); //
rollLeft = map(rollLeft, servoMidRange, servoMaxRange, servoMinRange, servoMidRange); //IF THIS CONFUSED YOU, GO TO //
https://www.arduino.cc/reference/en/language/functions/math/map/
rollRight = constrain(roll, servoMinRange, servoMidRange); //FOR MORE INFO..
rollRight = map(rollRight, servoMinRange, servoMidRange, servoMidRange, servoMinRange); //
leftServo = constrain((leftElevator-rollLeft+rollRight), servoDownRange, servoUpRange);
rightServo = constrain((rightElevator+rollRight-rollLeft), servoDownRange, servoUpRange);
//////////////////////////////// MIXING DONE///////////////////////
left.write(leftServo); //
right.write(rightServo); // WRITING THE SERVO SIGNAL
}//Loop end