Problem with joystick to servo over radio control

For the past couple of weeks I have been working on an RC plane. I have everything working accept for two things:

1.)I cannot get the elevator servo to work right.

#1 Description: I have tried to attach the elevator servo to the up and down motion of the joystick but every time I try to do that it moves with the ailerons(meaning three servos move at once). I have attached an image of this for a visual.

2.)My second problem is that I want the aileron servos to move in opposing directions to each other but I can't seem to figure out how to do that.

I have also attached the code of the transmitter and receiver so that you can see what I am running on my boards.

-Thanks-

Receiver:

#include<SoftwareSerial.h>
#include <Servo.h>

Servo esc;

Servo el;
Servo rudd;
Servo aile1;
Servo aile2;

String input;
int throttle, thr;
int aileron1, ail1;
int aileron2, ail2;
int elevator, ele;
int rudder, rud;

int boundLow;
int boundHigh;
const char COMMA = ',';

SoftwareSerial hc12(7, 8);

void setup() {
el.attach(11);
rudd.attach(6);
aile1.attach(5);
aile2.attach(3);

esc.attach(10);
Serial.begin(9600);
hc12.begin(9600);
esc.write(170);
delay(2000);
esc.write(90);
delay(2000);
esc.write(140);
delay(2000);
esc.write(90);
delay(2000);

Serial.write("Ports are attached and serial has begun\n");
Serial.write("ESC Callibration Complete\n");
Serial.write("Setup Complete");
}

void loop() {

if(Serial.available())
{
  input = Serial.readStringUntil('\n');
  if (input.length() > 0)
      {
      // Serial.println(input);

       boundLow = input.indexOf(COMMA);
       throttle = input.substring(0, boundLow).toInt();

       boundHigh = input.indexOf(COMMA, boundLow+1);
       aileron1 = input.substring(boundLow+1, boundHigh).toInt();

       boundHigh = input.indexOf(COMMA, boundLow+1);
       aileron2 = input.substring(boundLow+1, boundHigh).toInt();

       boundLow = input.indexOf(COMMA, boundHigh+1);
       elevator = input.substring(boundHigh+1, boundLow).toInt();

       rudder = input.substring(boundLow+1).toInt();

      esc.write(throttle);
      el.write(elevator);
      rudd.write(rudder);
      aile1.write(aileron1);
      aile2.write(aileron2);

      delay(10);}   
        }
}

Transmitter:

#include <SoftwareSerial.h>
#include <Servo.h>


SoftwareSerial hc12(2, 3); 
Servo esc;

int thr, ail1, ail2, ele, rud;

void setup() {

  esc.attach(10);
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A5, INPUT);
  pinMode(A7, INPUT);
  pinMode(A2, INPUT);
  pinMode(13, OUTPUT);

  Serial.begin(9600);
  hc12.begin(9600);
}

void loop()  {

  ele = map(analogRead(A0), 0, 1023, 0, 180);
  rud = map(analogRead(A1), 0, 1023, 0, 180);
  thr = map(analogRead(A7), 0, 1023, 0, 180);
  ail1 = map(analogRead(A5), 0, 1023, 0, 180);
  ail2 = map(analogRead(A5), 0, 1023, 0, 180);

  hc12.print(thr);
  hc12.print(",");
  hc12.print(ail1);
  hc12.print(",");
  hc12.print(ail2);
  hc12.print(",");
  hc12.print(ele);
  hc12.print(",");
  hc12.print(rud);
  hc12.println("");

  Serial.print(thr);
  Serial.print(",");
  Serial.print(ail1);
  Serial.print(",");
  Serial.print(ail2);
  Serial.print(",");
  Serial.print(ele);
  Serial.print(",");
  Serial.print(rud);
  Serial.println("");


  delay(100);
}

Reciever-Code.ino (1.56 KB)

Transmitter-Code.ino (1.05 KB)

4765909E-4496-4625-B332-1E7FFDAC688F.pdf (1.17 MB)

Thanks for the detailed problem description. Please post your code inline in your post using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing or review.

Open your message and select "modify" from the pull down menu labelled, "More", at the lower right corner of the message. Insert the code. Highlight your code by selecting it (it turns blue), and then click on the "</>" icon at the upper left hand corner. Click on the "Save" button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Then please read these two posts:

General Guidance and How to use the Forum
and
Read this before posting a programming question ...

I'm confused. Can we have a detailed description of the hardware you're using please. In the receiver code you seem to have an HC12 connected on a SoftwareSerial...but you're receiving data on the hardware Serial. What is actually connected where? And what do Servo.h and esc do in transmitter code?

You don't need to send two identical values for the ailerons. Just send one value then write(value) to one servo and write(180-value) to the other. Or just place them in the wings so that the same signal to each moves the servos arms in different directions.

Steve

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. That could be disastrous with a model airplane.

Just use cstrings - char arrays terminated with '\0' (NULL).

When using Cstrings you must use strcmp() to compare values rather than ==

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

I’m very new to programming and so yes I have HC-12 is attached to HardwareSerial because when I tried using SoftwareSerial it made the servos move back and forth very quickly and I would have no control. When I was experimenting with the board I found that when I attached HC-12 to the HardwareSerial the connection was much cleaner with no moving back and forth. And for the transmitter I will be removing that because it doesn’t really do anything.

bignation112:
I’m very new to programming and so yes I have HC-12 is attached to HardwareSerial because when I tried using SoftwareSerial it made the servos move back and forth very quickly

Put away the servos for a while and just write a simple receiver program that takes in data using SoftwareSerial and displays it on the Serial Monitor.

When that is working reliably you can gradually add in the code for the servos.

...R

Thank you for the help.