Hello everyone I am in the process of making a quad copter out of just an arduino and what I mean by that is I have an MPU I plan to use the brains of it all will be an UNO R3, I am building my own controller. I do not want to use someone else's software like multwii. I wanted to make a bare bones flight controller from the arduino IDE After tons of research I have only found one other person who has done this. I have tried to study his code but there is not much documentation to it and threre is not much comenting well not enough for me to understand. I am fairly new to arduino I would say I've been messing around with it for 3 months and what better way to learn arduino IDE than to take on such a big project...Yeah, I understand I am insane.
Never the less I would really appreciate if you all could look at the code that I have written and give me some feed back. I have been able to tie my drone down and get it to lift off the ground about 7in~18cm off the ground. I have been able to make it roll to the right and pitch forward both were in different variations of code and then I tried to make it so i can pitch and roll to either side and now... Nothing works. Now all I can do is get it to lift off the ground. I am trying to fix this at the moment. This is as close to the code I could get to when I could make the drone Pitch forward. I think I have the code pretty well commented. and I have pictures attached. If you have any questions let me know. As of right now the joysticks are hardwired to the UNO R3. I also have not yet implemented the MPU.
Here is my code
/* The Quad is set up like this *Note: I call them esc's but they represent the motors in my code*
*
* (Front)
* esc1 esc2
* \ /
* \ /
* [ ] (Rightside)
* / \
* / \
* esc3 esc4
* (Back)
*/
#include<Servo.h> //Include The servo library for the functions used
Servo esc1; //Declare the ESC's as a Servo Object
Servo esc2;
Servo esc3;
Servo esc4;
int lift;
int roll;
int MAX;
int pitch;
void setup()
{
esc1.attach(11); esc2.attach(10); esc3.attach(6); esc4.attach(5); //Attach the ESC's to PWM Pins
Serial.begin(9600); //Begin Reading/Open Serial Monitor
}
void loop()
{
lift=analogRead(A0); //Value of analog input on pin A0, which controls the LIFT or "ELEVATION" which is the Y direnction on joystick(1)
pitch=analogRead(A1); //Value of analog input on pin A1, which controls the ROLL which is the X direnction on joystick(2)
roll=analogRead(A2); //Value of analog input on pin A2, which controls the PITCH which is the Y direnction on joystick(2)
MAX=160; //This value is 160 insead of 180 because one of my ESC's freaks out past this value
lift=map(analogRead(A0), 0, 1023, 0, 160); //Map the input values from the joystick on analog pin 0 to correspond to max and min values
//for the servo output: 180 and 0
esc1.write(lift); esc2.write(lift); esc3.write(lift); esc4.write(lift); //this will cause the ESC's spin acording to the position of
//Joystick(1) in the Y direction
Serial.print("LIFT: ");
Serial.println(lift); //Print the value of the lift this should be 80 (nutral possition of joystick(1)) to 160
delay(1);
if (pitch > 515){ //If the anolog value coming from Joystick(2) in the Y direction is grater than 515
//execute following statment *Note:The values on the if statements are above or bellow
//511.5 because my joysticks tend to fluctuate at the nutral or standard position so
//placing the values that cause the if statments to "turn on" above or bellow the
//fluctuating point will ensure that the if statment does not "turn on" unless I have
//actually moved the joystick
lift=map(analogRead(A0), 0, 1023, 0, 160); //Map for the LIFT values as stated above (not sure this is nessecary to state again)
pitch=map(analogRead(A1), 0, 1023, lift, (lift-50)); //The map for the Pitch values from the standard position (not touching the joystick)
//being the same as the lift value to "lift minus 50" 50 is just a number I chose so
//I could be able to see the effect on the motor without putting the propellers on
esc1.write(pitch); esc2.write(pitch); //This line and the one bellow it are saying; cause the front 2 ESC's or MOTORS to
//slow down bases on the PITCH value and keep the back/rear motors spining at
//whatever the lift values are
esc3.write(lift); esc4.write(lift);
}
//The rest of the if statemnts just a repeat of the code above taylored for the pitch in the rear direction,Roll to the left side, and Roll to the right side
if (pitch < 509){
pitch=analogRead(A1);
lift=map(analogRead(A0), 0, 1023, 0, 160);
pitch=map(analogRead(A1), 0, 1023, lift, (lift-50));
esc1.write(lift); esc2.write(lift);
esc3.write(pitch); esc4.write(pitch);
}
if (roll > 515) {
roll=analogRead(A2);
lift=map(analogRead(A0), 0, 1023, 0, 160);
roll=map(analogRead(A2), 0, 1023, lift, (lift-50));
esc1.write(lift); esc2.write(roll);
esc3.write(lift); esc4.write(roll);
}
if (roll < 509) {
roll=analogRead(A2);
lift=map(analogRead(A0), 0, 1023, 0, 160);
roll=map(analogRead(A2), 0, 1023, lift, (lift-50));
esc1.write(roll); esc2.write(lift);
esc3.write(roll); esc4.write(lift);
}
}