如何在arduino上使用millis运行伺服和超声波传感器?

时间:2015-12-01 02:36:48

标签: arduino multitasking timedelay

我的伺服和超声波有问题,无法顺利运行。我的代码现在工作如下:伺服将每10秒移动一次,超声波总是检测范围并在LED上显示结果。 我正在寻找谷歌的参考资料,我发现延迟导致伺服和超声波无法一起运行。为了使用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);
    }
}

1 个答案:

答案 0 :(得分:0)

你不是等待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 
    }
}

我不得不删除音调,因为你使用它的方式不允许其他“进程”同时工作。看看它是否是您想要的方法;如果是报告,我们可以看到如何使用蜂鸣器实现它