Before I start, I just want to say that as much as I want to test this, I cant. I am missing parts.
Anyways, the code shown is quite a large improvement to my first attempt at my quadcopter code.
I feel that, since I am a intermediate programmer, this code is rubbish. I think I need the help of someone with more programming experience
I also feel that someone here can help me get smaller code
The !radio.write() is for if it doesn't send, and the transmitter fails or quadcopter is out of range
Lastly, if you're wondering why I am using values 10000, it's like an encoding scheme I thought of. So if the Rx receives a value around 1000,it will be roll. In Rx, if such a value is received, it is.subtracted by 1000, leaving me with whatever servo signal I intend to send (so 1080-1000=80; motors react to that
Here it is. Please don't judge! They're only like less than 80% done.
Transmitter
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
int
LeftStickX = 14, //A0
LeftStickY = 15, //A1
RightStickX = 16, //A2
RightStickY = 17; //A3
const byte address[5] = {'R','o','M','e','O'};
RF24 radio(9, 10);
long command;
bool sending;
void setup()
{
pinMode(LeftStickX, INPUT);
pinMode(LeftStickY, INPUT);
pinMode(RightStickX, INPUT);
pinMode(RightStickY, INPUT);
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.setRetries(2, 15)
radio.openWritingPipe(address);
}
void loop()
{
char testingConnection= "CONNECT TEST";
bool connected = false;
bool test = radio.write(connectTest, &sizeof(connectTest));
if(test && connected = false)
{
//BEEEEEEP
bool connected = true
}
else
{
while(connected = false)
{
// BEEP BEEP
}
}
//Out of range test
int currentTime = millis();
int prevTime;
if(currentTime - prevTime >= 1000)
{
send();
prevTime = millis();
}
}
}
void send()
{
/*
this is where potentiometer is
read and sent
*/
int
THROTTLE,
ROLL,
PITCH,
YAW;
int
RAW_THROTTLE,
RAW_ROLL,
RAW_PITCH,
RAW_YAW;
RAW_YAW =
analogRead(LeftStickX);
YAW =
map(RAW_YAW, 0,1023,-20200, 20200);
RAW_THROTTLE =
analogRead(LeftStickY);
THROTTLE =
map(RAW_THROTTLE,0,1023,0,200);
RAW_ROLL =
analogRead(RightStickX);
ROLL =
map(RAW_ROLL,0,1023,-1200,1200);
RAW_PITCH =
analogRead(RightStickY);
PITCH =
map(RAW_PITCH,0,1023,-10200,10200);
radio.write(YAW, &sizeof(YAW));
radio.write(THROTTLE, &sizeof(THROTLLE));
radio.write(ROLL, &sizeof(ROLL));
radio.write(PITCH, &sizeof(PITCH));
//radio has ACK pkgs for Tx, so I use that
//Credits to Robin2. Thanks.
if(!radio.write(YAW, &sizeof(YAW)))
{
//BUZZER BEEP
}
if(!radio.write(THROTTLE, &sizeof(THROTTLE)))
{
//BUZZER BEEP
}
if(!radio.write(ROLL, &sizeof(ROLL)))
{
//BUZZER BEEP
}
if(!radio.write(PITCH, &sizeof(PITCH)))
{
//BUZZER BEEP
}
}
}
/*
void verifyPotChange()
{
supposed to record "now" pot values
so that multiple values are not sent.
with some thinking, i dont think this
is necessary. just send values
uncomment to test
long
CURRENT_THROTTLE = RAW_THROTTLE,
CURRENT_ROLL = RAW_ROLL,
CURRENT_PITCH = RAW_PITCH,
CURRENT_YAW = RAW_YAW;
if(RAW_THROTTLE > || < CURRENT_THROTTLE)
{
radio.write()
}
}
*/
Receiver
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>
const byte address[5] = {'R','o','M','e','O'};
RF24 radio (9, 10);
long command;
bool newData = false;
//init all vars using previous code
/*motor 1 is CW, 2 is front right, 3 is back left, 4 is back right */
void setup(){
radio.begin();
radio.setDataRate(RF24_250KBPS);
radio.setRetries(2, 15);
radio.openReadingPipe(1, address);
radio.startListening();
}
void flightMode0()
{
while(armed == true && mode0 = true)
{
if(radio.available())
{
radio.read(command,&sizeof(command));
newData = true;
}
if(newData = true)
////////////////THROTTLE///////////////
if(command >= 0 && command <= 200)
{
int throttle;
throttle = command;
1.write(throttle);
2.write(throttle);
3.write(throttle);
4.write(throttle);
}
/////////////////ROLL//////////////////
if(command >= 1000 && command <= 1200)
{
int roll;
roll = command - 1000;
1.write(roll);
2.write(roll -= 20);
3.write(roll);
4.write(roll -= 20);
}
if(command >= -1200 && command <= -1000)
{
int negRoll;
negRoll = (command * -1) -1000;
1.write(negRoll -= 10);
2.write(negRoll);
3.write(negRoll -= 10);
4.write(negRoll);
}
/////////////////PITCH/////////////////
if(command >= 10000 && command <= 10200)
{
int pitch;
pitch = command -= 10000;
1.write(pitch -= 10);
2.write(pitch -= 10);
3.write(pitch);
4.write(pitch);
}
if(command >= -10200 && command <= -10000)
{
int negPitch;
negPitch = (command * -1) - 10000;
1.write(negPitch);
2.write(negPitch);
3.write(negPitch -= 10);
4.write(negPitch -= 10);
}
/////////////////YAW///////////////////
if(command >= 20000 && command <= 20200)
{
int yaw;
yaw = command -= 20000;
1.write(yaw);
2.write(yaw -= 20);
3.write(yaw -= 20);
4.write(yaw);
}
if(command >= -20200 && command <= -20000)
{
int negYaw;
negYaw = (command * -1) -20000;
1.write(negYaw -= 20);
2.write(negYaw);
3.write(negYaw);
4.write(negYaw -= 20);
}
newData = false;
}
}
}
void flightMode1(){
//old flight mode settings here...
while(armed == true && mode = 1){
//main code belongs here...
}
}
void changeMode(){
if(command = 100000){
mode0 = false;
}
if(command = 200000){
mode0 = true;
}
}
void preArm()
{
if(armed == false)
{
1.write(5);
2.write(5);
3.write(5);
4.write(5);
delay(4000);
armed == true;
}
}
void loop(){
bool armed = false;
bool mode0 = true;
preArm();
flightMode0();
flightMode1();
changeMode();
}