Brightness Control

My code is not working I need help :frowning:
This code is supposed to be a lamp

int led = 9; // the PWM pin the LED is attached to
int brightness = 0;
int ammount = 20; // the amount to increase by one click
int i = 0;

// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
pinMode(13, INPUT);
}

// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
if(digitalRead(13)==HIGH)
{
while(i>0)
{
if(13==LOW)
{
if(brightness < 255)
{
brightness = brightness + (255*(ammount/100));
}
else if(brightness >= 255)
{
brightness = 0;
}

}
i++;
}
i = 0;
}
}

if(13==LOW)

13 is never going to equal LOW.

Steve

As @slipstick says 13 is never going to equal LOW. Maybe you meant to write;
if(digitalRead(13)==LOW)

You should post your code within the </> tags.

Also smatter print statements through your code so that you can see where the program flow is going and why. That way you would probably have spotted your the error yourself. Quite often even if you think your program is working correctly having a few print statments in there will show you strange things going on.

aaryavansh:
while(i>0)

You initially declared i = 0, if there is nothing outside the while {} code that changes the value of i, then the condition will never be true.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html .
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

How have you got the button on pin13 wired?

Thanks.. Tom.. :slight_smile: