“Lesson 4--Microduino "LED Brightness and PWM"”的版本间的差异
(photo added) |
(photo added) |
||
第28行: | 第28行: | ||
Buttons are used with internal pull-up and external pull-down, then connect to the digital I/O port D0-D13. | Buttons 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. | PWM must use I/O port D3,D5,56,D9,D10 and D11. | ||
+ | |||
+ | [[File:lesson3Setup1.jpg|600px|center|thumb]] | ||
==Program== | ==Program== |
2015年7月3日 (五) 06:20的版本
Language | English |
---|
目录ObjectiveLED 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 |