Dim with Blink


Step 1 - Build the Project

This program illustrates just how fast code can move. After you assign pin 13 to be an output in the setup, you combine the digitalWrite and the ‘for’ loop structure to create a bright-dim-bright blink cycle.

Step 2 - Upload the Code

//Blink using a fast flicker to dim the LED from full on to dim... void setup() { pinMode(13, OUTPUT); // LED } //To fade an LED, you actually blink it off & on very fast. There's a //limit to how much you can dim an LED this way before you notice //the flickering. void loop() { digitalWrite(13, HIGH); // The LED is fully on and bright for 1 second delay(1000); // cycle on/off 50 times in one second: (1+19)*50=1000 milliseconds for (int i=0; i<50; i++) { digitalWrite(13, HIGH); delay(1); digitalWrite(13, LOW); delay(19); } } //Try increasing the delays within the 'for' loop until you see the flicker // Need help? http://www.letsstartcoding.com/help // (c) 2016 Let's Start Coding. License: www.letsstartcoding.com/bsdlicense
 

Step 3 - Read the Walkthrough

The ‘for’ loop is specifying “If the variable i is less than 50, do this loop again and add 1 to i”. After 50 times through the loop i will not be less than 50, so the for loop breaks. Inside that loop is a bit of code to blink an LED by writing it HIGH, waiting 1 millisecond (1/1000th of a second), and then writing it LOW.

You’re blinking it so fast (50 times a second!) that the LED looks dim to your eye. Don’t believe it? Just increase the delays in the ‘for’ loop by a bit. You’ll start to notice the light flicker until it’s obviously turning on and off. This is related to the concept of persistence of vision.