Small problem lighting an LED

Hey,

I'm a noob, but a curious one. I hope you don't mind me asking such a simple question.
I've spent a good ten hours trying to make a very simple circuit work and I'm guessing I've misunderstood something or made a very rudimentary error.

I basically just want to use a button to make an LED gradually increase in brightness. i.e. press the button down and the led gradually moves to its full beam. release and it gradually dims to 0.

If anyone could give me a few pointers that's be great.

Here is my code

int buttonpress;
int ledpin;
int brightness;

void setup() {
pinMode (5, INPUT);
pinMode (9, OUTPUT);

}

void loop() {

if (buttonpress == HIGH)
{
for (brightness = 0; brightness <= 255; brightness += 5);
analogWrite (9, brightness);

}

else (buttonpress == LOW);
{
for (brightness = 255; brightness >= 0; brightness -=5);
analogWrite (9, brightness);

}

}

Dimming and button detection are separate tasks. In order to do them concurrently you have to use non-blocking, cooperative multitasking using millis() for the fade/dimming. Follow the links at the top of the forum to find information on doing "more than one thing at a time".

While you are there, read the instructions about how to post code.

 for (brightness = 0; brightness <= 255; brightness += 5);

I spy a semicolon that should not be there

In any case, how long do you suppose that the for loop will take to execute all 255 steps ?

and

elseĀ  (buttonpress == LOW);

is nonsense too.

if (buttonpress == HIGH)

well there's a problem straightaway.

elseĀ  (buttonpress == LOW);the semicolon is another.
If you'd actually read what the button state was, then the implied "if" would not be necessary. In either case, the semicolon is a waste of space.

Please remember to use code tags when posting code.

So how did you expect that any value like HIGH or LOW would get into buttonpress? And why do you have a variable ledpin that is never used? And what actual pins are you using for anything?

Steve

slipstick:
So how did you expect that any value like HIGH or LOW would get into buttonpress?

Well, LOW is zero, and so is buttonpress.

AWOL:
Well, LOW is zero, and so is buttonpress.

That's something I've learned then. Is the default value for a declared but uninitialised int really guaranteed to be 0? Normally in my limited experience of C/C++ uninitialised variables have indeterminate values (though I guess the compiler could always choose a value for them).

Of course it doesn't help this program but it does mean that it's only HIGH that can never happen.

Steve

All statics and globals are initialised to zero by crt0, unless they've been given another value by the programmer.
This happens before main() is called, so before init(), setup() and loop()

Thanks. So it's only variables with local scope that have indeterminate values if not initialised? That might make more sense of my hazy memories. OTOH I may just be a bit old to relearn C++ (after about 20 years away).

Steve

So it's only variables with local scope that have indeterminate values if not initialised?

Not if you declare them as static as noted by AWOL above.