¿Como puedo activar un servo y un motor al mismo tiempo?

Hola, he visto en varios foros y paginas como arreglar este problema pero no he encontrado una solución.
Necesito hacer que un motor brushless se active y un segundo después se active un servo mientras el motor sigue encendido.

Este es el código que estoy usando:

#include <Servo.h>
#include "ESC.h"
#define Vel_Min (1000) 
#define Vel_Max (2000)

Servo Servo_Shoot;
float distancia = 0;
int pos = 0; 

Servo Servo_Rotate;

ESC myESC (10, Vel_Min, Vel_Max, 500);                 
int oESC;   

long Duracion; 
int dist; 

char option;
int cm = 0;

float Leerdistancia(int triggerPin, int echoPin){ 

  pinMode(triggerPin, OUTPUT);  
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(triggerPin, LOW);
  pinMode(echoPin, INPUT);
  return 0.01723 * pulseIn(echoPin, HIGH);
}

void setup() {
  Servo_Shoot.attach(9); 
  Servo_Shoot.write(0);
  Servo_Rotate.attach(11); 
  Servo_Rotate.write(90);
  myESC.arm();  
  Serial.begin(9600); 
  Serial.println("Encendiendo torreta"); 
  delay(2000);
  Serial.println("¿Quieres disparar? S / N");
  delay(5000);  
}

void loop() {
  if (Serial.available()>0){
    option=Serial.read();
    if(option=='S'){
    //servo2(pos);
    /*distancia = Leerdistancia(2, 3);
    Serial.print (distancia); 
    Serial.println("cm");*/ 
    servo1(pos);
    motor1(oESC);
    Serial.println("¿Disparar de nuevo? S / N");
    } 
    if(option=='N'){
      Serial.println("Apagando");
    }
  }
}


void servo1 (float pos){
  //if (distancia <= 138){
    Serial.println("Objetivo Encontrado"); 
    for (pos = 0; pos  <= 110; pos += 1) {
    Servo_Shoot.write(pos); 
    delay(20); 
  }
    for (pos = 110; pos >= 1; pos -= 109) { 
    Servo_Shoot.write(pos);  
    delay(3);     
  }
  }
  /*else if (distancia > 139){
    for (pos = 0; pos <= 1; pos +=1){
      Servo_Shoot.write(pos);
      delay(1);
    }
  }
}*/

void servo2 (float pos) {
  for (pos = 90; pos  <= 180; pos += 1) {
    Servo_Rotate.write(pos); 
    delay(15); 
  }
  for (pos = 180; pos >= 0; pos -= 1) { 
    Servo_Rotate.write(pos);  
    delay(15);      
  }
  for (pos = 0; pos <= 90; pos +=1) {
    Servo_Rotate.write(pos);
    delay(15);
  }
  }


void motor1 (float dis) {
  //if (distancia <= 138){
    Serial.println("Disparando");
  for (oESC = Vel_Min; oESC <= Vel_Max; oESC += 1) { 
    myESC.speed(oESC); 
    delay(10); 
  }
  delay(1000);
  for (oESC = Vel_Max; oESC >= Vel_Min; oESC -= 1) {
    myESC.speed(oESC); 
    delay(10); 
   } 
  }
  /*else if (distancia > 139){
    Serial.println("No hay objetivos");
  }
}*/

Ve a Documentación y lee como usar millis() y deja de usar delay() en tus proyectos, solo te provocan problemas como estos, donde justamente no puedes comandar dos cosas porque cuando el código encuentra delay se detiene.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.