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.
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.
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?
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.
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).