Go Down

Topic: LED - switching state and changing brigtness (Read 723 times) previous topic - next topic

Oxmaster

Code: [Select]
int ledPin = 11; - PWM one

The code is the only problem,

right now it just switches led on and off,
and I want to make when led is on then I can change it brightness level by pressing another push button,
I don't know how to make it.

Henry_Best


Code: [Select]
ledLevel = ledLevel + 51;
if (ledLevel > 255) ledLevel = 0;
analogWrite(ledPin, ledLevel);


this does PWM functions.

Correct, but over how long a period? Looks to me like your five steps are completed in around a millisecond and so aren't noticeable. Put a delay(500); in there and you might stand a chance of seeing it get brighter.

Oxmaster

I'll write everything step by step to you to understand:

http://i.imgur.com/Un5EPsb.jpg

this is my breadboard.
What I want to do:
1. If you press switch button then LED turns ON or OFF - toggle led not hold to light up
2. if you press level button then LED changes brightness, each push = +51 to value and if 255 - max then turn to 51
3. if you press level button while LED is OFF then nothing happens.

Oxmaster

Code: [Select]
while (ledOn == HIGH)
  {
ledButton = debounceLevel(lastButton);
if (ledButton == HIGH && lastButtonLevel == LOW)
{
ledLevel = ledLevel + 51;
}

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

currentButton = debounce(lastButton);
if (currentButton == HIGH) ledOn = !ledOn;
  }


I think that it should be like this.

Go Up