Arduino drone

How do I control 4 motors with the below given program, currently it can only control 1.

#include <Servo.h>

#include <SoftwareSerial.h>


int incomingByte = 0;  // for incoming serial data
int armingTime;
int pin = 9;
int counter = 0;
int pulseTime = 1100; //microseconds
boolean highSpeed = true;
Servo myservo;

void setup(){
  
  Serial.begin(9600);
 
  myservo.attach(9);
  
 
  Serial.println("Enter 0-9 to adjust motor speed.");
  Serial.println("Enter h or l to change the speed range.");
}

void loop(){
  
 
  digitalWrite(pin, HIGH); 
  delayMicroseconds(pulseTime); 
  digitalWrite(pin, LOW); 
  delay(10-(pulseTime/1000)); 
  
  
  if(Serial.available() > 0){
    incomingByte = Serial.read();
    }
  



//void incomingByte(){
//void readSerialVal(){
  
  switch (incomingByte) {
      case '0':
        setTimingParams(1100);
        incomingByte='*';
        Serial.print("Throttle: ");
      Serial.print((pulseTime-1100)/8);
      Serial.println("%");     
        break;
    
      case '6':    
        setTimingParams(1600);
        incomingByte='*';
        Serial.print("Throttle: ");
      Serial.print((pulseTime-1100)/8);
      Serial.println("%");
        break;
  
      case '9':    
        setTimingParams(1900);
        incomingByte='*';
        Serial.print("Throttle: ");
      Serial.print((pulseTime-1100)/8);
      Serial.println("%");
        break;
 
    } 
}

