运行 伺服和超声波传感器如何与 arduino 上的毫秒同步?

How run servo and ultrasonic sensor concurently with millis on arduino?

我的伺服和超声波有问题不能同时运行。我的代码目前工作如下:伺服将每 10 秒移动一次,超声波始终检测范围并在 LED 上显示结果。 我在 google 上寻找参考资料,我发现延迟导致伺服和超声波不能 运行 在一起。需要多任务才能使用 millis()。但是我还是不能使用函数millis()。 如何使用 millis() 更改我的代码中的延迟?

谢谢你的帮助!

#include <Servo.h>
#define trigPin 13
#define echoPin 12
#define ledM 10
#define ledK 9
#define ledH 8

Servo ikiservo;  //servo 
int pos = 0;    //set positon servo
int pinSpeaker = 4;  //buzzer

void setup() {
  Serial.begin (9600); //SERIAL
  ikiservo.attach(3); //servo
  pinMode(pinSpeaker, OUTPUT);  
  pinMode(trigPin, OUTPUT); //ultrasonic
  pinMode(echoPin, INPUT); //ultrasonic
  pinMode(ledM, OUTPUT);
  pinMode(ledK, OUTPUT);
  pinMode(ledH, OUTPUT);
}

void loop() {
  //-------------SERVO----------

  for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
  {                                  // in steps of  1 degree 
    ikiservo.write(pos);            
    delay(10000);       // waits 10s for the servo to reach the position 
  }

  //---------ULTRASONIC-----------
  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  Serial.print(distance);
  Serial.println(" cm");
  delay (400);

  if (distance > 4 && distance <7) {  // turn on ledM when distance 4<distance<7
    digitalWrite(ledM,HIGH); 
    digitalWrite(ledK,LOW);
    digitalWrite(ledH,LOW);
    playTone(300, 160);          //buzzer ON
    delay(1000);
    //buzzer
  } else if(distance > 2 && distance <4) {// turn on ledK when 7<distance<4
    digitalWrite(ledM,LOW);
    digitalWrite(ledK,HIGH);
    digitalWrite(ledH,LOW);
    playTone(0, 0);
    delay(1000);
  } else if(distance <2) {//turn on ledH when distance<2
    digitalWrite(ledM,LOW);
    digitalWrite(ledK,LOW); 
    digitalWrite(ledH,HIGH);
    playTone(0, 0);
    delay(1000);
  } else{                      //turn off all
    digitalWrite(ledM,LOW);
    digitalWrite(ledK,LOW);  
    digitalWrite(ledH,LOW);
    playTone(0, 0);
    delay(1000);
  }
}

void playTone(long duration, int freq) { //funtion for add tone on buzzer
    duration *= 1000;
    int period = (1.0 / freq) * 1000000;
    long elapsed_time = 0;
    while (elapsed_time < duration) {
        digitalWrite(pinSpeaker,HIGH);
        delayMicroseconds(period / 2);
        digitalWrite(pinSpeaker, LOW);
        delayMicroseconds(period / 2);
        elapsed_time += (period);
    }
}

你不是等了10秒,而是等了15分钟。可能这是您代码中唯一的问题。

就写这个

for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
{                                 // in steps of  1 degree 
    ikiservo.write(pos);            
    delay(100); // waits 0.1s for the servo to reach the position
                // so total time will be 9 seconds
}

反之,如果你想在10秒内跨越90°区域,则返回,代码为

void loop() {
    //-------------SERVO----------

    for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees 
    {                                  // in steps of  1 degree 
        ikiservo.write(pos);

        //---------ULTRASONIC-----------
        long duration, distance;
        digitalWrite(trigPin, LOW); 
        delayMicroseconds(2);
        digitalWrite(trigPin, HIGH);
        delayMicroseconds(10);
        digitalWrite(trigPin, LOW);
        duration = pulseIn(echoPin, HIGH);
        distance = (duration/2) / 29.1;
        Serial.print(distance);
        Serial.println(" cm");

        if (distance > 4 && distance <7) {  // turn on ledM when distance 4<distance<7
            digitalWrite(ledM,HIGH); 
            digitalWrite(ledK,LOW);
            digitalWrite(ledH,LOW);
        } else if(distance > 2 && distance <4) {// turn on ledK when 7<distance<4
            digitalWrite(ledM,LOW);
            digitalWrite(ledK,HIGH);
            digitalWrite(ledH,LOW);
        } else if(distance <2) {//turn on ledH when distance<2
            digitalWrite(ledM,LOW);
            digitalWrite(ledK,LOW); 
            digitalWrite(ledH,HIGH);
        } else{                      //turn off all
            digitalWrite(ledM,LOW);
            digitalWrite(ledK,LOW);  
            digitalWrite(ledH,LOW);
        }

        delay(100); // waits 0.1s for the servo to reach the position 
    }
}

我不得不删除音调,因为您使用它的方式不允许其他 "processes" 同时工作。看看是不是你想要的方法;如果是反馈,我们可以看看如何用蜂鸣器实现它