I wrote a code for a drone using: Arduino UNO + HC-06 BT module + MPU6050 + Bluetooth app that i created.
I also added PID to balance the drone. Now when i hang the drone in mid air and balance the PID everything works fine. But when i want to test on the ground the quadcopter does not fly stable and flips on one side.
The code has couple if statement and their functions is described as below:
if (Received == '0') ---> To make the drone fly stable
if (Received == '1') ---> Left pushbutton on the android app (to roll left)
if (Received == '2') ---> Right pushbutton on the android app (to roll Right)
if (Received == '3') ---> Forward pushbutton on the android app (lift the pitch nose down)
if (Received == '4') ---> Backward pushbutton on the android app (lift the pitch nose up)
if (Received == '5') ---> (+) pushbutton to increase the throttle value to make drone fly higher.
if (Received == '6') ---> (-) pushbutton to decrease the throttle value to make drone fly lower.
if (Received == '7') ---> stop pushbutton to stop all the motors.
Important note: Also every-time I release any of the above pushbutton [1 to 7], my phone sends '0' to make the drone balance again.
I don't know why when I release the '7' pushbutton, all motors are turned off, (but motor 3) gets to work. Although I changed the throttle to 1000. So that all motors should be turned off when i release the stop pushbutton.
Time is NOT measured in other than whole milliseconds. It is stupid to use floats to hold integral values.
Why isn't the code to read the accelerometer in a function?
Why isn't the code to read the gyroscope in a function?
Why isn't the code indented worth a damn?
Having
all
the
code
start
in
column
one
does
NOT
improve
its
readability.
if(-3 <error <3) //y-axis
That is NOT how to test that a value is in a range.
Printing a bunch of values, and the computing and using new values, doesn't tell you squat about what the program is doing.
Using the constrain() function is better than all those if statements.
You have a LOT of duplicate code in each if(Received == 'x') body.
Having no useful data, it is going to be impossible to debug your program. Move the Serial.print() statements to AFTER you calculate the values to be printed. A clue-by-four just might whack you.
CoffeeBrain:
I don't know why when I release the '7' pushbutton, all motors are turned off, (but motor 3) gets to work. Although I changed the throttle to 1000. So that all motors should be turned off when i release the stop pushbutton.
When you release the '7' button and your program sends the '0' button your drone is going to try to level itself. Since it can't make any of the motors go slower than stopped it has to increase the speed of the motors on the low side until the drone is level. Try tilting the drone to see if the other motors run to level the drone.