user13334: I'm experimenting with buttons and LEDs and I would like my code to change a LED's brightness once a button is pressed. I connect the LED to the PWD digital pin.
My circuit looks like this one:

This is the code I'm using:
int switchPin = 8;
int ledPin = 11;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;

void setup() {
    pinMode(switchPin, INPUT);
    pinMode(ledPin, OUTPUT);
}

boolean debounce(boolean last) {
    boolean current = digitalRead(switchPin);
    if (last != current) {
        delay(5);
        current = digitalRead(switchPin);
    }
return current;
}

void loop() {
    currentButton = debounce(lastButton);
    if (lastButton == LOW && currentButton == HIGH) {
        ledLevel = ledLevel + 51;
    }
    lastButton = currentButton;

    if (ledLevel > 255) ledLevel = 0;
    analogWrite(ledPin, ledLevel);
}

Though, the LED never turns on, but the light on the Arduino is on. I made sure the leads of the LED were in the right spot. Why doesn't the LED turn on?

user4143: Because you don't actually have a circuit?  One side of the LED doesn't connect to anything.

13334: <p>I'm experimenting with buttons and LEDs and I would like my code to change a LED's brightness once a button is pressed. I connect the LED to the PWD digital pin.</p>

<p>My circuit looks like this one:</p>

<p><a href="https://i.stack.imgur.com/Ptv4v.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ptv4v.png" alt="enter image description here"></a></p>

<p>This is the code I'm using:</p>

<pre><code>int switchPin = 8;
int ledPin = 11;
boolean lastButton = LOW;
boolean currentButton = LOW;
int ledLevel = 0;

void setup() {
    pinMode(switchPin, INPUT);
    pinMode(ledPin, OUTPUT);
}

boolean debounce(boolean last) {
    boolean current = digitalRead(switchPin);
    if (last != current) {
        delay(5);
        current = digitalRead(switchPin);
    }
return current;
}

void loop() {
    currentButton = debounce(lastButton);
    if (lastButton == LOW &amp;&amp; currentButton == HIGH) {
        ledLevel = ledLevel + 51;
    }
    lastButton = currentButton;

    if (ledLevel &gt; 255) ledLevel = 0;
    analogWrite(ledPin, ledLevel);
}
</code></pre>

<p>Though, the LED never turns on, but the light on the Arduino is on. I made sure the leads of the LED were in the right spot. Why doesn't the LED turn on?</p>
 4143: <p>Because you don't actually <em>have</em> a circuit?  One side of the LED doesn't connect to anything.</p>
