如何使流动站向左行驶并停止

时间:2019-05-29 08:51:39

标签: arduino esp32

目前,我正在使用L298电动机驱动器,超声波传感器和ESP32。目标是当超声波传感器(位于流动站的前端)检测到物体时,流动站向左行驶并停止。 问题是,当传感器检测到物体时,流动站只会停止而不向左移动。否则,如果删除代码的“停止”部分,则漫游车将移至左侧。但是,我不知道我是怎么做的,他同时执行这两个命令。

// defines Sensor pins

#define trigPin 12
#define echoPin 13

//Define Motor pins
#define motorIn3  16 //Input 3
#define motorIn1  17 //Input 1
#define motorIn4  18 //Input 4
#define motorIn2  19 //Input 2

// defines variables
long duration;
int distance;

// define ActivateDistance
double activateDistance = 20;

void setup() 
{
  // Sets the trigPin as an Output
  pinMode(trigPin, OUTPUT); 
  // Sets the echoPin as an Input
  pinMode(echoPin, INPUT); 

  // sets the Motorpins  as outputs:
  pinMode(motorIn1, OUTPUT);
  pinMode(motorIn2, OUTPUT);
  pinMode(motorIn3, OUTPUT);
  pinMode(motorIn4, OUTPUT);

  Serial.begin(9600); // Starts the serial communication

}

void loop() 
{

  //Moving Motor forward
  digitalWrite(motorIn2, HIGH);
  digitalWrite(motorIn4, HIGH);
  digitalWrite(motorIn3, LOW);
  digitalWrite(motorIn1, LOW); 

  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);

 // Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

 // Reads the echoPin, returns the sound wave travel time in microseconds
 duration = pulseIn(echoPin, HIGH);

 // Calculating the distance
 distance = duration * 0.034 / 2;


//when the distance is under 20, the motor should drives to the left and stopps.
 if (distance <= activateDistance)
 {
  //for loop using that the motor drives a specific amount to the left
  for(int i = 0; i<5;i++)
  {
 //Motor drives to left
  digitalWrite(motorIn1, LOW);
  digitalWrite(motorIn2, HIGH);
  digitalWrite(motorIn3, HIGH);
  digitalWrite(motorIn4, LOW);
//After drives to left, the motor should stop.
    if(i == 4)
    {  
      //Moves forward 
      digitalWrite(motorIn2, HIGH);
      digitalWrite(motorIn4, HIGH);
      digitalWrite(motorIn3, LOW);
      digitalWrite(motorIn1, LOW); 
      delay(1000);
      //Motor Stops.
      digitalWrite(motorIn1, LOW);
      digitalWrite(motorIn2, LOW);
      digitalWrite(motorIn3, LOW);
      digitalWrite(motorIn4, LOW); 
    }
  }

 }
  delay(1000);
}

对不起,我的英语不好。

2 个答案:

答案 0 :(得分:1)

在您将流动站驱动器指示在左侧后,完全没有延迟,因此for(int i = 0; i<5;i++)中的前四次将非常迅速地执行。也许在其中添加延迟,以便漫游者有时间完成转弯。

我也不明白为什么将if(i == 4)和以下指令放在for loop的末尾:您可以将其放在for循环之后,并且具有完全相同的行为。

希望有帮助。

答案 1 :(得分:1)

首先,尝试使用每个函数都有其用途的函数重写代码。这将使您对正在发生的事情和问题所在有更多的了解。例如,您可以创建一个计算距离的函数并在主循环中对其进行检查,或者创建一个以持续时间作为参数的车辆前进函数。

第二,设置电动机销时,您需要稍加延迟,以使车辆有足够的时间行驶。

我用一些功能重写了您的代码并打印了一些图片,只是记得要校准时间以获得更好的性能。

// defines Sensor pins

#define trigPin 12
#define echoPin 13

//Define Motor pins
#define motorIn3 16 //Input 3
#define motorIn1 17 //Input 1
#define motorIn4 18 //Input 4
#define motorIn2 19 //Input 2

// defines variables
long duration;
int distance;

// define ActivateDistance
const int activateDistance = 20;

void setup()
{
    // Sets the trigPin as an Output
    pinMode(trigPin, OUTPUT);
    // Sets the echoPin as an Input
    pinMode(echoPin, INPUT);
    // sets the Motorpins  as outputs:
    pinMode(motorIn1, OUTPUT);
    pinMode(motorIn2, OUTPUT);
    pinMode(motorIn3, OUTPUT);
    pinMode(motorIn4, OUTPUT);
    Serial.begin(9600); // Starts the serial communication
}

void stop()
{
    // stop motor without duration
    Serial.println("STOP");
    digitalWrite(motorIn1, LOW);
    digitalWrite(motorIn2, LOW);
    digitalWrite(motorIn3, LOW);
    digitalWrite(motorIn4, LOW);
}

void left(int duration)
{
    //Motor goes to left
    Serial.println("LEFT");
    digitalWrite(motorIn1, LOW);
    digitalWrite(motorIn2, HIGH);
    digitalWrite(motorIn3, HIGH);
    digitalWrite(motorIn4, LOW);
    delay(duration);
    stop();
}

void forward(int duration)
{
    //Motor goes forward
    Serial.println("FORWARD");
    digitalWrite(motorIn2, HIGH);
    digitalWrite(motorIn4, HIGH);
    digitalWrite(motorIn3, LOW);
    digitalWrite(motorIn1, LOW);
    delay(duration);
    stop();
}

long get_distance(void)
{
    //get distance from sensor
    // Clears the trigPin
    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    // Sets the trigPin on HIGH state for 10 micro seconds
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);
    // Reads the echoPin, returns the sound wave travel time in microseconds
    duration = pulseIn(echoPin, HIGH);
    // Calculating the distance
    distance = duration * 0.034 / 2;
    return distance;
}

void loop()
{
    // check sensor
    if (get_distance() <= activateDistance)
    {
        Serial.println("Found an Obstacle!!!");
        // go left for 1 second
        left(1000);
        // go forward for 1 second
        forward(1000);
    }
    else
        // go forward for 1 second
        forward(1000);
}