似易又非易 , 編程不簡單喔!!!!!!!!-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 721|回复: 4

[已解决] 似易又非易, 編程不簡單喔!!!!!!!!

[复制链接]
发表于 2022-2-3 15:46 | 显示全部楼层 |阅读模式
本帖最后由 andywwf 于 2022-2-4 19:45 编辑

恭喜發財!! 身體健康!!

請問如何可以在呼吸燈由PWM 0~225, 225~0 之後停止1秒再閞始?
  1. const byte pwmLED = 3;


  2. // define directions for LED fade
  3. #define UP 0
  4. #define DOWN 1

  5. // constants for min and max PWM
  6. const int minPWM = 0;
  7. const int maxPWM = 255;

  8. // State Variable for Fade Direction
  9. byte fadeDirection = UP;

  10. // Global Fade Value
  11. // but be bigger than byte and signed, for rollover
  12. int fadeValue = 0;

  13. // How smooth to fade?
  14. byte fadeIncrement = 5;

  15. // millis() timing Variable, just for fading
  16. unsigned long previousFadeMillis;

  17. // How fast to increment?
  18. int fadeInterval = 50;

  19. void setup() {
  20.   // put pwmLED into known state (off)
  21.   analogWrite(pwmLED, fadeValue);
  22. }

  23. void doTheFade(unsigned long thisMillis) {
  24.   // is it time to update yet?
  25.   // if not, nothing happens
  26.   if (thisMillis - previousFadeMillis >= fadeInterval) {
  27.     // yup, it's time!
  28.     if (fadeDirection == UP) {
  29.       fadeValue = fadeValue + fadeIncrement;  
  30.       if (fadeValue >= maxPWM) {
  31.         // At max, limit and change direction
  32.         fadeValue = maxPWM;
  33.         fadeDirection = DOWN;
  34.       }
  35.     } else {
  36.       //if we aren't going up, we're going down
  37.       fadeValue = fadeValue - fadeIncrement;
  38.       if (fadeValue <= minPWM) {
  39.         // At min, limit and change direction
  40.         fadeValue = minPWM;
  41.         fadeDirection = UP;
  42.       }
  43.     }
  44.     // Only need to update when it changes
  45.     analogWrite(pwmLED, fadeValue);  

  46.     // reset millis for the next iteration (fade timer only)
  47.     previousFadeMillis = thisMillis;
  48.   }
  49. }

  50. void loop() {
  51.   // get the current time, for this time around loop
  52.   // all millis() timer checks will use this time stamp
  53.   unsigned long currentMillis = millis();
  54.    
  55.   doTheFade(currentMillis);

  56. }
复制代码

发表于 2022-2-3 19:38 | 显示全部楼层
本帖最后由 lwq1947 于 2022-2-3 19:41 编辑

在else中的末尾添加一条 delay(1000);语句即可。另外若要LED完全熄灭请将const int minPWM = 0;改为const int minPWM = -5;
 楼主| 发表于 2022-2-3 21:40 | 显示全部楼层
lwq1947 发表于 2022-2-3 19:38
在else中的末尾添加一条 delay(1000);语句即可。另外若要LED完全熄灭请将const int minPWM = 0;改为const i ...

謝謝, 但用delay就會拖垮其他功能, 因為我還想加一個開關
发表于 2022-2-4 11:17 | 显示全部楼层
andywwf 发表于 2022-2-3 21:40
謝謝, 但用delay就會拖垮其他功能, 因為我還想加一個開關

改后的程序能满足你的要求。
const byte pwmLED = 3;
int k=0;

// define directions for LED fade
#define UP 0
#define DOWN 1

// constants for min and max PWM
const int minPWM = 0;
const int maxPWM = 255;

// State Variable for Fade Direction
byte fadeDirection = UP;

// Global Fade Value
// but be bigger than byte and signed, for rollover
int fadeValue = 0;

// How smooth to fade?
byte fadeIncrement = 5;

// millis() timing Variable, just for fading
unsigned long previousFadeMillis;

// How fast to increment?
int fadeInterval = 50;

void setup() {
  // put pwmLED into known state (off)
  analogWrite(pwmLED, fadeValue);
}

void doTheFade(unsigned long thisMillis) {
  // is it time to update yet?
  // if not, nothing happens
  if (thisMillis - previousFadeMillis >= fadeInterval) {
    k++;
    if(k==20) {
      k=0;
      fadeIncrement=5;
    }
    if (fadeDirection == UP) {
      fadeValue = fadeValue + fadeIncrement;  
      if (fadeValue >= maxPWM) {
        // At max, limit and change direction
        fadeValue = maxPWM;
        fadeDirection = DOWN;
      }
    } else {
      //if we aren't going up, we're going down
      fadeValue = fadeValue - fadeIncrement;
      if (fadeValue <= minPWM) {
        // At min, limit and change direction
        fadeValue = minPWM;
        fadeDirection = UP;
        fadeIncrement=0;
      }
    }
    // Only need to update when it changes
    analogWrite(pwmLED, fadeValue);  

    // reset millis for the next iteration (fade timer only)
    previousFadeMillis = thisMillis;
  }
}

void loop() {
  // get the current time, for this time around loop
  // all millis() timer checks will use this time stamp
  unsigned long currentMillis = millis();
   
  doTheFade(currentMillis);

}
 楼主| 发表于 2022-2-4 19:48 | 显示全部楼层
lwq1947 发表于 2022-2-4 11:17
改后的程序能满足你的要求。
const byte pwmLED = 3;
int k=0;

謝謝大哥 lwq1947
可以了, 我會好好學習, 多謝指導, 受教了.
祝  您虎年
神功護體 百毒不侵 身體健康 恭喜發財
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 15:49 , Processed in 0.070862 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表