我需要编写一个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的水平衰落只在右侧