# TinkerKit Tutorial: Basics: 10 – for loop

Basic Lessons:

In this tutorial we introduce the for loop.

So far we’ve seen how to light up an LED at a specific brightness using the method brightness(), that takes a value between 0 and 1023.

So if we want the LED to turn on at mid brightness, we have to write led.brightness(512).

What if we want the light value to change over time? If for example we want a pulsing LED? Well, there’s a function that comes in our help, it’s the for loop:

```for (int i = 0; i < 1023; i++) {
led.brightness(i);
delay(5);
}```

the purpose of the for loop is to repeat a piece of code until a condition is true. As we can see from the example above, the first part of the for is between regular brackets and is itself split in three parts:

int i = 0 first we set a new variable called i to 0

i < 1023 Until this condition is true, the loop is executed

i++ it’s the same as i = i + 1, it’s used to increase the value of i by one.

Basically we are creating a new variable that increases of one unit every loop, until it is greater than 1023. At that point, the loop is concluded and the program moves on.

The second part of the loop is inside curly brackets and is the piece of code that we want to run.

```led.brightness(i);
delay(5);```

Inside our code we write the i variable, that goes from 0 to 1023, to the brightness method of the LED. We have to put a delayafterwards, otherwise we cannot perceive the variation because the loops goes too fast.

this is the code we have so far:

```#include <TinkerKit.h>

TKLedled(O0);

voidsetup() {
//nothing
}

voidloop() {
for(inti = 0; i < 1023; i++) {
led.brightness(i);
delay(5);
}
}```

Ok we have a pulsing LED, what about fading it? We’ve seen how to increase it until it’s fully bright but now we want it also to slowly fade until it’s off.I’m sure you know how to do it, give it a try and only after that, read further for the solution.

Done? You had to copy the for loop and slightly modify some details, if before it was:

```for (int i = 0; i < 1023; i++) {
led.brightness(i);
delay(5);
}```

now it’s

```for (int i = 1023; i > 0; i--) {
led.brightness(i);
delay(5);
}```

just put them one after the other, in this way we slowly turn it on and then slowly turn it off repeatedly.

```#include <TinkerKit.h>

TKLed led(O0);

void setup() {
//nothing
}

void loop() {
for (int i = 0; i < 1023; i++) {
led.brightness(i);
delay(5);
}
for (int i = 1023; i > 0; i--) {
led.brightness(i);
delay(5);
}
}```

We can also play with the speed of the whole fading process by changing the delay value. To make it quick, we can use a variable to store the value so that we don’t have to change it in both of the for loops every time:

```#include <TinkerKit.h>

TKLed led(O0);
int del = 5;

void setup() {
//nothing
}

void loop() {
for (int i = 0; i < 1023; i++) {
led.brightness(i);
delay(del);
}
for (int i = 1023; i > 0; i--) {
led.brightness(i);
delay(del);
}
}```

Prev Section: Button States

Next Section: Traffic Light