Problems for my quad

Hello !
I have build a quadricopter. It’s finish but I have few problems with programming…
In my quad, I’ve got an accelgyro MPU6050, somes ultrasonic detectors (not programming yet) and an IR receptor for command with a IRremote.
My problem is that I have programming my MPU6050 and it’s work great but when my quad is incline, the correct motor start, but don’t stop until another motor have to start…
I have to makes all my motor runs with my remote, and then, if my quad incline, the correct motor have to start for a few moment ( there is 3 speed depends on the incline) and then, turn at the initial speed…
I don’t know if I will make me understand… ^^ I just want fix priority beetween my remote, and the MPU 6050, easily…
I have try, but it’s hard for me…
Sorry for my english :confused:
My code :

#include "Wire.h"
#include <Servo.h>
#include <IRremote.h>
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
Servo myservo7;
Servo myservo8;
Servo myservo9;
Servo myservo10;


void setup() {
    Wire.begin();
    Serial.begin(38400);
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
    myservo7.attach(7);
    myservo9.attach(9);
    myservo8.attach(8);
    myservo10.attach(10);
    myservo7.write(30);
    myservo9.write(30);
    myservo8.write(30);
    myservo10.write(30);
    irrecv.enableIRIn();
}

void loop() {
  

    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);

  if (ax >= 1000)
    { int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit + 10);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(10);
      if (ax >= 3000)
      {  int vit = 40;
          myservo7.write(vit);
          myservo9.write(vit + 20);
          myservo8.write(vit);
          myservo10.write(vit);
      delay(10);
          if (ax >= 5000)
          { int vit = 40;
            myservo7.write(vit);
            myservo9.write(vit + 50);
            myservo8.write(vit);
            myservo10.write(vit);
      delay(10);  }
       else
       {
         return;
       }
      }
    }
    
    
  if (ax <= -1000)
    { int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit + 10);
      myservo10.write(vit);
      delay(10);
      if (ax <= -3000)
      {  int vit = 40;
          myservo7.write(vit);
          myservo9.write(vit);
          myservo8.write(vit + 20);
          myservo10.write(vit);
      delay(10);
          if (ax <= -5000)
          { int vit = 40;
            myservo7.write(vit);
            myservo9.write(vit);
            myservo8.write(vit + 50);
            myservo10.write(vit);
      delay(10);  }
       else
       {
         return;
       }
      }
    }
     
  if (ay <= -1000)
    { int vit = 40;
      myservo7.write(vit + 10);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(10);
      if (ay <= -3000)
      {  int vit = 40;
          myservo7.write(vit + 20);
          myservo9.write(vit);
          myservo8.write(vit);
          myservo10.write(vit);
      delay(10);
          if (ay <= -5000)
          { int vit = 40;
            myservo7.write(vit + 50);
            myservo9.write(vit);
            myservo8.write(vit);
            myservo10.write(vit);
      delay(10);  }
       else
       {
         return;
       }
      }
    }
     
    if (ay >= 1000)
    { int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit + 10);
      delay(10);
      if (ay >= 3000)
      {  int vit = 40;
          myservo7.write(vit);
          myservo9.write(vit);
          myservo8.write(vit);
          myservo10.write(vit + 20);
      delay(10);
          if (ay >= 5000)
          { int vit = 40;
            myservo7.write(vit);
            myservo9.write(vit);
            myservo8.write(vit);
            myservo10.write(vit + 50);
      delay(10);  }
       else
       {
         return;
       }
      }
    }

if (irrecv.decode(&results))
    {
    Serial.println(results.value, HEX);
    
        if (results.value == 0xFD9867)
    { 
      int vit = 30;
      vit += 10;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
    } 
        if (results.value == 0xFDA857) 
    { 
      int vit = 30;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(1000);
    } 
        if (results.value == 0xFDB24D) 
    { 
            monter();         
    } 
        if (results.value == 0xFD8A75) 
    { 
            descendre();
    } 
        if (results.value == 0xFDB04F) 
    { 
            avancer();         
    } 
        if (results.value == 0xFD8877) 
    { 
            reculer();
    }
    
        if (results.value == 0xFD10EF) 
    { 
            pivotd();         
    } 
        if (results.value == 0xFD52AD) 
    { 
            pivotg();
    } 
    irrecv.resume(); 
    }
}

