Difference between revisions of "Lesson 6--Microduino "Breathing Light""
From Microduino Wiki
(→Program) |
|||
Line 27: | Line 27: | ||
{ | { | ||
} | } | ||
− | void loop(){ | + | void loop() |
+ | { | ||
for(int fadeValue=0;fadeValue<=255;fadeValue+=5) | for(int fadeValue=0;fadeValue<=255;fadeValue+=5) | ||
//Increase PWM value, control the LED brightness by adjusting the value of fadeValue. | //Increase PWM value, control the LED brightness by adjusting the value of fadeValue. | ||
Line 40: | Line 41: | ||
delay(30); //Keep the current brightness 30ms. | delay(30); //Keep the current brightness 30ms. | ||
} | } | ||
− | + | } | |
</source> | </source> | ||
Program uses the loop statement, it's convenient to automatically control the brightness of the leds. | Program uses the loop statement, it's convenient to automatically control the brightness of the leds. | ||
+ | |||
==Result== | ==Result== | ||
LED lights from off to on softly then off, followed by cycle. | LED lights from off to on softly then off, followed by cycle. |
Revision as of 13:35, 22 April 2014
Language: | English • 中文 |
---|
ObjectiveThe last two experiment uses the external device to generate PWM to control the LED, how to control the LED by program instead of the external device? This lesson will give you an example. This experiment implemented an led fadein dimming, named breathing lamp. Equipment
Experimental schematicUsing high level lighting, the output also need connect to Microduino's PWM I/O port. If use the low level lighting, it has the same result. Programint ledPin=11;//D3、D5、D6、D9、D10、D11 is Microduino PWM I/O output port
void setup()
{
}
void loop()
{
for(int fadeValue=0;fadeValue<=255;fadeValue+=5)
//Increase PWM value, control the LED brightness by adjusting the value of fadeValue.
{
analogWrite(ledPin,fadeValue); //Write the brightness value to LED.
delay(30); //Keep the current brightness 30ms.
}
for(int fadeValue=255;fadeValue>=0;fadeValue-=5)
//Decrease PWM value,control the LED brightness by adjusting the value of fadeValue.
{
analogWrite(ledPin,fadeValue); //Write the brightness value to LED.
delay(30); //Keep the current brightness 30ms.
}
} Program uses the loop statement, it's convenient to automatically control the brightness of the leds. ResultLED lights from off to on softly then off, followed by cycle. Video |