Now that your LED is connected to the Arduino, you surely want to turn it on. No problem!
For a first test, you’ll make the LED blink again. This time, however, you’ll start with very rapid blinking and then make it gradually slower.
Variables and the Setup Function
First, declare two variables at the beginning of your sketch – making them global so they’re available throughout the sketch:
int ledPin = 9;
int pause = 0;
___STEADY_PAYWALL___
The first variable ledPin
defines the pin to which the LED is connected – in our case pin 9. The second variable pause
will come into play in the loop and will determine the time period during which the LED doesn’t light up. Since this period should gradually increase, you need a variable where you can store increasingly larger numbers.
In the void setup()
function, all you need to do is set the pinMode of the ledPin. Since you want to send signals from the Arduino, this is set to OUTPUT.
void setup()
{
pinMode(ledPin, OUTPUT);
}
The Loop
Here you first use the digitalWrite()
function, which you already know from the last blinking LED (the internal one).
You send a HIGH signal to the LED, wait 500 milliseconds, and then turn it off again with a LOW signal:
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
Now it gets interesting! In the next line, you increase the value in the variable pause
by 50. During the first iteration of the loop, this changes from 0 to 50 – in the second from 50 to 100 – in the third from 100 to 150 – and so on.
You then use this value in the delay()
function as the number of milliseconds that the LED should remain off:
pause = pause + 50;
delay(pause);
By the way: There’s also a shorthand form to increase a variable by a value other than one. You can program it like this:
pause += 50;
You can find the complete sketch in the Exercise Files for this lesson. Upload it to your Arduino and see what your LED does. If everything is connected correctly, it should initially blink very quickly. Only gradually will the intervals become longer, making the blinking slower.