Difference between revisions of "Lesson 6--Microduino "Breathing Light""
From Microduino Wiki
(→Program) |
(→Program) |
||
(6 intermediate revisions by 3 users not shown) | |||
Line 4: | Line 4: | ||
| | | | ||
==Objective== | ==Objective== | ||
− | + | In the last two lessons we used external devices (a button and a potentiometer) to generate PWM to control the LED. From Lesson 1, we also learned how to turned an LED on and off using software. But how would we go about controlling an LED's brightness through PWM using software only? This lesson will teach you how to implement an LED "Breathing Light". | |
− | This lesson will | ||
==Equipment== | ==Equipment== | ||
Line 11: | Line 10: | ||
*'''[[Microduino-FT232R]]''' | *'''[[Microduino-FT232R]]''' | ||
*Other hardware equipment | *Other hardware equipment | ||
− | ** | + | **1x Box of breadboard jumper wires |
− | **Breadboard | + | **1x Breadboard |
− | **LED | + | **1x LED |
− | **220Ω resistor | + | **1x 220Ω resistor |
− | **USB Data cable | + | **1x USB Data cable |
− | == | + | |
+ | [[File:lesson6All.jpg|600px|center|thumb]] | ||
+ | |||
+ | ==Experiment Schematic== | ||
[[File:lesson 6-schematic.jpg|600px|center|thumb]] | [[File:lesson 6-schematic.jpg|600px|center|thumb]] | ||
− | + | The schematic above uses a high level connection. Since we want the LED to be more than just on and off, we still need to connect the anode to a Microduino PWM I/O port. Using a low level connection achieves the same result. | |
==Program== | ==Program== | ||
<source lang="cpp"> | <source lang="cpp"> | ||
− | int ledPin=11;// | + | int ledPin=11;//D3, D5, D6, D9, D10, D11 is Microduino PWM I/O output port |
void setup() | void setup() | ||
{ | { | ||
+ | pinMode(ledPin,OUTPUT); | ||
} | } | ||
void loop() | void loop() | ||
Line 43: | Line 46: | ||
} | } | ||
</source> | </source> | ||
− | + | This program uses for loops, which allows for convenient control of the brightness of the LEDs. | |
==Result== | ==Result== | ||
− | LED | + | LED gradually goes from off to on, then off. It then repeats. |
+ | |||
+ | [[File:lesson6Result.jpg|600px|center|thumb]] | ||
==Video== | ==Video== | ||
+ | http://v.youku.com/v_show/id_XNzA5OTk3MTU2.html | ||
|} | |} |
Latest revision as of 07:21, 12 September 2016
Language: | English • 中文 |
---|
ObjectiveIn the last two lessons we used external devices (a button and a potentiometer) to generate PWM to control the LED. From Lesson 1, we also learned how to turned an LED on and off using software. But how would we go about controlling an LED's brightness through PWM using software only? This lesson will teach you how to implement an LED "Breathing Light". Equipment
Experiment SchematicThe schematic above uses a high level connection. Since we want the LED to be more than just on and off, we still need to connect the anode to a Microduino PWM I/O port. Using a low level connection achieves the same result. Programint ledPin=11;//D3, D5, D6, D9, D10, D11 is Microduino PWM I/O output port
void setup()
{
pinMode(ledPin,OUTPUT);
}
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.
}
} This program uses for loops, which allows for convenient control of the brightness of the LEDs. ResultLED gradually goes from off to on, then off. It then repeats. Video |