void monter()
{
      int vit = 30;
      vit += 30;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(1000);
      vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void descendre()
{
      int vit = 40;
      vit -= 10;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(1000);
      vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void avancer()
{
      int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit - 30);
      myservo8.write(vit + 30);
      myservo10.write(vit);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void reculer()
{
      int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit + 30);
      myservo8.write(vit - 30);
      myservo10.write(vit);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void pivotg()
{
      int vit = 40;
      myservo7.write(vit - 10);
      myservo9.write(vit + 30);
      myservo8.write(vit + 30);
      myservo10.write(vit - 10);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void pivotd()
{
      int vit = 40;
      myservo7.write(vit + 30);
      myservo9.write(vit - 10);
      myservo8.write(vit - 10);
      myservo10.write(vit + 30);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

Someone can help me please ? I think it's just a problem of structure in my code... :/

I think one of the problems folks here are having is that we don't know what the various functions do, because they are not named in English. Could you translate some of the non-English terms for us? It would help us to figure out what the functions do.

Thank's for your replie :) fonction "monter" = make up the quad in the air fonction "descendre" = make down the quad in the air fonction "avancer" = make forward the quad fonction "reculer" = make back the quad fonction "pivotg" = make rotate the quad on the left direction fonction "pivotd" = make rotate the quad on the right direction

Could it be because your using delays that block your code from doing anything else until they are done? Maybe look into the Blink Without Delay sketch, and learn how it works.

I have use the millis fonction, delete the delays, and now, it’s work great ! :slight_smile:

#include "Wire.h"
#include <Servo.h>
#include <IRremote.h>
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
Servo myservo7;
Servo myservo8;
Servo myservo9;
Servo myservo10;
long previousMillis = 0;

long interval = 10;


void setup() {
    Wire.begin();
    Serial.begin(38400);
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
    myservo7.attach(7);
    myservo9.attach(9);
    myservo8.attach(8);
    myservo10.attach(10);
    myservo7.write(30);
    myservo9.write(30);
    myservo8.write(30);
    myservo10.write(30);
    irrecv.enableIRIn();
}

void loop() {
  
if (irrecv.decode(&results))
    {
    Serial.println(results.value, HEX);
    
        if (results.value == 0xFD9867)
    { 
      int vit = 30;
      vit += 10;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
    } 
        if (results.value == 0xFDA857) 
    { 
      int vit = 30;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(1000);
    } 
        if (results.value == 0xFDB24D) 
    { 
            monter();         
    } 
        if (results.value == 0xFD8A75) 
    { 
            descendre();
    } 
        if (results.value == 0xFDB04F) 
    { 
            avancer();         
    } 
        if (results.value == 0xFD8877) 
    { 
            reculer();
    }
    
        if (results.value == 0xFD10EF) 
    { 
            pivotd();         
    } 
        if (results.value == 0xFD52AD) 
    { 
            pivotg();
    } 
    irrecv.resume(); 
    }
  
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
  
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);

  if (ax >= 1000)
    { int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit + 10);
      myservo8.write(vit);
      myservo10.write(vit);
      if (ax >= 3000)
      {  int vit = 40;
          myservo7.write(vit);
          myservo9.write(vit + 20);
          myservo8.write(vit);
          myservo10.write(vit);
          if (ax >= 5000)
          { int vit = 40;
            myservo7.write(vit);
            myservo9.write(vit + 50);
            myservo8.write(vit);
            myservo10.write(vit);
          }
       else
       {
         return;
       }
      }
    } 
    
  if (ax <= -1000)
    { int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit + 10);
      myservo10.write(vit);
      if (ax <= -3000)
      {  int vit = 40;
          myservo7.write(vit);
          myservo9.write(vit);
          myservo8.write(vit + 20);
          myservo10.write(vit);
          if (ax <= -5000)
          { int vit = 40;
            myservo7.write(vit);
            myservo9.write(vit);
            myservo8.write(vit + 50);
            myservo10.write(vit);
          }
       else
       {
         return;
       }
      }
    }
     
  if (ay <= -1000)
    { int vit = 40;
      myservo7.write(vit + 10);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      if (ay <= -3000)
      {  int vit = 40;
          myservo7.write(vit + 20);
          myservo9.write(vit);
          myservo8.write(vit);
          myservo10.write(vit);
          if (ay <= -5000)
          { int vit = 40;
            myservo7.write(vit + 50);
            myservo9.write(vit);
            myservo8.write(vit);
            myservo10.write(vit);
          }
       else
       {
         return;
       }
      }
    }
     
    if (ay >= 1000)
    { int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit + 10);
      if (ay >= 3000)
      {  int vit = 40;
          myservo7.write(vit);
          myservo9.write(vit);
          myservo8.write(vit);
          myservo10.write(vit + 20);
          if (ay >= 5000)
          { int vit = 40;
            myservo7.write(vit);
            myservo9.write(vit);
            myservo8.write(vit);
            myservo10.write(vit + 50);
          }
       else
       {
         return;
       }
      }
    }
  }
}

