Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino Uno Millis功能可同时点亮运行伺服的LED_Arduino_Servo - Fatal编程技术网

Arduino Uno Millis功能可同时点亮运行伺服的LED

Arduino Uno Millis功能可同时点亮运行伺服的LED,arduino,servo,Arduino,Servo,我有一个Arduino Uno,一个伺服电机和两个LED(绿色和红色)。伺服电机每4秒旋转20度并返回 我希望红色led(LedR)在代码的前4秒亮起,然后在接下来的12秒低 我希望绿色led(ledG)从代码的第8秒一直亮到第12秒,然后在接下来的12秒内一直处于低电平 但是,我无法将其集成到伺服运行的/if语句中 我可以为Led写延迟功能,也可以写伺服代码,但是,延迟功能会停止所有代码,导致伺服不移动或Led不亮 我已经了解了millis()是解决方案,但我将如何使用它 #include &

我有一个Arduino Uno,一个伺服电机和两个LED(绿色和红色)。伺服电机每4秒旋转20度并返回

我希望红色led(LedR)在代码的前4秒亮起,然后在接下来的12秒低

我希望绿色led(ledG)从代码的第8秒一直亮到第12秒,然后在接下来的12秒内一直处于低电平

但是,我无法将其集成到伺服运行的
/
if
语句中

我可以为Led写延迟功能,也可以写伺服代码,但是,延迟功能会停止所有代码,导致伺服不移动或Led不亮

我已经了解了
millis()
是解决方案,但我将如何使用它

#include <Servo.h>

Servo myservo;  // create servo object to control a servo
// twelve servo objects can be created on most boards

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

void setup() {
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(LED_BUILTIN, OUTPUT);//LedR
}


