我需要編寫一個Arduino代碼,以交替頻率淡入淡出兩個LED。 也就是說 - 當第一個LED達到峯值時,第二個LED開始消失。 同樣最佳的是它應該沒有延遲地運行(),因爲我需要一堆其他代碼同時運行。我想我會使用SoftPWMLibrary,但我沒有看到如何使用它來淡化開始。Arduino中的時間衰落
0
A
回答
0
看看這個http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231200459#2。 我使用過這個解決方案,它效果很好!
0
也許你可以添加此
class fade
{
private:
uint8_t _min;
uint8_t _max;
uint8_t _to;
uint8_t _dutyCycle;
uint32_t _time;
uint32_t _last;
int _pin;
public:
fade (int pin, uint32_t timeStep=10000, uint8_t min=0, uint8_t max=255)
{
_pin = pin;
_time = timeStep;
_min = min;
_max = max;
analogWrite(_pin,_min);
_dutyCycle = _min;
}
void write(int to)
{
_to = (uint8_t) constrain(to,_min,_max);
this->update();
}
void update()
{
this->update(micros());
}
void update(uint32_t time)
{
if(time + _time > _last)
{
_last = time;
if(_dutyCycle > _to) analogWrite(_pin,--_dutyCycle);
if(_dutyCycle < _to) analogWrite(_pin,++_dutyCycle);
}
}
uint8_t read()
{
return _dutyCycle;
}
uint32_t readSpeed()
{
return _time;
}
uint32_t writeSpeed(uint32_t time)
{
_time=time;
}
};
/*now create a fade object
* fade foo(pin, timestep, min, max)
* timeStep is by default 10000 us (10 ms)
* min is by default 0
* max is by default 255
*
* example
* fade foo(13) is the same as fade foo(13, 10000, 0, 255)
*
* to change the speed once declared.
* foo.writeSpeed(new speed);
* to read the current speed
* foo.readSpeedspeed();
*
* foo.read() returs the current fade level (pwm dutyCycle);
* foo.write(to) defines the new endpoint of the fader;
*
* foo.update(); needs to called in the loop
* foo.update(time); is for saving time if more time relating objects;
* unsigned long a = micros();
* foo.update(a);
* bar.update(a);
* foobar.update(a);
* is faster than redefine time every update
*/
fade led1(11);// fader pin 11 speed 10 ms/ step
fade led2(10,100000);// fader pin 10 speed 100 ms/step;
void setup()
{
led2.write(128); //fade to half
led1.write(255); //fade to max
//setup
}
void loop()
{
unsigned long time = micros();
led1.update(time);
led2.update(time);
// loop
}
這不是測試,但應該工作
相關問題
- 1. Arduino衰落細節
- 2. Arduino PWM衰落導致與avra
- 3. 衰落的MKCircleView
- 4. 衰落android.support.v7.widget.Toolbar
- 5. 衰落與jQuery
- 6. jQuery的兩個類之間的衰落
- 7. 與衰落顏色
- 8. LoadScene與衰落C#
- 9. 衰落一個div
- 10. 衰落邊框(CSS)
- 11. 與衰落效應
- 12. 衰落包含HTML
- 13. 涉及衰落,時間和聲音的代碼
- 14. 衰落的div使用jQuery
- 15. Jquery的衰落在一類
- 16. JavaScript的衰落內容
- 17. 衰落背景中的中心圖像
- 18. 圖像在加載時衰落問題
- 19. UIWebview擁有焦點時衰落背景
- 20. jQuery中的衰落模態窗口
- 21. Android衰落邊緣消失
- 22. 使用CSS鏈衰落?
- 23. 顏色衰落算法?
- 24. 衰落背景圖片
- 25. UIView與衰落結束?
- 26. 衰落在另一個
- 27. HorizontalListView衰落邊緣
- 28. 元素衰落中,但不是淡出
- 29. 衰落在PHP中使用jQuery
- 30. ViewPager的水平衰落只在右側