Arduino - Problema para hacer funcionar un motor CC y una servo con un sensor sónico

 
Vista:

Problema para hacer funcionar un motor CC y una servo con un sensor sónico

Publicado por sebastián (1 intervención) el 14/07/2022 00:36:47
#define Servo1 13
#define trigPin 12
#define echoPin 11
#define MAX_DISTANCE 50 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

#include <Servo.h>

Servo servo1;

int pos = 0; // variable to store the servo position
const int Motor_pin = 4;



void setup() {
Serial.begin (9600);
pinMode(echoPin, INPUT);
pinMode(trigPin, OUTPUT);
pinMode(Servo1, OUTPUT);
servo1.attach(13);

}

void loop() {
long duracion, distancia; // variables

/* Hacer el disparo */
digitalWrite(12, LOW);
delayMicroseconds(100);
digitalWrite(12, HIGH); // Flanco ascendente
delayMicroseconds(100); // Duracion del pulso
digitalWrite(12, LOW); // Flanco descendente

/* Recepcion del eco de respuesta */
duracion = pulseIn(11, HIGH);

/* Calculo de la distancia efectiva */
distancia = (duracion / 10) / 29.1;

if (distancia < 100) {
for (pos = 0; pos < 45; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
for (pos = 45; pos >= 1; pos -= 1) // goes from 180 degrees to 0 degrees
{
servo1.write(pos); // tell servo to go to position in variable 'pos'
delay(100); // waits 15ms for the servo to reach the position
}

}
else {
}
//task 2
{

if (echoPin = HIGH)
{
digitalWrite(Motor_pin, LOW);

}
else
{
digitalWrite(Motor_pin, HIGH);
}
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder