Skip to main content

2. Fading LED

Arduino tutorial: Fading LED

 

Hi, Axtyax here,

and now we will now learn how to make an LED fade out or in with an arduino!


you will need:

  • Solderless Breadboard (if you dont know what this is, look it up)
  • Wires
  • An arduino
  • The arduino cable to connect it to your computer
  • A computer with the arduino IDE
  •  Im not completely sure, but i think you will need an 820ohm 1/8w resistor
  • (if you know of better specs for the resistor, just comment please)
  • And finally, an LED. 

Use this schematic to wire up the circuit:




http://arduino.cc/en/uploads/Tutorial/simplefade_bb.png
Now lets have a look at the code:


 --------------------------------------------------------------------------------------------------------------

/*
 Fade

 This example shows how to fade an LED on pin 9
 using the analogWrite() function.

 This example code is in the public domain.
 */


int led = 9;           // the pin that the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop()  {
  // set the brightness of pin 9:
  analogWrite(led, brightness);  

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }    
  // wait for 30 milliseconds to see the dimming effect    
  delay(30);                          
}
-------------------------------------------------------------------------------------------------------------------------
Read it through then copy and paste is into the IDE once you've understood how it all works.
 Dont forget to save and upload!
If it works, the LED  should fade in and out, every time it gets either to max brightness, or minimum brightness!

Thanks for reading!



Comments

Popular posts from this blog

Arduino Lesson : Using a DC motor

Hey guys, today we will be using the arduino to control a motor! to wire this just use this drawing I made in fritzing:   Unfortunately, these kinds of motors do not have an adapter that will allow you to connect it to the arduino, so I had to solder two jumper cables on each of those little metal things that come out of the motor and connect one to ground and the other to pin 6. This code will make that motor start spinning: void setup() { pinMode (6, OUTPUT); } void loop() { digitalWrite (6, HIGH); } Now upload this sketch to your arduino and the motor should start spinning. Which direction does it spin? now try swapping the cables, which way does it spin now? With most motors, it should be spinning in the opposite direction! If you have any problems do not forget to comment them! please follow me on this blog, and on twitter @axtyab, and hopefully refer this blog to others that you know!

C++ intro: Hello World

In almost all programming languages, the first program that people learn is how to print "Hello, World!" to the screen. So this is what we will be doing now in c++! So we start off  by doing this: #include <iostream> using namespace std; This basically says that we will be using the iostream library to be able to print things out to the console. don't worry about using namespace std; for now. int main() { return 0; } This is the function where everything will happen ( everything inside the brackets.) the return 0; is what we need to include at the end of the function because main is an int function. We just return 0 to show that the function has ended. cout << "Hello World!!!" << endl; This will go inside int main(). All this does is prints out Hello World!!! on the console, and endl just ends the line after that. Remember to include that semicolon at the end of cout. Your final code should look like this: #i

C++ Lesson : Variables

Just like in algebra, one of the first thing you must know in programming is how variables work. I am sure you understand, but if you don't, a variable is a letter or symbol that can represent any number. In C++, they can also represent things such as words or characters, but we will get into those later. Lets run through the most basic types of variables: int x; X is now an integer so it can be negative or positive, but NOT a decimal. double x; X can now be negative, positive, AND a decimal. char x; X can now be any one character like Y or H. char's are NOT values, so you CANNOT do any sort of mathematical operation with them, like this: char x = 10; char y = 5; x + y = z; z = 15; WRONG!!! but you CAN do this: char y; char x = 'h'; y = x; Now y = "h" because x = "h" and we said y = x; But now we need to understand how to assign values or characters to these variable types! For int: int x; x = 5; OR in