为什么 millis() 函数加快了一个循环并减慢了另一个循环?

Why did millis() function sped up one loop and slowed down another one?

所以我正在制作一个带 LED 的无限镜盒,我已经被这个问题困扰了几天。我在盒子上有两个按钮和一个开关。开关将模式从一更改为三。第一种模式使 LED 始终点亮,第二种模式使 LED 像波浪一样缓慢反复打开和关闭,第三种模式使 LED 像心脏一样跳动。我为模式做了三个 while 循环。一切正常,直到我将 millis() 函数添加到第三个循环。我检查了代码一百次它应该像它应该的那样工作但是由于某种原因在添加 millis() 功能 LED 之后在第二种模式下开始非​​常快速地闪烁但它应该非常慢并且相反发生在第三种模式中。脉冲应该很快,但由于某种原因它们很慢。当我使用串行监视器时,看起来一切正常,但是当我打开 LED 时,奇怪的事情发生了。抱歉我的英语不好和长代码,但我真的需要帮助:DDD。

int modePin = 7;
int LEDPin = 11;
int lowPin = 2;
int highPin = 4;
int mode = 2;
int mButtonState = 0;
int mOldButtonState = 0;
int hButtonState = 0;
int hOldButtonState = 0;
int lButtonState = 0;
int lOldButtonState = 0;
float brightness = 255;
float waveLightSpeed = 1;
int pulseLightSpeed = 7;
int intervals = 500;
unsigned long prevTime = 0;





void setup() {
  Serial.begin(9600);
  pinMode(modePin, INPUT_PULLUP);
  pinMode(highPin, INPUT_PULLUP);
  pinMode(lowPin, INPUT_PULLUP);
  pinMode(LEDPin, OUTPUT);
}

void loop() {

  chechkForMode();

  while(mode == 1){

    brightness = 255;
    analogWrite(LEDPin, brightness);

    chechkForMode();
  }



  while(mode == 2){

    while(brightness > 10  and mode == 2){
      
      analogWrite(LEDPin, brightness);
      brightness = brightness - waveLightSpeed;
      
      chechkForMode();
      repeat();
    }

    while(brightness < 240 and mode == 2){
      
      analogWrite(LEDPin, brightness);
      brightness = brightness + waveLightSpeed;
      
      chechkForMode();
      repeat();
    }

    chechkForMode();
  }



  while(mode == 3){

    brightness = 255;
    analogWrite(LEDPin, brightness);
    repeat();
    unsigned long currentTime = millis();



    if (currentTime - prevTime > intervals){

        while(brightness > 10  and mode == 3){
        
        analogWrite(LEDPin, brightness);
        brightness = brightness - pulseLightSpeed;
        chechkForMode();
        repeat();
      }

      while(brightness < 240 and mode == 3){
      
        analogWrite(LEDPin, brightness);
        brightness = brightness + pulseLightSpeed;
        chechkForMode();
        repeat();
      }

      while(brightness > 10  and mode == 3){
        
        analogWrite(LEDPin, brightness);
        brightness = brightness - pulseLightSpeed;
        chechkForMode();
        repeat();
      }

     while(brightness < 240 and mode == 3){
      
        analogWrite(LEDPin, brightness);
        brightness = brightness + pulseLightSpeed;
        chechkForMode();
        repeat();
      }
      unsigned long currentTime = millis();
      prevTime = currentTime;
    }

    chechkForMode();
  }

}







void chechkForMode() {
  mButtonState = digitalRead(modePin);
  
  if (mButtonState != mOldButtonState) {
    
    if (mButtonState == LOW) {

      mode++;
      if (mode >= 4) {
        mode = 1;
      }    
    }
    if (mButtonState == HIGH) {

      mode++;
      if (mode >= 4) {
        mode = 1;
      }    
    }
  }
  mOldButtonState = mButtonState;
  Serial.print(mode);
  Serial.print(", ");
  Serial.println(brightness);
}



void repeat(){
  lButtonState = digitalRead(lowPin);
  hButtonState = digitalRead(highPin);
  
  if (lButtonState != lOldButtonState) {
    
    if (lButtonState == LOW) {
      
      intervals = intervals - 100;

      waveLightSpeed = waveLightSpeed - 0.2;

        if(waveLightSpeed < 0.2){
          waveLightSpeed = 0.2;
        }
      
        if(intervals < 100){
          intervals = 100;

    }
 
  }
  
  lOldButtonState = lButtonState;
}


if (hButtonState != hOldButtonState) {
    
    if (hButtonState == LOW) {
      
       intervals = intervals + 100;

       waveLightSpeed = waveLightSpeed + 0.2;

        if(waveLightSpeed > 3){
          waveLightSpeed = 3;
        }
       
        if(intervals > 5000){
          intervals = 5000;

    }
 
  }
  
  hOldButtonState = hButtonState;
}

}

删除第三个 while 循环末尾的 unsigned long

您正在第三个 while 循环的末尾创建一个新的本地变量 unsigned long currentTime = millis();。因此,在 if (currentTime - prevTime > intervals){ 中,变量 currentTime 始终是在模式 3 while 循环顶部设置的 currentTime 的原始值。