Lesson 6--Microduino "Breathing Light"

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文

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".

Equipment


Lesson6All.jpg

Experiment Schematic

Lesson 6-schematic.jpg

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

int 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.

Result

LED gradually goes from off to on, then off. It then repeats.

Lesson6Result.jpg

Video

http://v.youku.com/v_show/id_XNzA5OTk3MTU2.html