This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

  • 0 voto(s) - 0 Media
  • 1
  • 2
  • 3
  • 4
  • 5
Ayuda con robot arduino
#1
Hola, soy nuevo en este foro y también en arduino. Estoy haciendo un proyecto en el cual necesito que el robot camine por cierto tiempo, luego se detenga y vuelva a caminar. Pero al momento de estar en movimiento no debe chocar. Tiene un sensor ultrasónico y hasta ahora puedo hacer que evite objetos y no choque o que se mueva y se detenga cada cierto tiempo, pero no he conseguido hacer que detecte objetos y se mueva al mismo tiempo.

Este es el código, si alguien puede ayudarme...

Código:
int izqA = 5; //  controla motor izquierdo hacia adelante
int izqB = 6; //  controla motor izquierdo hacia atras
int derA = 9; //  controla motor derecho hacia adelante
int derB = 10; // controla motor derecho hacia atras
int vel = 220; // Velocidad de los motores (0-255)


unsigned long anteriorMillis = 0;
long encendido = 30000;
long apagado = 50000;
int ledPin = 13;
int ledEstado = HIGH;

int pecho = 3;            // echo del ultrasonico
int ptrig = 4;            // trig del ultrasonico
int duracion, distancia;  // para calcular distacia

// --------gira hacia atras al detectar un obstaculo---------
void atras()          
  {
    
      if (distancia <= 15 && distancia >=3 )
     {
         analogWrite(derB, vel);          
         analogWrite(izqB, 0);
         analogWrite(derA, 0);  
         analogWrite(izqA, 0);
  
      }
    
  }

// ------genera el pulso del sensor ultrasonico-------
void calculo()          
{
    digitalWrite(ptrig, HIGH);   // genera el pulso de trigger por 10ms
     delay(0.01);
     digitalWrite(ptrig, LOW);
    
     duracion = pulseIn(pecho, HIGH);              // Lee el tiempo del echo
     distancia = (duracion/2) / 29;            // calcula la distancia en centimetros
     delay(10);
  
}

void setup()  {

  pinMode(derA, OUTPUT);
  pinMode(derB, OUTPUT);
  pinMode(izqA, OUTPUT);
  pinMode(izqB, OUTPUT);

  pinMode(pecho, INPUT);  
  pinMode(ptrig,OUTPUT);  
  
  pinMode(ledPin,OUTPUT);

  attachInterrupt(1, atras, RISING);   // interrupcion al detectar un objeto.
  
}


  
void loop()  {
  
  unsigned long actualMillis = millis();
  
  if((ledEstado == LOW) && (actualMillis - anteriorMillis >= apagado))
  {
    //  enciende motores hacia adelante y el LED por 30 segundos
    ledEstado = HIGH;
    anteriorMillis = actualMillis;
    digitalWrite(ledPin, ledEstado);
            
      analogWrite(derB, 0);          
      analogWrite(izqB, 0);
      analogWrite(derA, vel);  
      analogWrite(izqA, vel);

      

      /*       En esta parte deseo que al estar en movimiento
     *       durante ese tiempo me detecte los objetos y no choque.
     */

     //calculo(); .............??  puedo generar el pulso desde aqui? y a traves del attachInterrupt no chocar?
  
  }
  else if ((ledEstado == HIGH) && (actualMillis - anteriorMillis >= encendido))
  {
    // detiene motores y apaga el LED por 50 segundos
    ledEstado = LOW;
    anteriorMillis = actualMillis;
    digitalWrite(ledPin, ledEstado);
    
      analogWrite(derB, 0);          
      analogWrite(izqB, 0);
      analogWrite(derA, 0);  
      analogWrite(izqA, 0);
      
  }

      
}

Gracias!!!
  Responder


Posibles temas similares…
Tema Autor Respuestas Vistas Último mensaje
  APORTE Montaje Robot 2WD Arduino (DIY) kuadri 1 1,001 17-06-2020, 09:51 PM
Último mensaje: asesorplaza1
Triste DUDA Error compilando para la tarjeta Arduino/Genuino Uno. Necesito ayuda siro7 9 4,836 28-04-2019, 02:22 PM
Último mensaje: siro7
  CONSULTA ROBOT DOS MOTORES CON SERVO PARA DIRECCION Miguelmetro 1 1,497 23-04-2018, 06:30 PM
Último mensaje: Iván
  Ayuda con drivers arduino msa 32 17,808 28-03-2018, 10:37 AM
Último mensaje: superkuko
  ayuda en coneccion de java con arduino claudiohnavarro 0 1,190 26-04-2017, 07:02 PM
Último mensaje: claudiohnavarro