The first step of this program is to create a variable to hold the brightness value of the LED. Here you'll use an integer called brightness and set its starting value to 0.
In the void setup() section, you set the pinMode for both the LED and the button.
The first line of the void loop() is new. First look at the right side of the equals sign. The constrain function takes three arguments: a variable name, the smallest value you want that variable to be able to reach, and the largest value the variable can reach. The function has the same effect as writing if statements so that "if variable is greater than max value, set variable to max value" and "if variable is less than minimum value, set variable to minimum value". Here you can shorten than to one one.
On the left side of the equals sign is your variable name. It looks like you are declaring this variable each time the loop runs, but you are not. The constrain function only matters when the variable value has reached its maximum or minimum. Otherwise, the constrain function doesn't change the value of the variable.
Next, you'll see a new command: analogWrite(). analogWrite() is a function built into the Arduino software. This function works by sending pulses of electricity to a component very quickly. The effect for an LED is that it looks dim because the 'flicker' is too fast for your eye to catch.
analogWrite() has some rules: It only works on pins 3,5,6,9,10, and 11 on Maker Board and its range is from 0 to 255.
This line writes the value of the variable 'brightness' to the LED. For the first loop, it's 0.
After a short delay, the program checks pin 12 for a signal- where the button is located. If the button is sending a LOW signal (it's pressed), the program increases the value of brightness by one.
If the button is pressed, the else statement is skipped, starting the loop over again. If the button is not pressed, brightness will be decreased by 1. The top line of the void loop() ensures that the value of the variable doesn't pass outside of 0 or 255.