void monter()
{
      int vit = 30;
      vit += 30;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(1000);
      vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void descendre()
{
      int vit = 40;
      vit -= 10;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(1000);
      vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void avancer()
{
      int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit - 30);
      myservo8.write(vit + 30);
      myservo10.write(vit);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void reculer()
{
      int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit + 30);
      myservo8.write(vit - 30);
      myservo10.write(vit);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void pivotg()
{
      int vit = 40;
      myservo7.write(vit - 10);
      myservo9.write(vit + 30);
      myservo8.write(vit + 30);
      myservo10.write(vit - 10);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void pivotd()
{
      int vit = 40;
      myservo7.write(vit + 30);
      myservo9.write(vit - 10);
      myservo8.write(vit - 10);
      myservo10.write(vit + 30);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

Thank you so much !

Now, I want that the gyro MPU6050 makes the motors turn only if I have push the 0xFD9867 button. Cause my problem is that when my motors is all off, if I incline the quad, the gyro make start the motors. But I want make turn the motors only if I have push the 0xFD9867 button before. And then, if I push the 0xFDA857 button, (the second one) I want that the gyro can't make the motor turn (stop button). I think it's a memory fonction ? Thanks for your help !

axoux72: Now, I want that the gyro MPU6050 makes the motors turn only if I have push the 0xFD9867 button. Cause my problem is that when my motors is all off, if I incline the quad, the gyro make start the motors. But I want make turn the motors only if I have push the 0xFD9867 button before. And then, if I push the 0xFDA857 button, (the second one) I want that the gyro can't make the motor turn (stop button). I think it's a memory fonction ?

Well, if I read your question correctly, it sounds like you need a flag to indicate whether or not certain buttons have been pressed.

// near the start of your sketch.. add a boolean
Servo myservo7;
Servo myservo8;
Servo myservo9;
Servo myservo10;
bool OKToRunMotors = true;   //name this whatever you like

// then add a line here...
    if (results.value == 0xFD9867)
    { 
      int vit = 30;
      vit += 10;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      OKToRunMotor = true;
    } 

// Then, in the routine that starts the motors (I don't know which one that is)
// just add an if statement

if (OKToRunMotors) {
   // place your normal stuff here
  }

Here's a little tip for you.... You can make things easier to code, and later, to troubleshoot or maintain, by using #define to refer to things like buttons, codes, etc.

// Near the beginning of yourr sketch, place these lines.

#define btnMonter     0xFDB24D
#define btnDesccendre 0xFD8A75
#define btnAvancer    0xFDB04F
#define btnReculer    0xFD8877
#define btnPivotD     0xFD10EF
#define btnPivotG     0xFD52AD

// Then, you can code

    if (results.value == btnMonter)  // you can name the button/code anything you want
    { 
      monter();         
    }

Oh ! It’s easy :slight_smile:
Thank’s a lot ! It’s work great !

#include "Wire.h"
#include <Servo.h>
#include <IRremote.h>
#include "I2Cdev.h"
#include "MPU6050.h"
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

int RECV_PIN = 2;
IRrecv irrecv(RECV_PIN);
decode_results results;
Servo myservo7;
Servo myservo8;
Servo myservo9;
Servo myservo10;
long previousMillis = 0;
boolean motoron = false;

long interval = 10;


void setup() {
    Wire.begin();
    Serial.begin(38400);
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
    myservo7.attach(7);
    myservo9.attach(9);
    myservo8.attach(8);
    myservo10.attach(10);
    myservo7.write(30);
    myservo9.write(30);
    myservo8.write(30);
    myservo10.write(30);
    irrecv.enableIRIn();
}

void loop() {
  
if (irrecv.decode(&results))
    {
    Serial.println(results.value, HEX);
    
        if (results.value == 0xFD9867)
    { 
            decoller();
    } 
        if (results.value == 0xFDA857) 
    { 
            arret();
    } 
        if (results.value == 0xFDB24D) 
    { 
            monter();         
    } 
        if (results.value == 0xFD8A75) 
    { 
            descendre();
    } 
        if (results.value == 0xFDB04F) 
    { 
            avancer();         
    } 
        if (results.value == 0xFD8877) 
    { 
            reculer();
    }
        if (results.value == 0xFD10EF) 
    { 
            pivotd();         
    } 
        if (results.value == 0xFD52AD) 
    { 
            pivotg();
    } 
    irrecv.resume(); 
    }
  
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    previousMillis = currentMillis;
    if (motoron)
    {
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    Serial.print("a/g:\t");
    Serial.print(ax); Serial.print("\t");
    Serial.print(ay); Serial.print("\t");
    Serial.print(az); Serial.print("\t");
    Serial.print(gx); Serial.print("\t");
    Serial.print(gy); Serial.print("\t");
    Serial.println(gz);

  if (ax >= 1500)
    { int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit + 10);
      myservo8.write(vit);
      myservo10.write(vit);
      if (ax >= 3000)
      {  int vit = 40;
          myservo7.write(vit);
          myservo9.write(vit + 20);
          myservo8.write(vit);
          myservo10.write(vit);
          if (ax >= 5000)
          { int vit = 40;
            myservo7.write(vit);
            myservo9.write(vit + 50);
            myservo8.write(vit);
            myservo10.write(vit);
          }
       else
       {
         return;
       }
      }
    } 
    
  if (ax <= -1500)
    { int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit + 10);
      myservo10.write(vit);
      if (ax <= -3000)
      {  int vit = 40;
          myservo7.write(vit);
          myservo9.write(vit);
          myservo8.write(vit + 20);
          myservo10.write(vit);
          if (ax <= -5000)
          { int vit = 40;
            myservo7.write(vit);
            myservo9.write(vit);
            myservo8.write(vit + 50);
            myservo10.write(vit);
          }
       else
       {
         return;
       }
      }
    }
     
  if (ay <= -1500)
    { int vit = 40;
      myservo7.write(vit + 10);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      if (ay <= -3000)
      {  int vit = 40;
          myservo7.write(vit + 20);
          myservo9.write(vit);
          myservo8.write(vit);
          myservo10.write(vit);
          if (ay <= -5000)
          { int vit = 40;
            myservo7.write(vit + 50);
            myservo9.write(vit);
            myservo8.write(vit);
            myservo10.write(vit);
          }
       else
       {
         return;
       }
      }
    }
     
   if (ay >= 1500)
    { int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit + 10);
      if (ay >= 3000)
      {  int vit = 40;
          myservo7.write(vit);
          myservo9.write(vit);
          myservo8.write(vit);
          myservo10.write(vit + 20);
          if (ay >= 5000)
          { int vit = 40;
            myservo7.write(vit);
            myservo9.write(vit);
            myservo8.write(vit);
            myservo10.write(vit + 50);
          }
       else
       {
         return;
       }
      }
    }
   }
  }
}

void decoller()
{
      int vit = 50;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      motoron = true;
}

void arret()
{
      int vit = 30;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      motoron = false;
}

void monter()
{
      int vit = 30;
      vit += 30;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(1000);
      vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void descendre()
{
      int vit = 40;
      vit -= 10;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
      delay(1000);
      vit = 40;
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void avancer()
{
      int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit - 30);
      myservo8.write(vit + 30);
      myservo10.write(vit);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void reculer()
{
      int vit = 40;
      myservo7.write(vit);
      myservo9.write(vit + 30);
      myservo8.write(vit - 30);
      myservo10.write(vit);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void pivotg()
{
      int vit = 40;
      myservo7.write(vit - 10);
      myservo9.write(vit + 30);
      myservo8.write(vit + 30);
      myservo10.write(vit - 10);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}

void pivotd()
{
      int vit = 40;
      myservo7.write(vit + 30);
      myservo9.write(vit - 10);
      myservo8.write(vit - 10);
      myservo10.write(vit + 30);
      delay(1000);
      myservo7.write(vit);
      myservo9.write(vit);
      myservo8.write(vit);
      myservo10.write(vit);
}