0

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:

enter image description here

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?

asked Sep 11, 2015 at 17:53
0

1 Answer 1

2

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

answered Sep 11, 2015 at 17:55
3
  • Thanks a lot for your answer, Majenko. Do I have to use a wire to connect things. Could you please propose a fix for this? Thanks. As you figured out, I'm very new to this. Commented Sep 11, 2015 at 17:57
  • 2
    One leg of the LED connects to the strip with a red line painted next to it. That then doesn't connect to anything unless you connect it to something. You should connect it to either +5V or GND depending on which way round your LED is. I can never tell which way the LEDs are in those awful fritzing drawings. Commented Sep 11, 2015 at 17:59
  • 1
    Those breadboards do not automatically connect the top red line to the bottom red line (nor the blue ones). You have to do that yourself, if that is what you want. Commented Sep 11, 2015 at 21:19

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.