void loop() {
  
  delay(4000);
    digitalWrite(LED_BUILTIN, HIGH);   // LedR high
  
  for (pos = 0; pos <= 20; pos += 1) { // goes from 0 degrees to 180 degrees
    // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }delay(4000);
  digitalWrite(LED_BUILTIN, LOW); //LedR low
  for (pos = 20; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
    
  }
}
#包括
伺服myservo;//创建伺服对象以控制伺服
//大多数电路板上可以创建12个伺服对象
int pos=0;//变量来存储伺服位置
无效设置(){
myservo.attach(9);//将针脚9上的伺服连接到伺服对象
pinMode(LED_内置,输出);//LedR
}
void循环(){
延迟(4000);
数码写入(LED内置,高);//LED高
对于(pos=0;pos=0;pos-=1){//从180度变为0度
myservo.write(pos);//告诉伺服转到变量“pos”中的位置
延迟(15);//等待15毫秒,等待伺服到达该位置
}
}

您的思路肯定是正确的:
delay()
确实会阻止其余的代码执行,您可以使用
millis()
来绕过该限制

返回自arduino代码开始运行以来的毫秒数。您可以使用一个额外的变量来构建一个类似临时秒表的机制:

  • setup()
    中,将当前
    millis()
    存储在变量中。对于该变量,时间暂时冻结:)
  • loop()
    中,如果连续调用
    millis()
    ,将得到一个递增的值。如果您将当前时间(最近的
    millis()
    call)与之前的“冻结”millis结果进行比较,您可以分辨出这两个时刻的时间差异。这有点类似于在现实世界中,你会有一个圈计时器和一个按钮按下/更新将拍摄时间流逝的快照,而时间一直滴答作响
  • 下面是一个非常基本的草图来说明这个想法:

    // a variable to store millis at a set time
    long lastMillis;
    // 5 seconds in millis
    const long fiveSeconds = 5 * 1000;
    
    void setup() {
      Serial.begin(9600);
      // remember the millis right now
      lastMillis = millis();
    }
    
    void loop() {
      // get current millis - ever increasing
      long millisNow = millis();
      // calculate the difference 
      long millisDifference = millisNow - lastMillis;
      // print debug text: open Serial Monitor to view
      Serial.print("time between setup complete and now:");
      Serial.println(millisDifference);
      // test after 5 seconds
      if(millisDifference >= fiveSeconds){
        Serial.println("5 seconds or more passed");  
      }
    }
    
    如果在波特率设置为9600的情况下打开串行监视器,则会出现一些调试文本。希望评论的文本能够说明上述各点

    第三件事是重置/更新
    lastMillis
    ,以便每5秒满足一次条件,而不是像上面的代码那样在5秒后连续满足:

    // a variable to store millis at a set time
    long lastMillis;
    // 5 seconds in millis
    const long fiveSeconds = 5 * 1000;
    
    void setup() {
      Serial.begin(9600);
      // remember the millis right now
      lastMillis = millis();
    }
    
    void loop() {
      // get current millis - ever increasing
      long millisNow = millis();
      // calculate the difference 
      long millisDifference = millisNow - lastMillis;
      // print debug text: open Serial Monitor to view
      Serial.print("time between setup complete and now:");
      Serial.println(millisDifference);
      // test after 5 seconds
      if(millisDifference >= fiveSeconds){
        Serial.println("5 seconds or more passed");  
        // update millis snaphot so this happens every 5 seconds
        lastMillis = millis();
      }
    }
    
    您可以使用此功能构建一个系统,在0和20之间移动扫描,而无需使用
    for/delay
    块循环,只需在每次
    循环()时递增即可:

    #include <Servo.h>
    
    Servo myservo;  // create servo object to control a servo
    
    // a variable to store millis at a set time
    long lastMillis;
    // seconds to millis
    const long INTERVAL = 4 * 1000;
    
    int pos = 0;    // variable to store the servo position
    int targetPos = 0;// the target position rotate towards
    
    void setup() {
      Serial.begin(9600);
      // remember the millis right now
      lastMillis = millis();
    
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
      myservo.write(pos);
      
      pinMode(LED_BUILTIN, OUTPUT);//LedR
    }
    
    void loop() {
      // get current millis - ever increasing
      long millisNow = millis();
      // calculate the difference 
      long millisDifference = millisNow - lastMillis;
      // print debug text: open Serial Monitor to view
      Serial.print("time between setup complete and now:");
      Serial.println(millisDifference);
      // test after 4 seconds
      if(millisDifference >= INTERVAL){
        Serial.println("4 seconds or more passed");  
        // update millis snaphot so this happens every 5 seconds
        lastMillis = millis();
        // flip between 0 and 20 servo target position by subtracting the current position from the maximum position
        // e.g. 20 - 0 = 20, otherwise, 20 - 20 = 0
        targetPos = 20 - targetPos;
      }
      // update servo position
      updateServo();
    }
    // update servo without a blocking delay
    // you could use an extra millis() based system
    void updateServo(){
      // difference between the current servo position and the next (target) servo position
      int positionDifference = targetPos - pos;
      // check the sign of the difference to tell if the servo should increment or decrement positions
      // otherwise ignore
      if(positionDifference > 0){
        // the target position is greater than the current therefore increase
        // feel free to change the increment to something nicer
        pos++;
        myservo.write(pos);
      }else{
        // the target position is smaller than the current therefore decrea
        pos--;
        myservo.write(pos);
      }
    }
    
    您可以使用一个计数器,以4秒为间隔,操作员检查您开启的4秒增量(每4、8、12等)以控制LED。如果您想轻松地更改模式,这将提高内存效率,但灵活性/趣味性会降低

    你可以这样做,意识到这会浪费更多宝贵的记忆:

    #include <Servo.h>
    
    Servo myservo;  // create servo object to control a servo
    
    // a variable to store millis at a set time
    long lastMillis;
    // seconds to millis
    const int SECONDS = 4;
    const long INTERVAL_MILLIS = SECONDS * 1000;
    
    int pos = 0;    // variable to store the servo position
    int targetPos = 0;// the target position rotate towards
    
    /*
     have an Arduino Uno, a Servo Motor & 2 LEDs (Green & Red). The Servo motor rotates 20 degrees and back every 4 seconds.
    
    I would like the Red led (LedR) to be on for the first 4 seconds of code then low for the next 12 seconds.
    
    I would like the Green led (ledG) to be on from the 8th second of code until the 12th then low for the next 12 seconds.
    
    time(s): 4,  8, 12, 16
    servo: [ 0][20][ 0][20]
    red:   [ 1][ 0][ 0][ 0]
    green: [ 0][ 1][ 0][ 0]
    */
    
    int intervalIndex;
    
    // rotates 20 degrees and back every 4 seconds.
    const int  SERVO_PATTERN[4] = {0, 20, 0, 20};
    // on for the first 4 seconds of code then low for the next 12 seconds.
    const bool RED_PATTERN[4]   = {1,  0, 0,  0};
    // on from the 8th second of code until the 12th then low for the next 12 seconds.
    const bool GREEN_PATTERN[4] = {0,  1, 0,  0};
    
    const int LED_PIN_RED   = LED_BUILTIN;
    //LedG, maybe pin 12: TODO update to what you've got on your breadboard
    const int LED_PIN_GREEN = 12;
    
    void setup() {
      Serial.begin(9600);
      // remember the millis right now
      lastMillis = millis();
    
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
      myservo.write(pos);
      
      pinMode(LED_PIN_RED, OUTPUT);//LedR
      pinMode(LED_PIN_GREEN, OUTPUT);//
    }
    
    void loop() {
      // get current millis - ever increasing
      long millisNow = millis();
      // calculate the difference 
      long millisDifference = millisNow - lastMillis;
      // print debug text: open Serial Monitor to view
      Serial.print("time between setup complete and now:");
      Serial.println(millisDifference);
      // test after 4 seconds
      if(millisDifference >= INTERVAL_MILLIS){
        Serial.println("4 seconds or more passed");  
        // update millis snaphot so this happens every 4 seconds
        lastMillis = millis();
    
        // update interval counter
        intervalIndex++;
        // reset every 4 => 0, 1, 2, 3, reset (perfect as array index)
        if(intervalIndex > 3){
          intervalIndex = 0;
        }
        // update servo target position
        targetPos = SERVO_PATTERN[intervalIndex];
        // update the red LED
        digitalWrite(LED_PIN_RED, RED_PATTERN[intervalIndex]);
        // update the green LED
        digitalWrite(LED_PIN_GREEN, GREEN_PATTERN[intervalIndex]);
      }
      // update servo position
      updateServo();
    }
    // update servo without a blocking delay
    // you could use an extra millis() based system
    void updateServo(){
      // difference between the current servo position and the next (target) servo position
      int positionDifference = targetPos - pos;
      // check the sign of the difference to tell if the servo should increment or decrement positions
      // otherwise ignore
      if(positionDifference > 0){
        // the target position is greater than the current therefore increase
        // feel free to change the increment to something nicer
        pos++;
        myservo.write(pos);
      }else{
        // the target position is smaller than the current therefore decrea
        pos--;
        myservo.write(pos);
      }
    }
    

    您的代码与您的问题完全无关。
    #include <Servo.h>
    
    Servo myservo;  // create servo object to control a servo
    
    // a variable to store millis at a set time
    long lastMillis;
    // seconds to millis
    const int SECONDS = 4;
    const long INTERVAL_MILLIS = SECONDS * 1000;
    
    int pos = 0;    // variable to store the servo position
    int targetPos = 0;// the target position rotate towards
    
    /*
     have an Arduino Uno, a Servo Motor & 2 LEDs (Green & Red). The Servo motor rotates 20 degrees and back every 4 seconds.
    
    I would like the Red led (LedR) to be on for the first 4 seconds of code then low for the next 12 seconds.
    
    I would like the Green led (ledG) to be on from the 8th second of code until the 12th then low for the next 12 seconds.
    
    time(s): 4,  8, 12, 16
    servo: [ 0][20][ 0][20]
    red:   [ 1][ 0][ 0][ 0]
    green: [ 0][ 1][ 0][ 0]
    */
    
    int intervalIndex;
    
    // rotates 20 degrees and back every 4 seconds.
    const int  SERVO_PATTERN[4] = {0, 20, 0, 20};
    // on for the first 4 seconds of code then low for the next 12 seconds.
    const bool RED_PATTERN[4]   = {1,  0, 0,  0};
    // on from the 8th second of code until the 12th then low for the next 12 seconds.
    const bool GREEN_PATTERN[4] = {0,  1, 0,  0};
    
    const int LED_PIN_RED   = LED_BUILTIN;
    //LedG, maybe pin 12: TODO update to what you've got on your breadboard
    const int LED_PIN_GREEN = 12;
    
    void setup() {
      Serial.begin(9600);
      // remember the millis right now
      lastMillis = millis();
    
      myservo.attach(9);  // attaches the servo on pin 9 to the servo object
      myservo.write(pos);
      
      pinMode(LED_PIN_RED, OUTPUT);//LedR
      pinMode(LED_PIN_GREEN, OUTPUT);//
    }
    
    void loop() {
      // get current millis - ever increasing
      long millisNow = millis();
      // calculate the difference 
      long millisDifference = millisNow - lastMillis;
      // print debug text: open Serial Monitor to view
      Serial.print("time between setup complete and now:");
      Serial.println(millisDifference);
      // test after 4 seconds
      if(millisDifference >= INTERVAL_MILLIS){
        Serial.println("4 seconds or more passed");  
        // update millis snaphot so this happens every 4 seconds
        lastMillis = millis();
    
        // update interval counter
        intervalIndex++;
        // reset every 4 => 0, 1, 2, 3, reset (perfect as array index)
        if(intervalIndex > 3){
          intervalIndex = 0;
        }
        // update servo target position
        targetPos = SERVO_PATTERN[intervalIndex];
        // update the red LED
        digitalWrite(LED_PIN_RED, RED_PATTERN[intervalIndex]);
        // update the green LED
        digitalWrite(LED_PIN_GREEN, GREEN_PATTERN[intervalIndex]);
      }
      // update servo position
      updateServo();
    }
    // update servo without a blocking delay
    // you could use an extra millis() based system
    void updateServo(){
      // difference between the current servo position and the next (target) servo position
      int positionDifference = targetPos - pos;
      // check the sign of the difference to tell if the servo should increment or decrement positions
      // otherwise ignore
      if(positionDifference > 0){
        // the target position is greater than the current therefore increase
        // feel free to change the increment to something nicer
        pos++;
        myservo.write(pos);
      }else{
        // the target position is smaller than the current therefore decrea
        pos--;
        myservo.write(pos);
      }
    }