Lesson 4--Microduino "LED Brightness and PWM"
From Microduino Wiki
Revision as of 00:54, 10 February 2014 by Jasonsheng (talk) (Created page with "{| style="width: 800px;" |- | ==Objective== LED has only two states on and off in other three experiment, now through button realize LED brightness light gradually and gradual...")
ContentsObjectiveLED has only two states on and off in other three experiment, now through button realize LED brightness light gradually and gradually out That is PWM pulse width modulation. Adjusting digital signal (" 0 ", "1") within a period of time that is the time of the duty ratio, high level "1", the longer and more bright. Detailed information for PWM, please refer to: http://www.geek-workshop.com/thread-125-1-1.html Equipment
Experimental schematicButtons are used with internal pull-up and external pull-down, then connect to the digital I/O port D0-D13. PWM must use I/O port D3,D5,56,D9,D10 and D11. Programint n=0;
void setup ()
{
pinMode(2,INPUT);
pinMode(7,INPUT_PULLUP);//Set to internal pull-up
pinMode(11,OUTPUT);//The PWM only can use I/O port 3、5、11、9、10、11
}
void loop()
{
int up =digitalRead(2); //Read port 2's state
int down = digitalRead(7); //Read port 7's state
if (up==HIGH)
{
n=n+5;
if (n>=255) {
n=255;
} //The max limitation is 255
analogWrite(11,n); //Using PWM control the output of port 11, the range of the variable n is 0-255
delay (300);
}
if (down==LOW)
{
n=n-5;
if (n<=0) {
n=0;
}
analogWrite(11,n); //Using PWM control the output of port 11, the range of the variable n is 0-255
delay (300);
}
} analogWrite()usage
ResultLeft button to make LED brightness progressively decreasing, and the right button to make LED brightness incremental. Video |