void setTimingParams(int newPulseTimeVal){
 if(highSpeed){
    pulseTime = newPulseTimeVal;
  } else {
    pulseTime = 1025 + (newPulseTimeVal/10);
  }

That code is blocking and uses delay or delayMicroseconds for the timing. You can't extend it to more motors if you want to be able to control them independently like on a quadcopter. You'll have to spend some time learning about non-blocking code.

It looks like you are manually managing the servo signal. The servo library can do that for you.

Software serial and the servo library conflict over timers. Consider using ServoTimer2
But I suspect that your use of software serial is just for testing. Most drones are not controlled through the serial monitor.

I am using a Bluetooth module and will control the drone using the app on Android phone. I am new to this "arduino" so pls assist me with programming the esc's. This project has to be submitted by 8th of this month.

Huzaif35:
I am new to this “arduino” so pls assist me with programming the esc’s.

This is a really really really big project for someone new to Arduino. This is a really big project for a pro. This is like saying you’re going to learn to swim by crossing the English Channel.

This project has to be submitted by 8th of this month.

Oh, kid, I’m sorry but it ain’t gonna happen. Even a super pro Arduino coder with all the parts already sitting on his desk put together could make a drone fly in 5 days if he spent every waking minute on it. This is a HUGE project. It’s not something you can just throw together in a few days. Especially if you are a noobie. Why don’t you pick something a little simpler to start with.

i agree Delta_g but i have to do the impossible to win first place at the exhibition. I only need a bit of help for programming… :slight_smile: . here i have a code which controls the esc , but via a serial monitor i need to control it via Bluetooth please help if you know.

#include <Servo.h> 
 
//Create the 4 esc objects
Servo esc1;
Servo esc2;
Servo esc3;
Servo esc4;
 
//Esc pins
int escPin1 = 6;
int escPin2 = 9;
int escPin3 = 10;
int escPin4 = 11;

//Min and max pulse
int minPulseRate        = 1000;
int maxPulseRate        = 2000;
int throttleChangeDelay = 50;
 
//SETUP
void setup() {
  Serial.begin(9600);
  Serial.setTimeout(500);
  
  //Init escs
  initEscs();
}
 
//LOOP
void loop() {
  // Wait for some input
  if (Serial.available() > 0) {
    
    // Read the new throttle value
    int throttle = normalizeThrottle(Serial.parseInt());
    
    // Print it out
    Serial.print("Setting throttle to: ");
    Serial.println(throttle);
    
    // Change throttle to the new value
    changeThrottle(throttle);
  }
 
}
 
//Change throttle value
void changeThrottle(int throttle) {
  int currentThrottle = readThrottle();
  
  int step = 5;
  if(throttle < currentThrottle) {
    step = -5;
  }
  
  // Slowly move to the new throttle value 
  while(currentThrottle != throttle) {
    writeTo4Escs(currentThrottle + step);
    
    currentThrottle = readThrottle();
    delay(throttleChangeDelay);
  }
}

//Read the throttle value
int readThrottle() {
  int throttle = esc1.read();
  
  Serial.print("Current throttle is: ");
  Serial.print(throttle);
  Serial.println();
  
  return throttle;
}

//Change velocity of the 4 escs at the same time
void writeTo4Escs(int throttle) {
  esc1.write(throttle);
  esc2.write(throttle);
  esc3.write(throttle);
  esc4.write(throttle);
}

//Init escs
void initEscs() {
  esc1.attach(escPin1, minPulseRate, maxPulseRate);
  esc2.attach(escPin2, minPulseRate, maxPulseRate);
  esc3.attach(escPin3, minPulseRate, maxPulseRate);
  esc4.attach(escPin4, minPulseRate, maxPulseRate);
  
  //Init motors with 0 value
  writeTo4Escs(0);
}

//Start the motors
void startUpMotors() {
  writeTo4Escs(50);
}
 
// Ensure the throttle value is between 0 - 180
int normalizeThrottle(int value) {
  if(value < 0) {
    return 0;
    
  } else if(value > 180) {
    return 180;
  }
  
  return value;
}

Is that really all the control you want, throttle up/down on 4 motors at once?

If so then your next step is to write a simple program to get the same throttle information that you currently use from Serial. Only this time get it from your Android app into the Arduino with whatever Bluetooth module you are using and print it to the Serial monitor. Once that's working correctly you're more or less there.

Steve

No, I need to add more for the drone to move left, right, forward and backward. How do I do that?!

Huzaif35:
No, I need to add more for the drone to move left, right, forward and backward. How do I do that?

You need to send commands for what you want to do

Receive forward command - speed up rear motors and/or slow down front motors
Receive backward command - speed up front motors and/or slow down rear motors
Receive left command - speed up right motors and/or slow down left motors
Receive right command - speed up left motors and/or slow down right motors

Once the drone is at the required angle return all motors to the same power to continue in the same direction without flipping over.

WARNING !
I guarantee that the drone will crash unless you have on-board acceleration and angle sensors and programming to limit the angle of bank.

Ok I'll modify the program. Now I can control the stability of the drone by adding a delay right?!

Wrong. It takes A LOT more than a delay to turn a really basic throttle control into a full scale flight controller and that's what you need for a stable drone.

Plenty of people have done it before. Try Googling "Arduino drone" and see how complex it really is.

Steve

Is Arduino, at 16MHz, the right platform to implement a drone?

My gut feeling is that it might not be fast enough to deal in real time with keeping the drone stable in the air with no user inputs. E.G. if there is a gust of wind and things go pear shaped for example.

And I would have though that this would be the absolute minimum flight control you would need.

I am thinking that, if the OP is brand new to microcontrollers, it might be better to choose a simpler project for now and tackle a drone when they are a bit more advanced and using a purpose constructed flight controllers such as these:

Don't know a lot about programming these but more than likely you can customize the code if needed.

Even basic flight control is pretty full on mathematically, involving vectors and such like to calculate the correct response we re individual engine power?

It is inevitably going to involve a lot of floating point mathematics and this is where a 16MHz CPU is going to struggle to keep up?

slipstick:
Wrong. It takes A LOT more than a delay to turn a really basic throttle control into a full scale flight controller and that's what you need for a stable drone.

Plenty of people have done it before. Try Googling "Arduino drone" and see how complex it really is.

Steve

So if I want to move the drone to the left.
I have to decrease the speed of the left motors to less than the flying speed this will make the drone tilt to the left , to this I will add a small delay which will restore the flying speed of the motors after it tilts. I understand what you are saying but the delay will bring the drone back to normal before it flips. (I'll set the delay using trial and error)

Huzaif35:
So if I want to move the drone to the left.
I have to decrease the speed of the left motors to less than the flying speed this will make the drone tilt to the left , to this I will add a small delay which will restore the flying speed of the motors after it tilts. I understand what you are saying but the delay will bring the drone back to normal before it flips. (I'll set the delay using trial and error)

Adding a delay after tilting the quad and then restoring equal power to both sides of the quad will not put it back level. At best it will continue at the same angle in the same direction. It will, however, start falling as the lift produced by the propellers is no longer vertical so you have to increase the power to the motors to compensate.

Without a gyro on board all control must come from the pilot and feedback is visual. This is excellent when flying aerobatics but rubbish if you want a stable camera platform. The commercial flight controllers have gyros and accelerometers on board to provide stability when needed

UKHeliBob:
Adding a delay after tilting the quad and then restoring equal power to both sides of the quad will not put it back level. At best it will continue at the same angle in the same direction. It will, however, start falling as the lift produced by the propellers is no longer vertical so you have to increase the power to the motors to compensate.

Without a gyro on board all control must come from the pilot and feedback is visual. This is excellent when flying aerobatics but rubbish if you want a stable camera platform. The commercial flight controllers have gyros and accelerometers on board to provide stability when needed

I am not going to put a camera. I just want it to be controlled by my phone I just want it to do basic stuff like fly up down left and right that’s all. Once I build this I can always add a gyro and other techs to improve the drone.

I build drones for fun and I use programming from people smarter than me for the control system, I just tune the PID gains to my frame. They are absolutely uncontrollable without having at least a 3 axis gyro that feeds back into a PID controller to keep the thing stable. A human quite literally does not have the reaction times necessary to keep a quadcopter in the air. Quadcopters are inherently unstable and require constant corrections on a time scale of hundredths of a second to not flip and crash.

For what you are suggesting you absolutely need a gyro for the quad to be able to keep itself from rotating whichever way, and an accelerometer so that it has a reference for which way is up. Without those two things it will be absolutely uncontrollable from a phone. Hell, a phone doesn't really react fast enough to fly one directly anyway.

Also, there is a movement you are forgetting, turning. Clockwise is done by slowing the clockwise rotating motors and speeding up the counterclockwise rotating motors. Reverse to turn the opposite direction.

And finally, there are people who have managed to get working drone controllers out of an Arduino. They are kind of terrible. Most decent control boards are running 32 bit processors now.

UKHeliBob:
Adding a delay after tilting the quad and then restoring equal power to both sides of the quad will not put it back level. At best it will continue at the same angle in the same direction. It will, however, start falling as the lift produced by the propellers is no longer vertical so you have to increase the power to the motors to compensate.

Without a gyro on board all control must come from the pilot and feedback is visual. This is excellent when flying aerobatics but rubbish if you want a stable camera platform. The commercial flight controllers have gyros and accelerometers on board to provide stability when needed

I am not putting a camera on the drone for now. I just want it to do basic movement. Ill put a camera later once I get a gyro.

I just want it to do basic movement.

Basic movement should be relatively easy but that is not the same as having even basic control of the quad. Have you ever flown one or even a simulator ?

@Huzaif35, stop cross-posting. Other two threads removed.